Module: Eng::Unit
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from UnitCalc
calc_unit, multi_div_unit, parenthesis_unit, plus_minus_split, plus_minus_unit, split_si_unit, unit_arrange
Methods included from Formula
liner_function, new_liner_function, operator_reg, reverse_operator, split_by_ari, split_value_unit
Instance Attribute Details
#opt ⇒ Object
Returns the value of attribute opt.
57
58
59
|
# File 'lib/eng_unit.rb', line 57
def opt
@opt
end
|
Class Method Details
.convert_to_si(value:, unit:, opt: nil) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/eng_unit.rb', line 130
def convert_to_si(value: , unit: , opt: nil)
value = value.rationalize
metric = nil
num = 1
@opt = opt
original_unit = unit
unit = unit.send(opt)
if unit.match(to_si_reg) do |si_unit|
break if si_unit[0] != unit || si_unit[0].upcase == "KG"
metric = si_unit[:metric]
unit = si_unit[:si] + (si_unit[:num] || "")
num = si_unit[:num] || 1
end
end
result = find_all(unit)
if !result && !opt
convert_to_si(value: value, unit: unit, opt: :upcase)
elsif result
@opt = nil
metric = metric ? (metric_prefix[original_unit[0]].to_f ** num.to_f).rationalize : 1
{
value: eval(result[:formula].to_s + "*#{metric}").to_f,
unit: to_si(result[:kind]),
kind: result[:kind],
error: opt ? { unit_upcase: original_unit } : nil
}
else
{
value: value,
unit: original_unit,
kind: nil,
error: original_unit.empty? ? nil : { unit_not_found: original_unit }
}
end
end
|
.find_all(unit) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/eng_unit.rb', line 122
def find_all(unit)
[:si_base, :si_alter, :si_derived, :variable].each do |method|
result = Unit.send(method).to_find(unit)
return result if result
end
nil
end
|
.kind_by_si(unit) ⇒ Object
167
168
169
170
171
172
173
174
|
# File 'lib/eng_unit.rb', line 167
def kind_by_si(unit)
s_unit = unit_arrange(multi_div_unit(split_by_ari(unit)))
a = []
si_base.merge(si_derived).each do |e_unit|
a << e_unit[0] if s_unit == unit_arrange(multi_div_unit(split_by_ari(e_unit[1])))
end
a || false
end
|
.metric_prefix ⇒ Object
110
111
112
113
114
|
# File 'lib/eng_unit.rb', line 110
def metric_prefix
@metric_prefix ||= YAML.load_file(File.join(__dir__, 'unit_calc/metric_prefix.yml'))
return @metric_prefix.inject({}){ |h, (k,v)| h[k.upcase] = v; h } if @opt
@metric_prefix
end
|
.si_alter ⇒ Object
116
117
118
119
120
|
# File 'lib/eng_unit.rb', line 116
def si_alter
@si_alter_unit ||= YAML.load_file(File.join(__dir__, 'unit_calc/si_alter_unit.yml'))
return @si_alter_unit.inject({}){ |h, (k,v)| h[k] = v.inject([]){ |a, aa| a << aa.upcase; a }; h } if @opt
@si_alter_unit
end
|
.si_base ⇒ Object
92
93
94
95
96
|
# File 'lib/eng_unit.rb', line 92
def si_base
@si_base_unit ||= YAML.load_file(File.join(__dir__, 'unit_calc/si_base_unit.yml'))
return @si_base_unit.inject({}){ |h, (k,v)| h[k] = v.upcase; h } if @opt
@si_base_unit
end
|
.si_derived ⇒ Object
98
99
100
101
102
|
# File 'lib/eng_unit.rb', line 98
def si_derived
@si_derived_unit ||= YAML.load_file(File.join(__dir__, 'unit_calc/si_derived_unit.yml'))
return @si_derived_unit.inject({}){ |h, (k,v)| h[k] = v.upcase; h } if @opt
@si_derived_unit
end
|
.to_si(kind_unit) ⇒ Object
176
177
178
|
# File 'lib/eng_unit.rb', line 176
def to_si(kind_unit)
si_base[kind_unit] || si_derived[kind_unit]
end
|
.to_si_reg ⇒ Object
180
181
182
|
# File 'lib/eng_unit.rb', line 180
def to_si_reg
/((?<metric>#{metric_prefix.to_reg})(?<si>#{si_base.to_reg}|g){1}(?<num>-*\d)*)|((?<metric>#{metric_prefix.to_reg})(?<si>#{si_alter.to_reg}))/
end
|
.unit_reg ⇒ Object
184
185
186
|
# File 'lib/eng_unit.rb', line 184
def unit_reg
/(#{si_derived.to_reg})|((?:#{metric_prefix.to_reg})?#{si_alter.to_reg})|(#{variable.to_reg})|((?:#{metric_prefix.to_reg})?(?:#{si_base.to_reg}|g){1}-*\d*)|(#{operator_reg(:ari)})/
end
|
.variable ⇒ Object
104
105
106
107
108
|
# File 'lib/eng_unit.rb', line 104
def variable
@variable_unit ||= YAML.load_file(File.join(__dir__, 'unit_calc/variable_unit.yml'))
return @variable_unit.inject({}){ |h, (k,v)| h[k] = v.inject({}){ |hh, (kk, vv)| hh[kk.upcase] = vv; hh }; h } if @opt
@variable_unit
end
|
.variable_names ⇒ Object
188
189
190
191
192
193
|
# File 'lib/eng_unit.rb', line 188
def variable_names
variable.inject({}) do |a, k|
a[k[0]] = k[1].keys.join(", ").split(", ")
a
end
end
|
Instance Method Details
#convert(value:, unit:) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/eng_unit.rb', line 59
def convert(value: , unit: )
values = nil
first_unit = false
convert_unit = unit.split(/(\*|\/)/).map do |e_unit|
if e_unit.empty?
elsif e_unit =~ /(\*|\/)/
values = e_unit
if e_unit == "/" && !first_unit
values = value.to_s + "/"
first_unit = true
end
{ value: values, unit: e_unit }
else
values = first_unit ? 1 : value
first_unit = true
Unit.convert_to_si(value: values.to_f.rationalize, unit: e_unit)
end
end
results = convert_unit.compact.inject({ value: "", unit: [], error: [] }) do |result, convert|
result[:value] << convert[:value].to_s
result[:unit] << split_by_ari(convert[:unit]) if convert[:unit]
result[:error] << convert[:error] if convert[:error]
result
end
results[:value] = eval(results[:value] << "")
results[:unit].flatten!.reject!(&:empty?)
results
end
|