Class: CommerceUnits::Dimension
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CommerceUnits::Dimension
- Defined in:
- lib/commerce_units/dimension.rb
Overview
Schema Information
Table name: commerce_units_dimensions
id :integer not null, primary key
root_dimension :string(255) not null
unit_name :string(255) not null
multiply_constant :decimal(15, 5) default(1.0), not null
unitary_role :string(255) default("tertiary"), not null
created_at :datetime
updated_at :datetime
Defined Under Namespace
Classes: NoPrimaryUnit
Constant Summary collapse
- KnownRoots =
[:money, :mass]
Class Method Summary collapse
- .create_and_consider_making_primary!(param_hash) ⇒ Object
- .find_or_create_and_consider_making_primary!(param_hash) ⇒ Object
- .from_array_of_unit_names!(names) ⇒ Object
Instance Method Summary collapse
- #make_primary! ⇒ Object
- #to_converter_unit ⇒ Object
- #to_converter_value ⇒ Object
- #to_unit ⇒ Object
- #to_value ⇒ Object
Class Method Details
.create_and_consider_making_primary!(param_hash) ⇒ Object
37 38 39 40 41 |
# File 'lib/commerce_units/dimension.rb', line 37 def create_and_consider_making_primary!(param_hash) dimension = create! param_hash dimension.make_primary! if primary_unit_by_roots(dimension.root_dimension).blank? dimension end |
.find_or_create_and_consider_making_primary!(param_hash) ⇒ Object
43 44 45 |
# File 'lib/commerce_units/dimension.rb', line 43 def find_or_create_and_consider_making_primary!(param_hash) where(param_hash).first || create_and_consider_making_primary!(param_hash) end |
.from_array_of_unit_names!(names) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/commerce_units/dimension.rb', line 28 def from_array_of_unit_names!(names) result = where unit_name: names unless result.count == names.count unfound_names = names.map(&:to_s) - result.map(&:unit_name) raise ActiveRecord::RecordNotFound, "I don't know the following units: #{unfound_names} out of #{names}" end result end |
Instance Method Details
#make_primary! ⇒ Object
75 76 77 78 79 |
# File 'lib/commerce_units/dimension.rb', line 75 def make_primary! _shift_primary! if self.class.primary_unit_by_roots(root_dimension).present? _make_primary! self end |
#to_converter_unit ⇒ Object
48 49 50 51 52 53 |
# File 'lib/commerce_units/dimension.rb', line 48 def to_converter_unit CommerceUnits::Unit.new.tap do |u| u.numerator = [_primary_root_unit_name] u.denominator = [unit_name] end end |
#to_converter_value ⇒ Object
61 62 63 64 65 66 |
# File 'lib/commerce_units/dimension.rb', line 61 def to_converter_value CommerceUnits::Value.new.tap do |v| v.number = multiply_constant v.unit = to_converter_unit end end |
#to_unit ⇒ Object
55 56 57 58 59 |
# File 'lib/commerce_units/dimension.rb', line 55 def to_unit CommerceUnits::Unit.new.tap do |u| u.numerator = [unit_name] end end |
#to_value ⇒ Object
68 69 70 71 72 73 |
# File 'lib/commerce_units/dimension.rb', line 68 def to_value CommerceUnits::Value.new.tap do |v| v.number = multiply_constant v.unit = to_unit end end |