Class: ActiveEnumerable::Finder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_enumerable/finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ Finder

Returns a new instance of Finder.



4
5
6
# File 'lib/active_enumerable/finder.rb', line 4

def initialize(record)
  @method_caller = MethodCaller.new(record)
end

Instance Method Details

#is_of(conditions = {}) ⇒ true, false

Regex conditions

Finder.new({ name: "Timmy" }).is_of({ name: /Tim/ })
  #=> true

Hash conditions

record = { name: "Timmy", parents: [{ name: "Dad", age: 33 }, { name: "Mom", age: 29 }] } }

Matching array of partial hashes identities
  Finder.new(record).is_of(parents: [{ name: "Dad" }, { name: "Mom" }]))
    #=> true

Matching partial hashes identities to an array of hashes
  Finder.new(record).is_of(parents: { name: "Dad", age: 33 })
    #=> true

Array conditions

record = { name: "Timmy" }

Finder.new(record).is_of(name: %w(Timmy Fred))
  #=> true
Finder.new(record).is_of(name: ["Sammy", /Tim/])
  #=> true

Value conditions

record = { name: "Timmy", age: 10 }

Finder.new(record).is_of(name: "Timmy")
  #=> true
Finder.new(record).is_of(age: 10)
  #=> true

Parameters:

  • conditions (Hash) (defaults to: {})

Returns:

  • (true, false)


41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_enumerable/finder.rb', line 41

def is_of(conditions={})
  conditions.all? do |col, match|
    if match.is_a? Hash
      hash_match(col, match)
    elsif match.is_a? Array
      array_match(col, match)
    else
      compare(col, match)
    end
  end
end