Class: RubyUnits::Unit::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_units/definition.rb

Overview

Handle the definition of units

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_name, _definition = []) {|_self| ... } ⇒ Definition

Returns a new instance of Definition.

Examples:

Raw definition from a hash

Unit::Definition.new("rack-unit",[%w{U rack-U}, (6405920109971793/144115188075855872), :length, %w{<meter>} ])

Block form

Unit::Definition.new("rack-unit") do |unit|
  unit.aliases = %w{U rack-U}
  unit.definition = Unit("7/4 inches")
end

Yields:

  • (_self)

Yield Parameters:



33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_units/definition.rb', line 33

def initialize(_name, _definition = [], &block)
  yield self if block_given?
  self.name     ||= _name.gsub(/[<>]/,'')
  @aliases      ||= (_definition[0] || [_name])
  @scalar       ||= _definition[1]
  @kind         ||= _definition[2]
  @numerator    ||= _definition[3] || RubyUnits::Unit::UNITY_ARRAY
  @denominator  ||= _definition[4] || RubyUnits::Unit::UNITY_ARRAY
  @display_name ||= @aliases.first
end

Instance Attribute Details

#aliasesArray

alias array must contain the name of the unit and entries must be unique

Returns:



61
62
63
# File 'lib/ruby_units/definition.rb', line 61

def aliases
  [[@aliases], @name].flatten.compact.uniq
end

#denominatorArray

Returns:



19
20
21
# File 'lib/ruby_units/definition.rb', line 19

def denominator
  @denominator
end

#display_nameString

Returns:



22
23
24
# File 'lib/ruby_units/definition.rb', line 22

def display_name
  @display_name
end

#kindSymbol

Returns:

  • (Symbol)


10
11
12
# File 'lib/ruby_units/definition.rb', line 10

def kind
  @kind
end

#numeratorArray

Returns:



16
17
18
# File 'lib/ruby_units/definition.rb', line 16

def numerator
  @numerator
end

#scalarNumeric

Returns:



13
14
15
# File 'lib/ruby_units/definition.rb', line 13

def scalar
  @scalar
end

Instance Method Details

#base?Boolean

is this a base unit? units are base units if the scalar is one, and the unit is defined in terms of itself.

Returns:

  • (Boolean)


92
93
94
95
96
97
98
# File 'lib/ruby_units/definition.rb', line 92

def base?
  (self.denominator     == RubyUnits::Unit::UNITY_ARRAY) &&
  (self.numerator       != RubyUnits::Unit::UNITY_ARRAY) &&
  (self.numerator.size  == 1) &&
  (self.scalar          == 1) &&
  (self.numerator.first == self.name)
end

#definition=(unit) ⇒ Unit::Definition

define a unit in terms of another unit

Parameters:

Returns:



68
69
70
71
72
73
74
75
# File 'lib/ruby_units/definition.rb', line 68

def definition=(unit)
  _base         = unit.to_base
  @scalar       = _base.scalar
  @kind         = _base.kind
  @numerator    = _base.numerator
  @denominator  = _base.denominator
  self
end

#nameString?

TODO:

refactor Unit and Unit::Definition so we don’t need to wrap units with angle brackets

name of the unit nil if name is not set, adds ‘<’ and ‘>’ around the name

Returns:



48
49
50
# File 'lib/ruby_units/definition.rb', line 48

def name
  "<#{@name}>" if (defined?(@name) && @name)
end

#name=(_name) ⇒ String

set the name, strip off ‘<’ and ‘>’

Parameters:

Returns:



55
56
57
# File 'lib/ruby_units/definition.rb', line 55

def name=(_name)
  @name = _name.gsub(/[<>]/,'')
end

#prefix?Boolean

is this definition for a prefix?

Returns:

  • (Boolean)


79
80
81
# File 'lib/ruby_units/definition.rb', line 79

def prefix?
  self.kind == :prefix
end

#unity?Boolean

Is this definition the unity definition?

Returns:

  • (Boolean)


85
86
87
# File 'lib/ruby_units/definition.rb', line 85

def unity?
  self.prefix? && self.scalar == 1
end