Module: Ext::Object

Defined in:
lib/ext/object.rb

Instance Method Summary collapse

Instance Method Details

#attrs_match?(**args) ⇒ Boolean

Returns whether all key/value pairs match.

Returns:

  • (Boolean)

    whether all key/value pairs match



5
6
7
8
9
# File 'lib/ext/object.rb', line 5

def attrs_match?(**args)
  args.all? do |key, value|
    key_value_match?(key, value)
  end
end

#key_value_match?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ext/object.rb', line 11

def key_value_match?(key, value)
  if respond_to?(:[])
    attribute = self[key]
    return true if attribute == value
    return true if attribute.respond_to?(value) && attribute.send(value)
  end

  if respond_to?(key)
    attribute = send(key)
    return true if attribute == value
    return true if attribute.respond_to?(value) && attribute.send(value)
  end

  return false
rescue TypeError, NoMethodError, NameError, ArgumentError
  false
end