value_object_ar 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/methods.rb CHANGED
@@ -20,9 +20,11 @@ module ValueObjectAR
20
20
  end
21
21
 
22
22
  def define_value_object_ar_writer(attribute, klass)
23
- define_method("#{attribute}=") do |value|
24
- if value.is_a?(klass)
25
- value = value.send(attribute)
23
+ define_method("#{attribute}=") do |value_or_object|
24
+ value = if value_or_object.is_a?(klass)
25
+ value_or_object.value
26
+ else
27
+ value_or_object
26
28
  end
27
29
  attributes[attribute.to_s] = value
28
30
  end
@@ -0,0 +1,56 @@
1
+ require "test/unit"
2
+ require_relative "../lib/value_object_ar"
3
+
4
+ class TestValueObjectAr < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @product = Product.new(currency: 'DKK')
8
+ end
9
+
10
+ def test_call_methods_on_value_object
11
+ assert_equal @product.currency.hipstier_than_bitcoin?, "DKK? Probably not"
12
+ end
13
+
14
+ def test_set_with_value
15
+ @product.currency = 'EUR'
16
+ assert_equal @product.currency.hipstier_than_bitcoin?, "EUR? Probably not"
17
+ end
18
+
19
+ def test_set_with_value_object
20
+ @product.currency = CurrencyValueObject.new('USD')
21
+ assert_equal @product.currency.hipstier_than_bitcoin?, "USD? Probably not"
22
+ end
23
+
24
+ end
25
+
26
+ class CurrencyValueObject
27
+
28
+ attr_accessor :value
29
+
30
+ def initialize(value)
31
+ @value = value
32
+ end
33
+
34
+ def hipstier_than_bitcoin?
35
+ "#{value}? Probably not"
36
+ end
37
+
38
+ end
39
+
40
+ class Product
41
+
42
+ include ValueObjectAR::Methods
43
+ value_object :currency, CurrencyValueObject
44
+
45
+ # ActiveRecord behaviour below:
46
+ attr_accessor :attributes
47
+
48
+ def initialize(options={})
49
+ options.each {|k,v| self.public_send("#{k}=",v)}
50
+ end
51
+
52
+ def attributes
53
+ @attributes ||= {'currency' => nil}
54
+ end
55
+
56
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: value_object_ar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -51,6 +51,7 @@ extra_rdoc_files: []
51
51
  files:
52
52
  - lib/methods.rb
53
53
  - lib/value_object_ar.rb
54
+ - test/value_object_ar_test.rb
54
55
  homepage: http://github.com/kasperbn/value_object_ar
55
56
  licenses: []
56
57
  post_install_message:
@@ -76,4 +77,5 @@ rubygems_version: 1.8.25
76
77
  signing_key:
77
78
  specification_version: 3
78
79
  summary: Value objects for your active models
79
- test_files: []
80
+ test_files:
81
+ - test/value_object_ar_test.rb