Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/leolay/templates/lib/upd_array.rb

Instance Method Summary collapse

Instance Method Details

#item(options = {}) ⇒ Object

Find in collection where :search condition is matched and return a fields if param :get is passed otherwise it return the item Example1 Articles.quickly :search => => 1, :get => :title Example2 People.quickly :search => => ABCDFG01H02I100J



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/generators/leolay/templates/lib/upd_array.rb', line 6

def item(options={})
  #:search     => {:id => 3}
  #:get        => :name
  options = {
    :search   => nil,
    :get      => nil
  }.merge(options)
  raise "Array.trova: specify required parameters!" unless options[:search]
  self.each do |item|
    condition = nil
    options[:search].each do |label, value|
      condition = (item[label] == value)
      break unless condition
    end
    next unless condition
    return (options[:get] ? item[options[:get]] : item)
  end
  nil
end