Class: NationalParks::State

Inherits:
Object
  • Object
show all
Defined in:
lib/national_parks/state.rb

Constant Summary collapse

@@states =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_attribute_hash = nil) ⇒ State

Returns a new instance of State.



7
8
9
10
11
12
13
# File 'lib/national_parks/state.rb', line 7

def initialize(state_attribute_hash = nil)
  if state_attribute_hash
    state_attribute_hash.each{|key, value| self.send("#{key}=", value)}
  end
  @parks = [] # has many park objects interface
  @@states << self
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/national_parks/state.rb', line 3

def name
  @name
end

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/national_parks/state.rb', line 3

def url
  @url
end

Class Method Details

.allObject



32
33
34
# File 'lib/national_parks/state.rb', line 32

def self.all
  @@states.sort_by{|state| state.name} # state objects stored in alphabetical order by name
end

.find_state(id) ⇒ Object



40
41
42
# File 'lib/national_parks/state.rb', line 40

def self.find_state(id)
  self.all[id.to_i - 1]
end

.find_state_by_name(name) ⇒ Object



36
37
38
# File 'lib/national_parks/state.rb', line 36

def self.find_state_by_name(name)
  self.all.detect{|state| state.name.downcase == name.downcase}
end

Instance Method Details

#add_park(park) ⇒ Object

has many park objects interface



19
20
21
22
23
24
25
26
# File 'lib/national_parks/state.rb', line 19

def add_park(park) # has many park objects interface
  if !park.is_a?(NationalParks::Park)
    raise InvalidType, "#{park.class} received, Park expected"
  else
    @parks << park unless @parks.include?(park)
    park.state = self unless park.state == self
  end
end

#find_park(id) ⇒ Object



28
29
30
# File 'lib/national_parks/state.rb', line 28

def find_park(id)
  parks[id.to_i - 1]
end

#parksObject

has many park objects interface



15
16
17
# File 'lib/national_parks/state.rb', line 15

def parks # has many park objects interface
  @parks.dup.freeze.sort_by{|park| park.name} # park objects of each state object stored in alphabetical order by name
end