Class: Pilipinas::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/pilipinas/base.rb
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
8
9
10
11
|
# File 'lib/pilipinas/base.rb', line 8
def initialize(options = {})
@code = options[:code]
@name = options[:name]
end
|
Class Attribute Details
.data ⇒ Object
Returns the value of attribute data.
14
15
16
|
# File 'lib/pilipinas/base.rb', line 14
def data
@data
end
|
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
3
4
5
|
# File 'lib/pilipinas/base.rb', line 3
def code
@code
end
|
#name ⇒ Object
Returns the value of attribute name.
3
4
5
|
# File 'lib/pilipinas/base.rb', line 3
def name
@name
end
|
Class Method Details
.add(region) ⇒ Object
63
64
65
66
|
# File 'lib/pilipinas/base.rb', line 63
def add(region)
self.data ||= []
self.data << region
end
|
.all ⇒ Object
35
36
37
|
# File 'lib/pilipinas/base.rb', line 35
def all
load_data
end
|
.assoc_collection(options) ⇒ Object
16
17
18
19
20
21
|
# File 'lib/pilipinas/base.rb', line 16
def assoc_collection(options)
code = options.fetch(:code)
dir = options.fetch(:dir)
load_file(File.join(File.dirname(__FILE__), '..', 'data', dir.to_s, "#{code}.yml"))
end
|
.count ⇒ Object
23
24
25
|
# File 'lib/pilipinas/base.rb', line 23
def count
all.count
end
|
.find_by(options) ⇒ Object
43
44
45
46
|
# File 'lib/pilipinas/base.rb', line 43
def find_by(options)
raise 'invalid hash' if options.empty?
send "find_by_#{options.keys.first.to_s}", options.values.first.to_s
end
|
.first ⇒ Object
27
28
29
|
# File 'lib/pilipinas/base.rb', line 27
def first
all.first
end
|
.last ⇒ Object
31
32
33
|
# File 'lib/pilipinas/base.rb', line 31
def last
all.last
end
|
.load_data ⇒ Object
39
40
41
|
# File 'lib/pilipinas/base.rb', line 39
def load_data
raise 'Implement load_data'
end
|
.load_file(file) ⇒ Object
56
57
58
59
60
61
|
# File 'lib/pilipinas/base.rb', line 56
def load_file(file)
reset_data
yaml = YAML.load_file(file)
yaml.each { |hash, _opts| add(new(hash)) } if yaml
data
end
|
.method_missing(*args) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/pilipinas/base.rb', line 48
def method_missing(*args)
regex = args.first.to_s.match(/^find_by_(.*)/)
super if !regex || Regexp.last_match(1).nil?
load_data unless data
select_by(Regexp.last_match(1), args[1])
end
|
.parse_attributes(attr, val) ⇒ Object
76
77
78
79
80
|
# File 'lib/pilipinas/base.rb', line 76
def parse_attributes(attr, val)
raise Pilipinas::UnknownAttribute, "Invalid attribute '#{attr}'." unless %i[code name].include?(attr.to_sym)
[attr, val]
end
|
.reset_data ⇒ Object
86
87
88
|
# File 'lib/pilipinas/base.rb', line 86
def reset_data
self.data = []
end
|
.select_by(attribute, val) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/pilipinas/base.rb', line 68
def select_by(attribute, val)
attr, value = parse_attributes(attribute.downcase, (val ? val.to_s.downcase : val))
obj = data.select { |c| c.send(attr.to_sym).downcase == value }
validate(obj)
obj.first
end
|
.validate(obj) ⇒ Object
82
83
84
|
# File 'lib/pilipinas/base.rb', line 82
def validate(obj)
return nil if obj.empty?
end
|