Class: Decoder::Country

Inherits:
Object
  • Object
show all
Includes:
CommonMethods
Defined in:
lib/decoder/country.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonMethods

#inspect, #to_s

Constructor Details

#initialize(args) ⇒ Country

Returns a new instance of Country.



8
9
10
11
12
# File 'lib/decoder/country.rb', line 8

def initialize(args)
  self.code   = args[:code].to_s
  self.name   = args[:name]
  self.states = Decoder.locale[Decoder.i18n][self.code][:states]
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



6
7
8
# File 'lib/decoder/country.rb', line 6

def code
  @code
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/decoder/country.rb', line 6

def name
  @name
end

Instance Method Details

#[](_code) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/decoder/country.rb', line 26

def [](_code)
  _code = _code.to_s.upcase
  state = states[_code]
  if state.is_a?(Array)
    fips  = state.last
    state = state.first
  end
  Decoder::State.new(:code => _code, :name => state, :fips => fips)
end

#by_fips(fips) ⇒ Object



36
37
38
39
40
# File 'lib/decoder/country.rb', line 36

def by_fips(fips)
  fips = fips.to_s
  state = states.detect { |k,v| v.include?(fips) if v.is_a?(Array) }
  self[state.first]
end

#statesObject Also known as: counties, provinces, territories



14
15
16
# File 'lib/decoder/country.rb', line 14

def states
  @states
end

#states=(_states) ⇒ Object



18
19
20
# File 'lib/decoder/country.rb', line 18

def states=(_states)
  @states = _states
end