class WashingtonHikes::Region

Attributes

hikes[RW]
name[RW]

Public Class Methods

all() click to toggle source

All regions in class

# File lib/washington_hikes/region.rb, line 19
def self.all
  @@all
end
find_or_create_region_by_name(name) click to toggle source

Find or create a new region by name

# File lib/washington_hikes/region.rb, line 13
def self.find_or_create_region_by_name(name)
  existing = self.all.detect{|region| region.name == name}
  existing != nil ? existing : self.new(name)
end
new(name) click to toggle source

Initializes a region with a name and hikes array

# File lib/washington_hikes/region.rb, line 6
def initialize(name)
  @name = name
  @hikes = []
  @@all << self
end

Public Instance Methods

add_hike(hike) click to toggle source

Add hike to region's 'hikes' array

# File lib/washington_hikes/region.rb, line 24
def add_hike(hike)
  @hikes << hike
end
average_hike_rating() click to toggle source

Calculates the average rating of popular hikes in the region

# File lib/washington_hikes/region.rb, line 47
def average_hike_rating
  ratings = @hikes.collect {|hike| hike.rating}   # Collect ratings of hikes in region
  (ratings.sum / ratings.size).round(2)           # Return the average rating of hikes in region
end
common_landscape_features() click to toggle source

Identifies the most common landcape features in a region based on hike features

# File lib/washington_hikes/region.rb, line 29
def common_landscape_features
  # Collect count of each landscape feature from hikes in region
  feature_list = {}
  not_landscape = ["Dogs allowed on leash", "Dogs not allowed", "Established campsites", "Good for kids", "Fall foliage"]

  @hikes.each do |hike|
    hike.features.each do |feature|
      if not_landscape.include?(feature) == false
        feature_list[feature] == nil ? feature_list[feature] = 1 : feature_list[feature] += 1
      end
    end
  end

  # Sort feature list by most common and return the top 5 landscape features
  feature_list.sort_by{|feature,count| count}.reverse.flatten.delete_if{|x| x.is_a?(Integer)}[0..4]
end