Class: Unitwise::Base

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

Direct Known Subclasses

Atom, Prefix

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Base



30
31
32
33
34
# File 'lib/unitwise/base.rb', line 30

def initialize(attrs)
  attrs.each do |k, v|
    public_send :"#{k}=", v
  end
end

Instance Attribute Details

#namesObject

Returns the value of attribute names.



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

def names
  @names
end

#primary_codeObject

Returns the value of attribute primary_code.



4
5
6
# File 'lib/unitwise/base.rb', line 4

def primary_code
  @primary_code
end

#scaleObject (readonly)

Returns the value of attribute scale.



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

def scale
  @scale
end

#secondary_codeObject

Returns the value of attribute secondary_code.



4
5
6
# File 'lib/unitwise/base.rb', line 4

def secondary_code
  @secondary_code
end

#symbolObject

Returns the value of attribute symbol.



4
5
6
# File 'lib/unitwise/base.rb', line 4

def symbol
  @symbol
end

Class Method Details

.allObject



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

def self.all
  @all ||= data.map{|d| self.new d }
end

.find(string) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/unitwise/base.rb', line 11

def self.find(string)
  [:primary_code, :secondary_code, :names, :slugs, :symbol].reduce(nil) do |m, method|
    if found = find_by(method, string)
      return found
    end
  end
end

.find_by(method, string) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/unitwise/base.rb', line 19

def self.find_by(method, string)
  self.all.find do |i|
    key = i.send(method)
    if key.respond_to?(:each)
      key.include?(string)
    else
      key == string
    end
  end
end

Instance Method Details

#slugsObject



40
41
42
# File 'lib/unitwise/base.rb', line 40

def slugs
  names.map(&:to_slug)
end