sequel-units 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/lib/sequel/plugins/units/class_methods.rb +13 -0
- data/lib/sequel/plugins/units/instance_methods.rb +17 -0
- data/lib/sequel/plugins/units.rb +5 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b04f1f3670f5c69a64727468e9061c00859c0de5
|
4
|
+
data.tar.gz: e551ec30fc463e77c51d8f6b0a5001f4243422b2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1cf74fb1f20ec3b10410675d6fc43c752308311c960276acc4f71653fb540f04dd21f0df60fb00985c74e59e8789f66c63a6b187880bdf3863600e6bdb673a91
|
7
|
+
data.tar.gz: 5843bf27f62df539937a9b46d80f19cb2b913bac2e68cf66d1a7ba23cfcb22f0149ce53fad2377b72c0cd6b5663c7e3f0fa370842e6cbad6b479ea2e2c984a2a
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Akihiko Itoh
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Sequel::Units
|
2
|
+
|
3
|
+
Sequel plugin for working with numeric values with unit.
|
4
|
+
|
5
|
+
## Basic usage
|
6
|
+
|
7
|
+
Let's say you have a Sequel model `Product` which has a numeric attribute called `quantity` with unit (e.g. "10 kg").
|
8
|
+
|
9
|
+
Your migration file would look like
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
DB.create_table(:products) do
|
13
|
+
primary_key :id
|
14
|
+
column :quantity_scalar, Integer # "{attribute}_scalar" which stands for the scalar value ("10" in this case).
|
15
|
+
column :quantity_unit, String # "{attribute}_unit" which stands for the unit ("kg" in this case).
|
16
|
+
end
|
17
|
+
```
|
18
|
+
|
19
|
+
Your model definition then looks like
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class Product < Sequel::Model
|
23
|
+
plugin :units
|
24
|
+
|
25
|
+
value_with_unit :quantity
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
Now instances of the model Product have an instance method `#quantity`.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
product = Product.new(quantity_scalar: 10, quantity_unit: 'kg')
|
33
|
+
# => #<Product @values={:quantity_scalar=>10, :quantity_unit=>"kg"}>
|
34
|
+
quantity = product.quantity
|
35
|
+
# => 10 kg
|
36
|
+
```
|
37
|
+
|
38
|
+
Note `product.quantity` here is an instance of `RubyUnits::Unit`, so you can get the original scalar value or the unit
|
39
|
+
by calling methods `#scalar` or `#units`.
|
40
|
+
See [Ruby Units](https://github.com/olbrich/ruby-units) for more details.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
quantity.scalar
|
44
|
+
# => 10
|
45
|
+
quantity.units
|
46
|
+
# => "kg"
|
47
|
+
```
|
48
|
+
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sequel::Plugins::Units
|
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}")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sequel::Plugins::Units
|
4
|
+
module InstanceMethods
|
5
|
+
private
|
6
|
+
|
7
|
+
def _validate_scalar(scalar)
|
8
|
+
raise ArgumentError, "#{scalar} is not a numeric value" unless scalar.is_a?(Numeric)
|
9
|
+
scalar
|
10
|
+
end
|
11
|
+
|
12
|
+
def _validate_unit(unit)
|
13
|
+
raise ArgumentError, "#{unit} is not a string" unless unit.is_a?(String)
|
14
|
+
unit
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-units
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Akihiko Itoh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: ruby-units
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.50'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.50'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.5'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.5'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.15'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.15'
|
111
|
+
description: Sequel::Units provides a simple way to work with numeric values with
|
112
|
+
units in Sequel models.
|
113
|
+
email:
|
114
|
+
- akihiko.mus@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- LICENSE.txt
|
120
|
+
- README.md
|
121
|
+
- lib/sequel/plugins/units.rb
|
122
|
+
- lib/sequel/plugins/units/class_methods.rb
|
123
|
+
- lib/sequel/plugins/units/instance_methods.rb
|
124
|
+
homepage: https://github.com/AkihikoITOH/sequel-units
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 2.6.11
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: Sequel plugin for working with numeric values with unit
|
148
|
+
test_files: []
|