Module: Widgeo::Collection

Included in:
Continent, Territory
Defined in:
lib/widgeo/collection.rb

Instance Method Summary collapse

Instance Method Details

#allObject

Parse full list and memoize it



50
51
52
53
54
# File 'lib/widgeo/collection.rb', line 50

def all

  @all ||= dataset.map { |item| self.new item }

end

#datasetObject

Return the dataset



13
14
15
16
17
18
19
20
21
22
# File 'lib/widgeo/collection.rb', line 13

def dataset

  # Raise an exception if the dataset file has not been set on the class
  raise UndefinedDatasetError unless dataset_file

  @dataset ||= YAML.load_file(
    File.join(File.dirname(__FILE__), "../", "data", "#{dataset_file}.yml")
  )

end

#dataset_file(file_name = nil) ⇒ Object

Set/Get the dataset file name



6
7
8
9
10
# File 'lib/widgeo/collection.rb', line 6

def dataset_file file_name = nil

  @dataset_file ||= file_name

end

#filter_by(attributes) ⇒ Object

Filter the items by a combination of values



36
37
38
39
40
# File 'lib/widgeo/collection.rb', line 36

def filter_by attributes

  all.select { |item| matches? item, attributes }

end

#find_by(attribute, value) ⇒ Object

Find an item by an attribute



43
44
45
46
47
# File 'lib/widgeo/collection.rb', line 43

def find_by attribute, value

  all.detect { |continent| continent.send(attribute) == value }

end

#matches?(item, attributes) ⇒ Boolean

Does the item match the requested attributes?

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
# File 'lib/widgeo/collection.rb', line 25

def matches? item, attributes

  attributes.map { |attribute, value|

    item.send(attribute) == value

  }.flatten == [true]

end