Class: Unitwise::Standard::Base

Inherits:
Object
  • Object
show all
Includes:
Extras
Defined in:
lib/unitwise/standard/base.rb

Direct Known Subclasses

BaseUnit, DerivedUnit, Prefix

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extras

#hash_to_markup

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



34
35
36
# File 'lib/unitwise/standard/base.rb', line 34

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/unitwise/standard/base.rb', line 6

def attributes
  @attributes
end

Class Method Details

.allObject



12
13
14
# File 'lib/unitwise/standard/base.rb', line 12

def self.all
  @all ||= read
end

.hashObject



20
21
22
# File 'lib/unitwise/standard/base.rb', line 20

def self.hash
  self.all.map(&:to_hash)
end

.local_keyObject



8
9
10
# File 'lib/unitwise/standard/base.rb', line 8

def self.local_key
  remote_key
end

.pathObject



24
25
26
# File 'lib/unitwise/standard/base.rb', line 24

def self.path
  Unitwise.data_file(local_key)
end

.readObject



16
17
18
# File 'lib/unitwise/standard/base.rb', line 16

def self.read
  Unitwise::Standard.hash[remote_key].inject([]){|a,h| a << self.new(h)}
end

.writeObject



28
29
30
31
32
# File 'lib/unitwise/standard/base.rb', line 28

def self.write
  File.open(path, 'w') do |f|
    f.write hash.to_yaml
  end
end

Instance Method Details

#namesObject



38
39
40
41
42
43
44
# File 'lib/unitwise/standard/base.rb', line 38

def names
  if attributes["name"].respond_to?(:map)
    attributes["name"].map(&:to_s)
  else
    attributes["name"].to_s
  end
end

#primary_codeObject



55
56
57
# File 'lib/unitwise/standard/base.rb', line 55

def primary_code
  attributes["@Code"]
end

#secondary_codeObject



59
60
61
# File 'lib/unitwise/standard/base.rb', line 59

def secondary_code
  attributes["@CODE"]
end

#symbolObject



46
47
48
49
50
51
52
53
# File 'lib/unitwise/standard/base.rb', line 46

def symbol
  sym = attributes["printSymbol"]
  if sym.is_a?(Hash)
    hash_to_markup(sym)
  elsif sym
    sym.to_s
  end
end

#to_hashObject



63
64
65
66
67
68
69
70
# File 'lib/unitwise/standard/base.rb', line 63

def to_hash
  [:names, :symbol, :primary_code, :secondary_code].inject({}) do |h,a|
    if v = self.send(a)
      h[a] = v
    end
    h
  end
end