module TinyRecord::ClassMethods

Public Instance Methods

fetch(id, with: nil) click to toggle source
# File lib/tiny/record.rb, line 12
def fetch(id, with: nil)
  _get_records(_primary_lookup(id), with).take!
end
fetch_by(tiny_columns = {}) click to toggle source
# File lib/tiny/record.rb, line 16
def fetch_by(tiny_columns = {})
  with = tiny_columns.delete(:with)
  _get_records(tiny_columns, with).take
end
fetch_where(*args) click to toggle source
# File lib/tiny/record.rb, line 21
def fetch_where(*args)
  if args[0].is_a?(Hash)
    _fetch_from_hash(args)
  else
    _fetch_from_string(args)
  end
end
tiny_columns(*args) click to toggle source
# File lib/tiny/record.rb, line 8
def tiny_columns(*args)
  @default_tiny_columns = *args
end

Private Instance Methods

_default_tiny_columns() click to toggle source
# File lib/tiny/record.rb, line 53
def _default_tiny_columns
  @default_tiny_columns ||= []
  @default_tiny_columns
end
_fetch_from_hash(args) click to toggle source
# File lib/tiny/record.rb, line 43
def _fetch_from_hash(args)
  target_parameter = args[0]
  with = target_parameter.delete(:with)
  _get_records(target_parameter, with)
end
_fetch_from_string(args) click to toggle source
# File lib/tiny/record.rb, line 49
def _fetch_from_string(args)
  _get_records(args)
end
_get_records(by_columns, with_columns = nil) click to toggle source
# File lib/tiny/record.rb, line 31
def _get_records(by_columns, with_columns = nil)
  collection = where(by_columns)
  collection = _retrieve_selective_columns(collection,with_columns)
  collection
end
_primary_lookup(lookup_id) click to toggle source
# File lib/tiny/record.rb, line 58
def _primary_lookup(lookup_id)
  { primary_key => lookup_id }
end
_retrieve_selective_columns(collection, requested_columns) click to toggle source
# File lib/tiny/record.rb, line 37
def _retrieve_selective_columns(collection, requested_columns)
  target_columns = requested_columns && requested_columns.present? ? requested_columns : _default_tiny_columns
  collection = collection.select(target_columns)
  collection
end