Module: Mooset::Findable::InstanceMethods

Defined in:
lib/mooset/findable.rb

Instance Method Summary collapse

Instance Method Details

#find(opts, *args) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/mooset/findable.rb', line 50

def find(opts, *args)
  logger.debug "find #{opts}"

  return find_from_hash(opts, *args) if opts.is_a? Hash

  find_from_object(opts, args)
end

#find_from_hash(hash) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mooset/findable.rb', line 38

def find_from_hash(hash)
  key, value = hash.find { |(k, _)| respond_to? self.class.finder_method_name(k) }

  args = hash.tap do |h|
    h.delete(key)
  end

  if key && value
    public_send(self.class.finder_method_name(key), value, args)
  end
end

#find_from_object(object, *args) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mooset/findable.rb', line 30

def find_from_object(object, *args)
  finder = object.finders.find { |x| respond_to? self.class.finder_method_name(x) }

  if finder && object[finder]
    public_send(self.class.finder_method_name(finder), object[finder], *args)
  end
end

#import(object) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/mooset/findable.rb', line 22

def import(object)
  logger.debug "import #{object}"

  method = self.class.import_method_name(object.provider)

  public_send(method, object)
end