Module: Pulo::Tables::ClassMethods
- Defined in:
- lib/pulo/tables/tables.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/pulo/tables/tables.rb', line 18
def method_missing(method_sym, *arguments, &block)
if @values[method_sym]
@quantity.new(@values[method_sym],@unit)
else
if @values.respond_to?(method_sym)
@values.send(method_sym,*arguments,&block)
else
raise "Can't find item '#{method_sym}' in table #{self.name.split('::')[1]}."
end
end
end
|
Instance Method Details
#add_item(key, value) ⇒ Object
84
85
86
87
88
|
# File 'lib/pulo/tables/tables.rb', line 84
def add_item(key,value)
key=key.to_s.gsub(/\s+/,"_").to_sym unless key.is_a?(Symbol)
@values.merge!({key=>value})
self.save_yaml
end
|
#find(value) ⇒ Object
30
31
32
33
34
35
|
# File 'lib/pulo/tables/tables.rb', line 30
def find value
search_value=value.downcase
Hash[(@values.select { |key, value| key.to_s.downcase.include? search_value }).map do |elm|
[elm[0],@quantity.new(elm[1],@unit)]
end]
end
|
#load_yaml ⇒ Object
81
82
83
|
# File 'lib/pulo/tables/tables.rb', line 81
def load_yaml
@values=YAML.load_file(File.join(__dir__,'table_data', self.name.split('::')[1] + '.yaml'))
end
|
#quantity ⇒ Object
the quantity specified for the table
43
44
45
|
# File 'lib/pulo/tables/tables.rb', line 43
def quantity
@quantity
end
|
#remove_item(key) ⇒ Object
89
90
91
92
93
|
# File 'lib/pulo/tables/tables.rb', line 89
def remove_item(key)
key=key.to_s.gsub(/\s+/,"_").to_sym unless key.is_a?(Symbol)
@values.delete(key)
self.save_yaml
end
|
#save_yaml ⇒ Object
78
79
80
|
# File 'lib/pulo/tables/tables.rb', line 78
def save_yaml
File.write(File.join(__dir__,'table_data', self.name.split('::')[1] + '.yaml'),self.to_yaml)
end
|
#sort ⇒ Object
55
56
57
58
59
|
# File 'lib/pulo/tables/tables.rb', line 55
def sort
(@values.sort_by {|k,v| v}).map do |elm|
[elm[0],@quantity.new(elm[1],@unit)]
end
end
|
#sort_reverse ⇒ Object
60
61
62
63
64
|
# File 'lib/pulo/tables/tables.rb', line 60
def sort_reverse
(@values.sort_by {|k,v| -v}).map do |elm|
[elm[0],@quantity.new(elm[1],@unit)]
end
end
|
#to_a ⇒ Object
47
48
49
50
51
|
# File 'lib/pulo/tables/tables.rb', line 47
def to_a
@values.to_a.map do |elm|
[elm[0],@quantity.new(elm[1],@unit)]
end
end
|
#to_frame ⇒ Object
66
67
68
69
70
71
72
73
74
|
# File 'lib/pulo/tables/tables.rb', line 66
def to_frame
frm=Frame.new
frm.append_column('Item')
frm.append_column(@quantity.quantity_name)
@values.to_a.each do |ar|
frm.append_row([ar[0],self.send(ar[0],ar[1])])
end
frm
end
|
#to_h ⇒ Object
52
53
54
|
# File 'lib/pulo/tables/tables.rb', line 52
def to_h
to_a.to_h
end
|
#to_yaml ⇒ Object
75
76
77
|
# File 'lib/pulo/tables/tables.rb', line 75
def to_yaml
@values.to_yaml
end
|
#unit ⇒ Object
the unit specified for the table
38
39
40
|
# File 'lib/pulo/tables/tables.rb', line 38
def unit
@unit
end
|