quantified 1.0.1 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 84ba6ca1ebcb53b0a7bae563f6c7f9d899bcad86
4
- data.tar.gz: 93d065832067c0c4c5d6a80867d6b90fcfd11958
3
+ metadata.gz: 323eacb30dcac97d216273b87fea6785af0507ff
4
+ data.tar.gz: 7d7d054c4efb68f54e1780db5356948d372fce45
5
5
  SHA512:
6
- metadata.gz: 0d1ad95fbed7c90460f6d3cfec4f5f11c2003abfa593a24571f7007c056112db723f5f969032a57628361ace8a096789936defaa79710c8d5709e3fe11056d5c
7
- data.tar.gz: f502a30054914e6ed2f9b805cf7260db8932ec4ab78cee9d993f552e243362fd25d450b9302c900565455e59bf438cde2a2b7368c11a8b82b8c4f0c702e758b9
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
@@ -1,4 +1,5 @@
1
1
  require 'quantified/version'
2
+ require 'quantified/coder'
2
3
  require 'quantified/attribute'
3
4
  require 'quantified/mass'
4
5
  require 'quantified/length'
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Quantified
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -20,5 +20,5 @@ Gem::Specification.new do |s|
20
20
  s.files = `git ls-files`.split($/)
21
21
  s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
22
22
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
23
- s.require_path = ['lib']
23
+ s.require_paths = ['lib']
24
24
  end
@@ -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
@@ -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
@@ -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.1
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-05-01 00:00:00.000000000 Z
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
- - - lib
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.0.14
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