sequel-units 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b04f1f3670f5c69a64727468e9061c00859c0de5
4
- data.tar.gz: e551ec30fc463e77c51d8f6b0a5001f4243422b2
3
+ metadata.gz: 5515c1978e06ae6b1bebc2b5201f0321fbc1eb8d
4
+ data.tar.gz: baa3796f2a314d0669eef73c01fe9422457fd6bf
5
5
  SHA512:
6
- metadata.gz: 1cf74fb1f20ec3b10410675d6fc43c752308311c960276acc4f71653fb540f04dd21f0df60fb00985c74e59e8789f66c63a6b187880bdf3863600e6bdb673a91
7
- data.tar.gz: 5843bf27f62df539937a9b46d80f19cb2b913bac2e68cf66d1a7ba23cfcb22f0149ce53fad2377b72c0cd6b5663c7e3f0fa370842e6cbad6b479ea2e2c984a2a
6
+ metadata.gz: 55d223bb200ea28b7c73c993787dd07131608a671bd5c34e0f0857f00fd8a979a5ab0f4bd9203185b5ed7190e71dfc79e991cb1856df6c8d65b5e2483179793b
7
+ data.tar.gz: c1be598f4854a418f34cadbae87dd132a00eedf444f6a35b1eebea1c0a8ec5f5de5ea9225084285bd6cf102b99bef3b9c38570fec51ca0c8394b0ae53f9ef69d
data/README.md CHANGED
@@ -1,15 +1,20 @@
1
1
  # Sequel::Units
2
2
 
