module Pulo::Tables::ClassMethods

Public Instance Methods

add_item(key,value) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# 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
method_missing(method_sym, *arguments, &block) click to toggle source
# 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
quantity() click to toggle source

the quantity specified for the table

# File lib/pulo/tables/tables.rb, line 43
def quantity
  @quantity
end
remove_item(key) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/pulo/tables/tables.rb, line 52
def to_h
  to_a.to_h
end
to_yaml() click to toggle source
# File lib/pulo/tables/tables.rb, line 75
def to_yaml
  @values.to_yaml
end
unit() click to toggle source

the unit specified for the table

# File lib/pulo/tables/tables.rb, line 38
def unit
  @unit
end