Class: SimpleUnits::Composite

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_units/composite.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_context(context) ⇒ Object



5
6
7
8
9
# File 'lib/simple_units/composite.rb', line 5

def from_context(context)
  new.tap do |composite|
    composite.append_numerators! context
  end
end

.from_string(string) ⇒ Object



11
12
13
# File 'lib/simple_units/composite.rb', line 11

def from_string(string)
  from_context SimpleUnits::Context.get_context string
end

Instance Method Details

#*(other) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/simple_units/composite.rb', line 86

def *(other)
  array = other.denominators.to_a + self.denominators.to_a + other.numerators.to_a + self.numerators.to_a
  self.class.new.tap do |composite|
    array.each do |name, hash|
      composite.modify_context_by_order!(hash[:context], hash[:order])
    end
    composite.simplify!
  end
end

#==(other) ⇒ Object



96
97
98
# File 'lib/simple_units/composite.rb', line 96

def ==(other)
  to_str == other.to_str
end

#append_denominator!(context) ⇒ Object



30
31
32
# File 'lib/simple_units/composite.rb', line 30

def append_denominator!(context)
  modify_context_by_order!(context, -1)
end

#append_denominators!(*contexts) ⇒ Object



21
22
23
24
# File 'lib/simple_units/composite.rb', line 21

def append_denominators!(*contexts)
  contexts.each { |context| append_denominator! context }
  simplify!
end

#append_numerator!(context) ⇒ Object



26
27
28
# File 'lib/simple_units/composite.rb', line 26

def append_numerator!(context)
  modify_context_by_order!(context, 1)
end

#append_numerators!(*contexts) ⇒ Object



16
17
18
19
# File 'lib/simple_units/composite.rb', line 16

def append_numerators!(*contexts)
  contexts.each { |context| append_numerator! context }
  simplify!
end

#denominatorsObject



50
51
52
53
54
# File 'lib/simple_units/composite.rb', line 50

def denominators
  @contexts_hash.reject do |name, hash|
    hash[:order] > 0
  end
end

#inspectorObject



70
71
72
73
74
# File 'lib/simple_units/composite.rb', line 70

def inspector
  if @contexts_hash.keys.count == 1 
    @contexts_hash[@contexts_hash.keys.first][:context].inspector
  end
end

#inverseObject



57
58
59
60
61
62
# File 'lib/simple_units/composite.rb', line 57

def inverse
  self.class.new.tap do |composite|
    (numerators.to_a + denominators.to_a).each { |name, hash| composite.modify_context_by_order!(hash[:context], -hash[:order]) }
    composite.simplify!
  end
end

#modify_context_by_order!(context, order) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/simple_units/composite.rb', line 34

def modify_context_by_order!(context, order)
  @contexts_hash ||= {}
  if @contexts_hash[context.name.to_s].nil? 
    @contexts_hash[context.name.to_s] = { :context => context, :order => order }
  else
    @contexts_hash[context.name.to_s][:order] += order
  end
end

#numeratorsObject



44
45
46
47
48
# File 'lib/simple_units/composite.rb', line 44

def numerators
  @contexts_hash.reject do |name, hash|
    hash[:order] < 0
  end
end

#simplify!Object



64
65
66
67
68
# File 'lib/simple_units/composite.rb', line 64

def simplify!
  @contexts_hash.reject! do |name, hash|
    hash[:order].zero?
  end
end

#to_sObject



84
# File 'lib/simple_units/composite.rb', line 84

def to_s; to_str; end

#to_strObject



76
77
78
79
80
81
82
# File 'lib/simple_units/composite.rb', line 76

def to_str
  @contexts_hash.to_a.sort { |a,b| a.first <=> b.first }.inject("") do |str, package|
    order = package.last[:order]
    name = package.first
    str + "(#{name}^#{order})"
  end.gsub("^1", "")
end