quantified 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -0
- data/lib/quantified.rb +1 -0
- data/lib/quantified/attribute.rb +13 -0
- data/lib/quantified/coder.rb +23 -0
- data/lib/quantified/version.rb +1 -1
- data/quantified.gemspec +1 -1
- data/test/coder_test.rb +30 -0
- data/test/length_test.rb +5 -0
- data/test/mass_test.rb +5 -0
- metadata +15 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 323eacb30dcac97d216273b87fea6785af0507ff
|
4
|
+
data.tar.gz: 7d7d054c4efb68f54e1780db5356948d372fce45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83b6d4f9e5c87663eea61d7b5429030ec49f38beb315792f9ddc190f5673dfd7508e3be8e0f9f943175a79ada346af5954f03f76d6ee8452f79bbdf1e870bdf0
|
7
|
+
data.tar.gz: c3859e3f30b9d83a4212dedc4443059a1e99637b1d381db7fd9bdaf6d5cdb7572f8ed0a9123bcdad47557c1c4e4f64980b85a3e6b661aa2dea970abd58f85ce6
|
data/README.md
CHANGED
@@ -46,3 +46,11 @@ You can easily define new attributes. Here's length.rb:
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
49
|
+
|
50
|
+
You can add a custom coder to an ActiveRecord model to deserialize attributes:
|
51
|
+
|
52
|
+
class Package < ActiveRecord::Base
|
53
|
+
serialize :weight, Quantified::Mass.ounces
|
54
|
+
serialize :length, Quantified::Length.inches
|
55
|
+
serialize :width, Quantified::Length.inches
|
56
|
+
end
|
data/lib/quantified.rb
CHANGED
data/lib/quantified/attribute.rb
CHANGED
@@ -102,6 +102,10 @@ module Quantified
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
105
|
+
def self.coder(unit)
|
106
|
+
Coder.new(self, unit)
|
107
|
+
end
|
108
|
+
|
105
109
|
protected
|
106
110
|
|
107
111
|
class << self
|
@@ -178,6 +182,7 @@ module Quantified
|
|
178
182
|
|
179
183
|
def self.add_methods_for(sym, options = {})
|
180
184
|
add_conversion_method_for(sym, options)
|
185
|
+
add_coder_method_for(sym, options)
|
181
186
|
add_numeric_method = if options.has_key?(:add_numeric_methods)
|
182
187
|
options[:add_numeric_methods]
|
183
188
|
else
|
@@ -197,6 +202,14 @@ module Quantified
|
|
197
202
|
end
|
198
203
|
end
|
199
204
|
|
205
|
+
def self.add_coder_method_for(sym, options = {})
|
206
|
+
(class << self; self; end).instance_eval do
|
207
|
+
define_method(sym) do
|
208
|
+
Coder.new(self, sym)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
200
213
|
def self.add_numeric_method_for(unit_name, options = {})
|
201
214
|
unit_name = unit_name.to_sym
|
202
215
|
raise ArgumentError, "#{unit_name.inspect} is not a unit in #{name}" unless units.include?(unit_name)
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Quantified
|
2
|
+
class Coder
|
3
|
+
attr_reader :attribute, :unit
|
4
|
+
|
5
|
+
def initialize(attribute, unit)
|
6
|
+
@attribute = attribute
|
7
|
+
@unit = unit
|
8
|
+
end
|
9
|
+
|
10
|
+
def load(amount)
|
11
|
+
attribute.new(amount, unit) unless amount.nil?
|
12
|
+
end
|
13
|
+
|
14
|
+
def dump(quantity)
|
15
|
+
conversion = "to_#{unit}"
|
16
|
+
if quantity.respond_to?(conversion)
|
17
|
+
quantity.send(conversion)
|
18
|
+
else
|
19
|
+
quantity
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/quantified/version.rb
CHANGED
data/quantified.gemspec
CHANGED
data/test/coder_test.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'quantified'
|
3
|
+
|
4
|
+
class CoderTest < Test::Unit::TestCase
|
5
|
+
include Quantified
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@coder = Coder.new(Length, :meters)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_load_nil
|
12
|
+
assert @coder.load(nil).nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_load_numeric
|
16
|
+
assert_equal @coder.load(5), Length.new(5, :meters)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_dump_nil
|
20
|
+
assert @coder.dump(nil).nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_dump_conversion
|
24
|
+
assert_equal @coder.dump(Length.new(500, :centimeters)), 5
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_dump_numeric
|
28
|
+
assert_equal @coder.dump(5), 5
|
29
|
+
end
|
30
|
+
end
|
data/test/length_test.rb
CHANGED
@@ -91,4 +91,9 @@ class LengthTest < Test::Unit::TestCase
|
|
91
91
|
assert_equal :metric, 2.centimetres.system
|
92
92
|
assert_equal :imperial, 2.feet.system
|
93
93
|
end
|
94
|
+
|
95
|
+
def test_coder
|
96
|
+
assert_equal :centimetres, Length.centimetres.unit
|
97
|
+
assert_equal Length, Length.centimetres.attribute
|
98
|
+
end
|
94
99
|
end
|
data/test/mass_test.rb
CHANGED
@@ -86,6 +86,11 @@ class MassTest < Test::Unit::TestCase
|
|
86
86
|
assert_equal [:ounces, :oz, :pounds, :lb, :stones, :st, :short_tons], Mass.units(:imperial)
|
87
87
|
end
|
88
88
|
|
89
|
+
def test_coder
|
90
|
+
assert_equal :grams, Mass.grams.unit
|
91
|
+
assert_equal Mass, Mass.grams.attribute
|
92
|
+
end
|
93
|
+
|
89
94
|
def test_right_side_comparison_with_fixnum
|
90
95
|
assert Mass.new(14, :grams) < 20
|
91
96
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quantified
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James MacAulay
|
@@ -9,34 +9,34 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-12-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- -
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '0'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- -
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: test-unit
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- -
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '0'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- -
|
39
|
+
- - ">="
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
42
|
description: Pretty quantifiable measurements which feel like ActiveSupport::Duration.
|
@@ -46,18 +46,20 @@ executables: []
|
|
46
46
|
extensions: []
|
47
47
|
extra_rdoc_files: []
|
48
48
|
files:
|
49
|
-
- .gitignore
|
50
|
-
- .travis.yml
|
49
|
+
- ".gitignore"
|
50
|
+
- ".travis.yml"
|
51
51
|
- Gemfile
|
52
52
|
- MIT-LICENSE
|
53
53
|
- README.md
|
54
54
|
- Rakefile
|
55
55
|
- lib/quantified.rb
|
56
56
|
- lib/quantified/attribute.rb
|
57
|
+
- lib/quantified/coder.rb
|
57
58
|
- lib/quantified/length.rb
|
58
59
|
- lib/quantified/mass.rb
|
59
60
|
- lib/quantified/version.rb
|
60
61
|
- quantified.gemspec
|
62
|
+
- test/coder_test.rb
|
61
63
|
- test/length_test.rb
|
62
64
|
- test/mass_test.rb
|
63
65
|
- test/test_helper.rb
|
@@ -68,24 +70,25 @@ metadata: {}
|
|
68
70
|
post_install_message:
|
69
71
|
rdoc_options: []
|
70
72
|
require_paths:
|
71
|
-
-
|
73
|
+
- lib
|
72
74
|
required_ruby_version: !ruby/object:Gem::Requirement
|
73
75
|
requirements:
|
74
|
-
- -
|
76
|
+
- - ">="
|
75
77
|
- !ruby/object:Gem::Version
|
76
78
|
version: '0'
|
77
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
80
|
requirements:
|
79
|
-
- -
|
81
|
+
- - ">="
|
80
82
|
- !ruby/object:Gem::Version
|
81
83
|
version: '0'
|
82
84
|
requirements: []
|
83
85
|
rubyforge_project:
|
84
|
-
rubygems_version: 2.
|
86
|
+
rubygems_version: 2.2.3
|
85
87
|
signing_key:
|
86
88
|
specification_version: 4
|
87
89
|
summary: Pretty quantifiable measurements which feel like ActiveSupport::Duration.
|
88
90
|
test_files:
|
91
|
+
- test/coder_test.rb
|
89
92
|
- test/length_test.rb
|
90
93
|
- test/mass_test.rb
|
91
94
|
- test/test_helper.rb
|