Module: Looksist::Core::ClassMethods

Defined in:
lib/looksist/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lookup_attributesObject

Returns the value of attribute lookup_attributes.



8
9
10
# File 'lib/looksist/core.rb', line 8

def lookup_attributes
  @lookup_attributes
end

Instance Method Details

#lookup(what, opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/looksist/core.rb', line 10

def lookup(what, opts)
  @lookup_attributes ||= {}
  unless opts.keys.all? { |k| [:using, :bucket_name, :as].include? k }
    raise 'Incorrect usage: Invalid parameter specified'
  end
  using, bucket_name, as = opts[:using], opts[:bucket_name] || opts[:using], opts[:as]
  if what.is_a? Array
    setup_composite_lookup(bucket_name, using, what, as)
  else
    alias_what = find_alias(as, what)
    @lookup_attributes[alias_what] = opts[:using]
    define_method(alias_what) do
      Looksist.redis_service.send("#{__entity__(bucket_name)}_for", self.send(using).try(:to_s))
    end
  end
end