Class: ISOCodes::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/iso_codes/base.rb

Direct Known Subclasses

Country, Currency, Language

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



48
49
50
51
52
# File 'lib/iso_codes/base.rb', line 48

def initialize(data)
  data.each do |k, v|
    instance_variable_set :"@#{k}", v
  end
end

Class Method Details

.allObject



27
28
29
30
31
# File 'lib/iso_codes/base.rb', line 27

def all
  data.collect do |item|
    self.new(item)
  end
end

.fields(*field_names) ⇒ Object



4
5
6
7
8
9
# File 'lib/iso_codes/base.rb', line 4

def fields(*field_names)
  field_names.each do |field|
    attr_reader field
    create_finder field
  end
end

.find(field, val) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/iso_codes/base.rb', line 11

def find(field, val)
  data.each do |item|
    return self.new(item) if item[field.to_sym] == val
  end
  
  nil
end

.find_all(field, val) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/iso_codes/base.rb', line 19

def find_all(field, val)
  data.collect do |item|
    if item[field.to_sym] == val
      self.new(item)
    end
  end.compact
end