3
+ ![stability-wip](https://img.shields.io/badge/stability-work_in_progress-lightgrey.svg)
4
+ [![Build Status](https://travis-ci.org/AkihikoITOH/sequel-units.svg?branch=master)](https://travis-ci.org/AkihikoITOH/sequel-units)
5
+ [![Gem Version](https://badge.fury.io/rb/sequel-units.svg)](https://badge.fury.io/rb/sequel-units)
6
+ [![Dependency Status](https://gemnasium.com/badges/github.com/AkihikoITOH/sequel-units.svg)](https://gemnasium.com/github.com/AkihikoITOH/sequel-units)
7
+
3
8
  Sequel plugin for working with numeric values with unit.
4
9
 
5
10
  ## Basic usage
6
11
 
7
- Let's say you have a Sequel model `Product` which has a numeric attribute called `quantity` with unit (e.g. "10 kg").
12
+ Let's say you have a Sequel model `Order` which has a numeric attribute called `quantity` with unit (e.g. "10 kg").
8
13
 
9
14
  Your migration file would look like
10
15
 
11
16
  ```ruby
12
- DB.create_table(:products) do
17
+ DB.create_table(:orders) do
13
18
  primary_key :id
14
19
  column :quantity_scalar, Integer # "{attribute}_scalar" which stands for the scalar value ("10" in this case).
15
20
  column :quantity_unit, String # "{attribute}_unit" which stands for the unit ("kg" in this case).
@@ -19,30 +24,72 @@ end
19
24
  Your model definition then looks like
20
25
 
21
26
  ```ruby
22
- class Product < Sequel::Model
27
+ class Order < Sequel::Model
23
28
  plugin :units
24
29
 
25
30
  value_with_unit :quantity
26
31
  end
27
32
  ```
28
33
 
29
- Now instances of the model Product have an instance method `#quantity`.
34
+ Now instances of the model Order have an instance method `#quantity`.
30
35
 
31
36
  ```ruby
32
- product = Product.new(quantity_scalar: 10, quantity_unit: 'kg')
33
- # => #<Product @values={:quantity_scalar=>10, :quantity_unit=>"kg"}>
34
- quantity = product.quantity
37
+ order = Order.new(quantity_scalar: 10, quantity_unit: 'kg')
38
+ # => #<Order @values={:quantity_scalar=>10, :quantity_unit=>"kg"}>
39
+ order.quantity
35
40
  # => 10 kg
41
+ order.quantity(in_unit: 'gram')
42
+ # => 10000 gram
36
43
  ```
37
44
 
38
- Note `product.quantity` here is an instance of `RubyUnits::Unit`, so you can get the original scalar value or the unit
45
+ Note `order.quantity` here is an instance of `RubyUnits::Unit`, so you can get the original scalar value or the unit
39
46
  by calling methods `#scalar` or `#units`.
40
47
  See [Ruby Units](https://github.com/olbrich/ruby-units) for more details.
41
48
 
42
49
  ```ruby
43
- quantity.scalar
50
+ order.quantity.scalar
44
51
  # => 10
45
- quantity.units
52
+ order.quantity.units
46
53
  # => "kg"
47
54
  ```
48
55
 
56
+ ### Custom name for scalar and unit
57
+
58
+ You can specify your own favorite names for scalar and unit.
59
+
60
+ ```ruby
61
+ class Order < Sequel::Model
62
+ plugin :units
63
+
64
+ value_with_unit :quantity, scalar: :my_scalar, unit: :my_unit
65
+ end
66
+
67
+ order = Order.new(my_scalar: 10, my_unit: 'kg')
68
+ # => #<Order @values={:my_scalar=>10, :my_unit=>"kg"}>
69
+ order.quantity
70
+ # => 10 kg
71
+ ```
72
+
73
+ ### Inverse unit
74
+
75
+ Let's say you have a Sequel model `Product`.
76
+ You might want to have price with unit like "5 USD / kg" (price per weight),
77
+ but want to store the weight unit as "kg" instead of "1/kg", separate from the currency.
78
+
79
+ ```ruby
80
+ class Product < Sequel::Model
81
+ plugin :units
82
+
83
+ value_with_unit :price, inverse_unit: :weight_unit
84
+ end
85
+
86
+ product = Product.new(price_scalar: 5, price_unit: 'USD', weight_unit: 'kg')
87
+ # => #<Product @values={:price_scalar=>5, :price_unit=>"USD", :weight_unit=>"kg"}>
88
+ product.price
89
+ # => 5 USD/kg
90
+ ```
91
+
92
+ ## TODO
93
+
94
+ - [ ] Specify attributes of associated models.
95
+ - [ ] Define calculations with multiple attributes using [Unit Math](https://github.com/olbrich/ruby-units#unit-math)
@@ -2,11 +2,14 @@
2
2
 
3
3
  module Sequel::Plugins::Units
4
4
  module ClassMethods
5
- def value_with_unit(method_name)
6
- define_method method_name do
7
- scalar = _validate_scalar(send("#{method_name}_scalar".to_sym))
8
- unit = _validate_unit(send("#{method_name}_unit".to_sym))
9
- Unit.new("#{scalar} #{unit}")
5
+ def value_with_unit(method_name, scalar: nil, unit: nil, inverse_unit: nil)
6
+ define_method method_name do |in_unit: nil|
7
+ _generate_units(
8
+ scalar || "#{method_name}_scalar".to_sym,
9
+ unit || "#{method_name}_unit".to_sym,
10
+ inverse_unit,
11
+ in_unit
12
+ )
10
13
  end
11
14
  end
12
15
  end
@@ -4,6 +4,14 @@ module Sequel::Plugins::Units
4
4
  module InstanceMethods
5
5
  private
6
6
 
7
+ def _generate_units(scalar_method, unit_method, inverse_unit_method, target_unit)
8
+ scalar = _validate_scalar(send(scalar_method))
9
+ unit = _validate_unit(send(unit_method))
10
+ inverse_unit = inverse_unit_method ? "#{send(inverse_unit_method)}^-1" : nil
11
+ new_units = Unit.new("#{scalar} #{unit} #{inverse_unit}".strip)
12
+ target_unit ? new_units.convert_to(target_unit) : new_units
13
+ end
14
+
7
15
  def _validate_scalar(scalar)
8
16
  raise ArgumentError, "#{scalar} is not a numeric value" unless scalar.is_a?(Numeric)
9
17
  scalar
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-units
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akihiko Itoh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-14 00:00:00.000000000 Z
11
+ date: 2017-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel