active_data 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.rspec +0 -1
  4. data/.rvmrc +1 -1
  5. data/.travis.yml +13 -6
  6. data/Appraisals +7 -0
  7. data/Gemfile +1 -5
  8. data/Guardfile +68 -15
  9. data/README.md +144 -2
  10. data/active_data.gemspec +19 -11
  11. data/gemfiles/rails.4.0.gemfile +14 -0
  12. data/gemfiles/rails.4.1.gemfile +14 -0
  13. data/gemfiles/rails.4.2.gemfile +14 -0
  14. data/gemfiles/rails.5.0.gemfile +14 -0
  15. data/lib/active_data.rb +120 -3
  16. data/lib/active_data/active_record/associations.rb +50 -0
  17. data/lib/active_data/active_record/nested_attributes.rb +24 -0
  18. data/lib/active_data/config.rb +40 -0
  19. data/lib/active_data/errors.rb +93 -0
  20. data/lib/active_data/extensions.rb +33 -0
  21. data/lib/active_data/model.rb +16 -74
  22. data/lib/active_data/model/associations.rb +84 -15
  23. data/lib/active_data/model/associations/base.rb +79 -0
  24. data/lib/active_data/model/associations/collection/embedded.rb +12 -0
  25. data/lib/active_data/model/associations/collection/proxy.rb +32 -0
  26. data/lib/active_data/model/associations/collection/referenced.rb +26 -0
  27. data/lib/active_data/model/associations/embeds_many.rb +124 -18
  28. data/lib/active_data/model/associations/embeds_one.rb +90 -15
  29. data/lib/active_data/model/associations/nested_attributes.rb +180 -0
  30. data/lib/active_data/model/associations/references_many.rb +96 -0
  31. data/lib/active_data/model/associations/references_one.rb +83 -0
  32. data/lib/active_data/model/associations/reflections/base.rb +100 -0
  33. data/lib/active_data/model/associations/reflections/embeds_many.rb +25 -0
  34. data/lib/active_data/model/associations/reflections/embeds_one.rb +49 -0
  35. data/lib/active_data/model/associations/reflections/reference_reflection.rb +45 -0
  36. data/lib/active_data/model/associations/reflections/references_many.rb +28 -0
  37. data/lib/active_data/model/associations/reflections/references_one.rb +28 -0
  38. data/lib/active_data/model/associations/validations.rb +63 -0
  39. data/lib/active_data/model/attributes.rb +247 -0
  40. data/lib/active_data/model/attributes/attribute.rb +73 -0
  41. data/lib/active_data/model/attributes/base.rb +116 -0
  42. data/lib/active_data/model/attributes/collection.rb +17 -0
  43. data/lib/active_data/model/attributes/dictionary.rb +26 -0
  44. data/lib/active_data/model/attributes/localized.rb +42 -0
  45. data/lib/active_data/model/attributes/reference_many.rb +21 -0
  46. data/lib/active_data/model/attributes/reference_one.rb +42 -0
  47. data/lib/active_data/model/attributes/reflections/attribute.rb +55 -0
  48. data/lib/active_data/model/attributes/reflections/base.rb +62 -0
  49. data/lib/active_data/model/attributes/reflections/collection.rb +10 -0
  50. data/lib/active_data/model/attributes/reflections/dictionary.rb +13 -0
  51. data/lib/active_data/model/attributes/reflections/localized.rb +43 -0
  52. data/lib/active_data/model/attributes/reflections/reference_many.rb +10 -0
  53. data/lib/active_data/model/attributes/reflections/reference_one.rb +58 -0
  54. data/lib/active_data/model/attributes/reflections/represents.rb +55 -0
  55. data/lib/active_data/model/attributes/represents.rb +64 -0
  56. data/lib/active_data/model/callbacks.rb +71 -0
  57. data/lib/active_data/model/conventions.rb +35 -0
  58. data/lib/active_data/model/dirty.rb +77 -0
  59. data/lib/active_data/model/lifecycle.rb +307 -0
  60. data/lib/active_data/model/localization.rb +21 -0
  61. data/lib/active_data/model/persistence.rb +57 -0
  62. data/lib/active_data/model/primary.rb +51 -0
  63. data/lib/active_data/model/scopes.rb +77 -0
  64. data/lib/active_data/model/validations.rb +27 -0
  65. data/lib/active_data/model/validations/associated.rb +19 -0
  66. data/lib/active_data/model/validations/nested.rb +39 -0
  67. data/lib/active_data/railtie.rb +7 -0
  68. data/lib/active_data/version.rb +1 -1
  69. data/spec/lib/active_data/active_record/associations_spec.rb +149 -0
  70. data/spec/lib/active_data/active_record/nested_attributes_spec.rb +16 -0
  71. data/spec/lib/active_data/config_spec.rb +44 -0
  72. data/spec/lib/active_data/model/associations/embeds_many_spec.rb +362 -52
  73. data/spec/lib/active_data/model/associations/embeds_one_spec.rb +250 -31
  74. data/spec/lib/active_data/model/associations/nested_attributes_spec.rb +23 -0
  75. data/spec/lib/active_data/model/associations/references_many_spec.rb +196 -0
  76. data/spec/lib/active_data/model/associations/references_one_spec.rb +134 -0
  77. data/spec/lib/active_data/model/associations/reflections/embeds_many_spec.rb +144 -0
  78. data/spec/lib/active_data/model/associations/reflections/embeds_one_spec.rb +116 -0
  79. data/spec/lib/active_data/model/associations/reflections/references_many_spec.rb +255 -0
  80. data/spec/lib/active_data/model/associations/reflections/references_one_spec.rb +208 -0
  81. data/spec/lib/active_data/model/associations/validations_spec.rb +153 -0
  82. data/spec/lib/active_data/model/associations_spec.rb +189 -0
  83. data/spec/lib/active_data/model/attributes/attribute_spec.rb +144 -0
  84. data/spec/lib/active_data/model/attributes/base_spec.rb +82 -0
  85. data/spec/lib/active_data/model/attributes/collection_spec.rb +73 -0
  86. data/spec/lib/active_data/model/attributes/dictionary_spec.rb +93 -0
  87. data/spec/lib/active_data/model/attributes/localized_spec.rb +88 -33
  88. data/spec/lib/active_data/model/attributes/reflections/attribute_spec.rb +72 -0
  89. data/spec/lib/active_data/model/attributes/reflections/base_spec.rb +56 -0
  90. data/spec/lib/active_data/model/attributes/reflections/collection_spec.rb +37 -0
  91. data/spec/lib/active_data/model/attributes/reflections/dictionary_spec.rb +43 -0
  92. data/spec/lib/active_data/model/attributes/reflections/localized_spec.rb +37 -0
  93. data/spec/lib/active_data/model/attributes/reflections/represents_spec.rb +70 -0
  94. data/spec/lib/active_data/model/attributes/represents_spec.rb +153 -0
  95. data/spec/lib/active_data/model/attributes_spec.rb +243 -0
  96. data/spec/lib/active_data/model/callbacks_spec.rb +338 -0
  97. data/spec/lib/active_data/model/conventions_spec.rb +12 -0
  98. data/spec/lib/active_data/model/dirty_spec.rb +75 -0
  99. data/spec/lib/active_data/model/lifecycle_spec.rb +330 -0
  100. data/spec/lib/active_data/model/nested_attributes.rb +202 -0
  101. data/spec/lib/active_data/model/persistence_spec.rb +47 -0
  102. data/spec/lib/active_data/model/primary_spec.rb +84 -0
  103. data/spec/lib/active_data/model/scopes_spec.rb +88 -0
  104. data/spec/lib/active_data/model/typecasting_spec.rb +192 -0
  105. data/spec/lib/active_data/model/validations/associated_spec.rb +94 -0
  106. data/spec/lib/active_data/model/validations/nested_spec.rb +93 -0
  107. data/spec/lib/active_data/model/validations_spec.rb +31 -0
  108. data/spec/lib/active_data/model_spec.rb +1 -32
  109. data/spec/lib/active_data_spec.rb +12 -0
  110. data/spec/spec_helper.rb +39 -0
  111. data/spec/support/model_helpers.rb +10 -0
  112. metadata +246 -54
  113. data/gemfiles/Gemfile.rails-3 +0 -14
  114. data/lib/active_data/attributes/base.rb +0 -69
  115. data/lib/active_data/attributes/localized.rb +0 -42
  116. data/lib/active_data/model/associations/association.rb +0 -30
  117. data/lib/active_data/model/attributable.rb +0 -122
  118. data/lib/active_data/model/collectionizable.rb +0 -55
  119. data/lib/active_data/model/collectionizable/proxy.rb +0 -42
  120. data/lib/active_data/model/extensions.rb +0 -9
  121. data/lib/active_data/model/extensions/array.rb +0 -24
  122. data/lib/active_data/model/extensions/big_decimal.rb +0 -17
  123. data/lib/active_data/model/extensions/boolean.rb +0 -38
  124. data/lib/active_data/model/extensions/date.rb +0 -17
  125. data/lib/active_data/model/extensions/date_time.rb +0 -17
  126. data/lib/active_data/model/extensions/float.rb +0 -17
  127. data/lib/active_data/model/extensions/hash.rb +0 -22
  128. data/lib/active_data/model/extensions/integer.rb +0 -17
  129. data/lib/active_data/model/extensions/localized.rb +0 -22
  130. data/lib/active_data/model/extensions/object.rb +0 -17
  131. data/lib/active_data/model/extensions/string.rb +0 -17
  132. data/lib/active_data/model/extensions/time.rb +0 -17
  133. data/lib/active_data/model/localizable.rb +0 -31
  134. data/lib/active_data/model/nested_attributes.rb +0 -58
  135. data/lib/active_data/model/parameterizable.rb +0 -29
  136. data/lib/active_data/validations.rb +0 -7
  137. data/lib/active_data/validations/associated.rb +0 -17
  138. data/spec/lib/active_data/model/attributable_spec.rb +0 -191
  139. data/spec/lib/active_data/model/collectionizable_spec.rb +0 -60
  140. data/spec/lib/active_data/model/nested_attributes_spec.rb +0 -67
  141. data/spec/lib/active_data/model/type_cast_spec.rb +0 -116
  142. data/spec/lib/active_data/validations/associated_spec.rb +0 -88
@@ -0,0 +1,144 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveData::Model::Attributes::Attribute do
4
+ before { stub_model(:dummy) }
5
+
6
+ def attribute(*args)
7
+ options = args.extract_options!
8
+ reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Attribute, :field, {type: Object}.merge(options))
9
+ Dummy.new.attribute(:field)
10
+ end
11
+
12
+ describe '#read' do
13
+ let(:field) { attribute(type: String, normalizer: ->(v){ v ? v.strip : v }, default: :world, enum: ['hello', '42', 'world']) }
14
+
15
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq('world') }
16
+ specify { expect(field.tap { |r| r.write(:world) }.read).to eq('world') }
17
+ specify { expect(field.tap { |r| r.write('hello') }.read).to eq('hello') }
18
+ specify { expect(field.tap { |r| r.write(' hello ') }.read).to eq(nil) }
19
+ specify { expect(field.tap { |r| r.write(42) }.read).to eq('42') }
20
+ specify { expect(field.tap { |r| r.write(43) }.read).to eq(nil) }
21
+ specify { expect(field.tap { |r| r.write('') }.read).to eq(nil) }
22
+
23
+ context ':readonly' do
24
+ specify { expect(attribute(readonly: true, default: :world).tap { |r| r.write('string') }.read).to eq(:world) }
25
+ end
26
+ end
27
+
28
+ describe '#read_before_type_cast' do
29
+ let(:field) { attribute(type: String, normalizer: ->(v){ v.strip }, default: :world, enum: ['hello', '42', 'world']) }
30
+
31
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq(:world) }
32
+ specify { expect(field.tap { |r| r.write(:world) }.read_before_type_cast).to eq(:world) }
33
+ specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq('hello') }
34
+ specify { expect(field.tap { |r| r.write(42) }.read_before_type_cast).to eq(42) }
35
+ specify { expect(field.tap { |r| r.write(43) }.read_before_type_cast).to eq(43) }
36
+ specify { expect(field.tap { |r| r.write('') }.read_before_type_cast).to eq('') }
37
+
38
+ context ':readonly' do
39
+ specify { expect(attribute(readonly: true, default: :world).tap { |r| r.write('string') }.read_before_type_cast).to eq(:world) }
40
+ end
41
+ end
42
+
43
+ describe '#default' do
44
+ before { allow_any_instance_of(Dummy).to receive_messages(value: 42) }
45
+
46
+ specify { expect(attribute.default).to eq(nil) }
47
+ specify { expect(attribute(default: 'hello').default).to eq('hello') }
48
+ specify { expect(attribute(default: -> { value }).default).to eq(42) }
49
+ specify { expect(attribute(default: ->(object) { object.value }).default).to eq(42) }
50
+ specify { expect(attribute(default: ->(*args) { args.first.value }).default).to eq(42) }
51
+ end
52
+
53
+ describe '#defaultize' do
54
+ specify { expect(attribute.defaultize(nil)).to be_nil }
55
+ specify { expect(attribute(default: 'hello').defaultize(nil)).to eq('hello') }
56
+ specify { expect(attribute(default: 'hello').defaultize('world')).to eq('world') }
57
+ end
58
+
59
+ describe '#typecast' do
60
+ specify { expect(attribute.typecast(:hello)).to eq(:hello) }
61
+ specify { expect(attribute(type: Integer).typecast(42)).to eq(42) }
62
+ specify { expect(attribute(type: Integer).typecast('42')).to eq(42) }
63
+ end
64
+
65
+ describe '#enum' do
66
+ before { allow_any_instance_of(Dummy).to receive_messages(value: 1..5) }
67
+
68
+ specify { expect(attribute.enum).to eq([].to_set) }
69
+ specify { expect(attribute(enum: []).enum).to eq([].to_set) }
70
+ specify { expect(attribute(enum: 'hello').enum).to eq(['hello'].to_set) }
71
+ specify { expect(attribute(enum: ['hello', 'world']).enum).to eq(['hello', 'world'].to_set) }
72
+ specify { expect(attribute(enum: [1..5]).enum).to eq([1..5].to_set) }
73
+ specify { expect(attribute(enum: 1..5).enum).to eq((1..5).to_a.to_set) }
74
+ specify { expect(attribute(enum: -> { 1..5 }).enum).to eq((1..5).to_a.to_set) }
75
+ specify { expect(attribute(enum: -> { 'hello' }).enum).to eq(['hello'].to_set) }
76
+ specify { expect(attribute(enum: -> { ['hello', 42] }).enum).to eq(['hello', 42].to_set) }
77
+ specify { expect(attribute(enum: -> { value }).enum).to eq((1..5).to_a.to_set) }
78
+ specify { expect(attribute(enum: ->(object) { object.value }).enum).to eq((1..5).to_a.to_set) }
79
+ end
80
+
81
+ describe '#enumerize' do
82
+ specify { expect(attribute.enumerize('anything')).to eq('anything') }
83
+ specify { expect(attribute(enum: ['hello', 42]).enumerize('hello')).to eq('hello') }
84
+ specify { expect(attribute(enum: ['hello', 42]).enumerize('world')).to eq(nil) }
85
+ specify { expect(attribute(enum: -> { 'hello' }).enumerize('hello')).to eq('hello') }
86
+ specify { expect(attribute(enum: -> { 1..5 }).enumerize(2)).to eq(2) }
87
+ specify { expect(attribute(enum: -> { 1..5 }).enumerize(42)).to eq(nil) }
88
+ end
89
+
90
+ describe '#normalize' do
91
+ specify { expect(attribute.normalize(' hello ')).to eq(' hello ') }
92
+ specify { expect(attribute(normalizer: ->(v) { v.strip }).normalize(' hello ')).to eq('hello') }
93
+ specify { expect(attribute(normalizer: [->(v) { v.strip }, ->(v) { v.first(4) }]).normalize(' hello ')).to eq('hell') }
94
+ specify { expect(attribute(normalizer: [->(v) { v.first(4) }, ->(v) { v.strip }]).normalize(' hello ')).to eq('hel') }
95
+
96
+ context do
97
+ before { allow_any_instance_of(Dummy).to receive_messages(value: 'value') }
98
+ let(:other) { 'other' }
99
+
100
+ specify { expect(attribute(normalizer: ->(v) { value }).normalize(' hello ')).to eq('value') }
101
+ specify { expect(attribute(normalizer: ->(v, object) { object.value }).normalize(' hello ')).to eq('value') }
102
+ specify { expect(attribute(normalizer: ->(v, object) { other }).normalize(' hello ')).to eq('other') }
103
+ end
104
+
105
+ context 'integration' do
106
+ before do
107
+ allow(ActiveData).to receive_messages(config: ActiveData::Config.send(:new))
108
+ ActiveData.normalizer(:strip) do |value|
109
+ value.strip
110
+ end
111
+ ActiveData.normalizer(:trim) do |value, options, attribute|
112
+ value.first(length || options[:length] || 2)
113
+ end
114
+ ActiveData.normalizer(:reset) do |value, options, attribute|
115
+ empty = value.respond_to?(:empty?) ? value.empty? : value.nil?
116
+ empty ? attribute.default : value
117
+ end
118
+ end
119
+
120
+ let(:length) { nil }
121
+
122
+ specify { expect(attribute(normalizer: :strip).normalize(' hello ')).to eq('hello') }
123
+ specify { expect(attribute(normalizer: [:strip, :trim]).normalize(' hello ')).to eq('he') }
124
+ specify { expect(attribute(normalizer: [:trim, :strip]).normalize(' hello ')).to eq('h') }
125
+ specify { expect(attribute(normalizer: [:strip, { trim: { length: 4 } }]).normalize(' hello ')).to eq('hell') }
126
+ specify { expect(attribute(normalizer: {strip: { }, trim: { length: 4 } }).normalize(' hello ')).to eq('hell') }
127
+ specify { expect(attribute(normalizer: [:strip, { trim: { length: 4 } }, ->(v) { v.last(2) }])
128
+ .normalize(' hello ')).to eq('ll') }
129
+ specify { expect(attribute(normalizer: :reset).normalize('')).to eq(nil) }
130
+ specify { expect(attribute(normalizer: [:strip, :reset]).normalize(' ')).to eq(nil) }
131
+ specify { expect(attribute(normalizer: :reset, default: '!!!').normalize(nil)).to eq('!!!') }
132
+ specify { expect(attribute(normalizer: :reset, default: '!!!').normalize('')).to eq('!!!') }
133
+
134
+ context do
135
+ let(:length) { 3 }
136
+
137
+ specify { expect(attribute(normalizer: [:strip, { trim: { length: 4 } }]).normalize(' hello ')).to eq('hel') }
138
+ specify { expect(attribute(normalizer: {strip: { }, trim: { length: 4 } }).normalize(' hello ')).to eq('hel') }
139
+ specify { expect(attribute(normalizer: [:strip, { trim: { length: 4 } }, ->(v) { v.last(2) }])
140
+ .normalize(' hello ')).to eq('el') }
141
+ end
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,82 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe ActiveData::Model::Attributes::Base do
5
+ before { stub_model(:dummy) }
6
+
7
+ def attribute(*args)
8
+ options = args.extract_options!
9
+ reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Base, :field, options)
10
+ Dummy.new.attribute(:field)
11
+ end
12
+
13
+ describe '#read' do
14
+ let(:field) { attribute(type: String, normalizer: ->(v){ v ? v.strip : v }, default: :world, enum: ['hello', '42', 'world']) }
15
+ let(:object) { Object.new }
16
+
17
+ specify { expect(field.tap { |r| r.write(nil) }.read).to be_nil }
18
+ specify { expect(field.tap { |r| r.write('') }.read).to eq('') }
19
+ specify { expect(field.tap { |r| r.write(:world) }.read).to eq(:world) }
20
+ specify { expect(field.tap { |r| r.write(object) }.read).to eq(object) }
21
+
22
+ context ':readonly' do
23
+ specify { expect(attribute(readonly: true).tap { |r| r.write('string') }.read).to be_nil }
24
+ end
25
+ end
26
+
27
+ describe '#read_before_type_cast' do
28
+ let(:field) { attribute(type: String, normalizer: ->(v){ v.strip }, default: :world, enum: ['hello', '42', 'world']) }
29
+ let(:object) { Object.new }
30
+
31
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to be_nil }
32
+ specify { expect(field.tap { |r| r.write('') }.read_before_type_cast).to eq('') }
33
+ specify { expect(field.tap { |r| r.write(:world) }.read_before_type_cast).to eq(:world) }
34
+ specify { expect(field.tap { |r| r.write(object) }.read_before_type_cast).to eq(object) }
35
+
36
+ context ':readonly' do
37
+ specify { expect(attribute(readonly: true).tap { |r| r.write('string') }.read_before_type_cast).to be_nil }
38
+ end
39
+ end
40
+
41
+ describe '#value_present?' do
42
+ let(:field) { attribute }
43
+
44
+ specify { expect(field.tap { |r| r.write(0) }).to be_value_present }
45
+ specify { expect(field.tap { |r| r.write(42) }).to be_value_present }
46
+ specify { expect(field.tap { |r| r.write(true) }).to be_value_present }
47
+ specify { expect(field.tap { |r| r.write(false) }).to be_value_present }
48
+ specify { expect(field.tap { |r| r.write(nil) }).not_to be_value_present }
49
+ specify { expect(field.tap { |r| r.write('') }).not_to be_value_present }
50
+ specify { expect(field.tap { |r| r.write(:world) }).to be_value_present }
51
+ specify { expect(field.tap { |r| r.write(Object.new) }).to be_value_present }
52
+ specify { expect(field.tap { |r| r.write([]) }).not_to be_value_present }
53
+ specify { expect(field.tap { |r| r.write([42]) }).to be_value_present }
54
+ specify { expect(field.tap { |r| r.write({}) }).not_to be_value_present }
55
+ specify { expect(field.tap { |r| r.write({hello: 42}) }).to be_value_present }
56
+ end
57
+
58
+ describe '#query' do
59
+ let(:field) { attribute }
60
+
61
+ specify { expect(field.tap { |r| r.write(0) }.query).to be(false) }
62
+ specify { expect(field.tap { |r| r.write(42) }.query).to be(true) }
63
+ specify { expect(field.tap { |r| r.write(true) }.query).to be(true) }
64
+ specify { expect(field.tap { |r| r.write(false) }.query).to be(false) }
65
+ specify { expect(field.tap { |r| r.write(nil) }.query).to be(false) }
66
+ specify { expect(field.tap { |r| r.write('') }.query).to be(false) }
67
+ specify { expect(field.tap { |r| r.write(:world) }.query).to be(true) }
68
+ specify { expect(field.tap { |r| r.write(Object.new) }.query).to be(true) }
69
+ specify { expect(field.tap { |r| r.write([]) }.query).to be(false) }
70
+ specify { expect(field.tap { |r| r.write([42]) }.query).to be(true) }
71
+ specify { expect(field.tap { |r| r.write({}) }.query).to be(false) }
72
+ specify { expect(field.tap { |r| r.write({hello: 42}) }.query).to be(true) }
73
+ end
74
+
75
+ describe '#readonly?' do
76
+ specify { expect(attribute).not_to be_readonly }
77
+ specify { expect(attribute(readonly: false)).not_to be_readonly }
78
+ specify { expect(attribute(readonly: true)).to be_readonly }
79
+ specify { expect(attribute(readonly: -> { false })).not_to be_readonly }
80
+ specify { expect(attribute(readonly: -> { true })).to be_readonly }
81
+ end
82
+ end
@@ -0,0 +1,73 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe ActiveData::Model::Attributes::Collection do
5
+ before { stub_model(:dummy) }
6
+
7
+ def attribute(*args)
8
+ options = args.extract_options!
9
+ reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Collection, :field, options)
10
+ Dummy.new.attribute(:field)
11
+ end
12
+
13
+ describe '#read' do
14
+ let(:field) { attribute(type: String, normalizer: ->(v){ v.uniq }, default: :world, enum: ['hello', '42']) }
15
+
16
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
17
+ specify { expect(field.tap { |r| r.write([nil]) }.read).to eq([nil]) }
18
+ specify { expect(field.tap { |r| r.write('hello') }.read).to eq(['hello']) }
19
+ specify { expect(field.tap { |r| r.write([42]) }.read).to eq(['42']) }
20
+ specify { expect(field.tap { |r| r.write([43]) }.read).to eq([nil]) }
21
+ specify { expect(field.tap { |r| r.write([43, 44]) }.read).to eq([nil]) }
22
+ specify { expect(field.tap { |r| r.write(['']) }.read).to eq([nil]) }
23
+ specify { expect(field.tap { |r| r.write(['hello', 42]) }.read).to eq(['hello', '42']) }
24
+ specify { expect(field.tap { |r| r.write(['hello', false]) }.read).to eq(['hello', nil]) }
25
+
26
+ context do
27
+ let(:field) { attribute(type: String, normalizer: ->(v){ v.uniq }, default: :world) }
28
+
29
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq([]) }
30
+ specify { expect(field.tap { |r| r.write([nil, nil]) }.read).to eq(['world']) }
31
+ specify { expect(field.tap { |r| r.write('hello') }.read).to eq(['hello']) }
32
+ specify { expect(field.tap { |r| r.write([42]) }.read).to eq(['42']) }
33
+ specify { expect(field.tap { |r| r.write(['']) }.read).to eq(['']) }
34
+ end
35
+ end
36
+
37
+ describe '#read_before_type_cast' do
38
+ let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
39
+
40
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq([]) }
41
+ specify { expect(field.tap { |r| r.write([nil]) }.read_before_type_cast).to eq([:world]) }
42
+ specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq(['hello']) }
43
+ specify { expect(field.tap { |r| r.write([42]) }.read_before_type_cast).to eq([42]) }
44
+ specify { expect(field.tap { |r| r.write([43]) }.read_before_type_cast).to eq([43]) }
45
+ specify { expect(field.tap { |r| r.write([43, 44]) }.read_before_type_cast).to eq([43, 44]) }
46
+ specify { expect(field.tap { |r| r.write(['']) }.read_before_type_cast).to eq(['']) }
47
+ specify { expect(field.tap { |r| r.write(['hello', 42]) }.read_before_type_cast).to eq(['hello', 42]) }
48
+ specify { expect(field.tap { |r| r.write(['hello', false]) }.read_before_type_cast).to eq(['hello', false]) }
49
+
50
+ context do
51
+ let(:field) { attribute(type: String, default: :world) }
52
+
53
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq([]) }
54
+ specify { expect(field.tap { |r| r.write([nil, nil]) }.read_before_type_cast).to eq([:world, :world]) }
55
+ specify { expect(field.tap { |r| r.write('hello') }.read_before_type_cast).to eq(['hello']) }
56
+ specify { expect(field.tap { |r| r.write([42]) }.read_before_type_cast).to eq([42]) }
57
+ specify { expect(field.tap { |r| r.write(['']) }.read_before_type_cast).to eq(['']) }
58
+ end
59
+ end
60
+
61
+ context 'integration' do
62
+ before do
63
+ stub_model(:post) do
64
+ collection :tags, String
65
+ end
66
+ end
67
+
68
+ specify { expect(Post.new(tags: ['hello', 42]).tags).to eq(['hello', '42']) }
69
+ specify { expect(Post.new(tags: ['hello', 42]).tags_before_type_cast).to eq(['hello', 42]) }
70
+ specify { expect(Post.new.tags?).to eq(false) }
71
+ specify { expect(Post.new(tags: ['hello', 42]).tags?).to eq(true) }
72
+ end
73
+ end
@@ -0,0 +1,93 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe ActiveData::Model::Attributes::Dictionary do
5
+ before { stub_model(:dummy) }
6
+
7
+ def attribute(*args)
8
+ options = args.extract_options!
9
+ reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Dictionary, :field, options)
10
+ Dummy.new.attribute(:field)
11
+ end
12
+
13
+ describe '#read' do
14
+ let(:field) { attribute(type: String, normalizer: ->(v){ v.delete_if { |_, v| v == nil } },
15
+ default: :world, enum: ['hello', '42']) }
16
+
17
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
18
+ specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
19
+ specify { expect(field.tap { |r| r.write({a: nil}) }.read).to eq({}) }
20
+ specify { expect(field.tap { |r| r.write({a: ''}) }.read).to eq({}) }
21
+ specify { expect(field.tap { |r| r.write({a: 1}) }.read).to eq({}) }
22
+ specify { expect(field.tap { |r| r.write({a: 42}) }.read).to eq({'a' => '42'}) }
23
+ specify { expect(field.tap { |r| r.write({a: 'hello', b: '42'}) }.read).to eq({'a' => 'hello', 'b' => '42'}) }
24
+ specify { expect(field.tap { |r| r.write({a: 'hello', b: '1'}) }.read).to eq({'a' => 'hello'}) }
25
+ specify { expect(field.tap { |r| r.write({a: 'hello', x: '42'}) }.read).to eq({'a' => 'hello', 'x' => '42'}) }
26
+
27
+ context do
28
+ let(:field) { attribute(type: String, normalizer: ->(v){ v.delete_if { |_, v| v == nil } },
29
+ default: :world) }
30
+
31
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
32
+ specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
33
+ specify { expect(field.tap { |r| r.write({a: 1}) }.read).to eq({'a' => '1'}) }
34
+ specify { expect(field.tap { |r| r.write({a: nil, b: nil}) }.read).to eq({'a' => 'world', 'b' => 'world'}) }
35
+ specify { expect(field.tap { |r| r.write({a: ''}) }.read).to eq({'a' => ''}) }
36
+ end
37
+
38
+ context 'with :keys' do
39
+ let(:field) { attribute(type: String, keys: ['a', :b]) }
40
+
41
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
42
+ specify { expect(field.tap { |r| r.write({}) }.read).to eq({}) }
43
+ specify { expect(field.tap { |r| r.write({a: 1, 'b' => 2, c: 3, 'd' => 4}) }.read).to eq({'a' => '1', 'b' => '2'}) }
44
+ specify { expect(field.tap { |r| r.write({a: 1, c: 3, 'd' => 4}) }.read).to eq({'a' => '1'}) }
45
+ end
46
+ end
47
+
48
+ describe '#read_before_type_cast' do
49
+ let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
50
+
51
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
52
+ specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
53
+ specify { expect(field.tap { |r| r.write({a: 1}) }.read_before_type_cast).to eq({'a' => 1}) }
54
+ specify { expect(field.tap { |r| r.write({a: nil}) }.read_before_type_cast).to eq({'a' => :world}) }
55
+ specify { expect(field.tap { |r| r.write({a: ''}) }.read_before_type_cast).to eq({'a' => ''}) }
56
+ specify { expect(field.tap { |r| r.write({a: 42}) }.read_before_type_cast).to eq({'a' => 42}) }
57
+ specify { expect(field.tap { |r| r.write({a: 'hello', b: '42'}) }.read_before_type_cast).to eq({'a' => 'hello', 'b' => '42'}) }
58
+ specify { expect(field.tap { |r| r.write({a: 'hello', b: '1'}) }.read_before_type_cast).to eq({'a' => 'hello', 'b' => '1'}) }
59
+ specify { expect(field.tap { |r| r.write({a: 'hello', x: '42'}) }.read_before_type_cast).to eq({'a' => 'hello', 'x' => '42'}) }
60
+
61
+ context do
62
+ let(:field) { attribute(type: String, default: :world) }
63
+
64
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
65
+ specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
66
+ specify { expect(field.tap { |r| r.write({a: 1}) }.read_before_type_cast).to eq({'a' => 1}) }
67
+ specify { expect(field.tap { |r| r.write({a: nil, b: nil}) }.read_before_type_cast).to eq({'a' => :world, 'b' => :world}) }
68
+ specify { expect(field.tap { |r| r.write({a: ''}) }.read_before_type_cast).to eq({'a' => ''}) }
69
+ end
70
+
71
+ context 'with :keys' do
72
+ let(:field) { attribute(type: String, keys: ['a', :b]) }
73
+
74
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
75
+ specify { expect(field.tap { |r| r.write({}) }.read_before_type_cast).to eq({}) }
76
+ specify { expect(field.tap { |r| r.write({a: 1, 'b' => 2, c: 3, 'd' => 4}) }.read_before_type_cast)
77
+ .to eq({'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4}) }
78
+ end
79
+ end
80
+
81
+ context 'integration' do
82
+ before do
83
+ stub_model(:post) do
84
+ dictionary :options, type: String
85
+ end
86
+ end
87
+
88
+ specify { expect(Post.new(options: {a: 'hello', b: 42}).options).to eq({'a' => 'hello', 'b' => '42'}) }
89
+ specify { expect(Post.new(options: {a: 'hello', b: 42}).options_before_type_cast).to eq({'a' => 'hello', 'b' => 42}) }
90
+ specify { expect(Post.new.options?).to eq(false) }
91
+ specify { expect(Post.new(options: {a: 'hello', b: 42}).options?).to eq(true) }
92
+ end
93
+ end
@@ -1,49 +1,104 @@
1
1
  # encoding: UTF-8
2
2
  require 'spec_helper'
3
3
 
4
- describe Localized do
5
- let(:klass) do
6
- Class.new do
7
- include ActiveData::Model
4
+ describe ActiveData::Model::Attributes::Localized do
5
+ before { stub_model(:dummy) }
8
6
 
9
- attribute :name, type: Localized
10
- end
7
+ def attribute(*args)
8
+ options = args.extract_options!
9
+ reflection = Dummy.add_attribute(ActiveData::Model::Attributes::Reflections::Localized, :field, options)
10
+ described_class.new('field', Dummy.new)
11
11
  end
12
- let(:translations) { { en: 'Hello', ru: 'Привет' } }
13
- before { I18n.locale = :en }
14
12
 
15
- describe '#name_translations' do
16
- subject { klass.new name_translations: translations }
17
- its(:name_translations) { should == translations.stringify_keys }
18
- its(:name) { should == translations[:en] }
19
- end
13
+ describe '#read' do
14
+ let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
20
15
 
21
- describe '#name' do
22
- subject { klass.new name: 'Hello' }
23
- its(:name_translations) { should == { 'en' => 'Hello' } }
24
- its(:name) { should == 'Hello' }
25
- end
16
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
17
+ specify { expect(field.tap { |r| r.write({ en: 'hello' }) }.read).to eq({ 'en' => 'hello' }) }
18
+ specify { expect(field.tap { |r| r.write({ en: 42 }) }.read).to eq({ 'en' => '42' }) }
19
+ specify { expect(field.tap { |r| r.write({ en: 43 }) }.read).to eq({ 'en' => nil }) }
20
+ specify { expect(field.tap { |r| r.write({ en: '' }) }.read).to eq({ 'en' => nil }) }
21
+ specify { expect(field.tap { |r| r.write({ en: nil }) }.read).to eq({ 'en' => nil }) }
22
+ specify { expect(field.tap { |r| r.write({ en: 'hello', ru: 42 }) }.read).to eq({ 'en' => 'hello', 'ru' => '42' }) }
26
23
 
27
- describe '#name_before_type_cast' do
28
- let(:object) { Object.new }
29
- subject { klass.new name: object }
30
- its(:name_before_type_cast) { should == object }
24
+ context do
25
+ let(:field) { attribute(type: String, default: :world) }
26
+
27
+ specify { expect(field.tap { |r| r.write(nil) }.read).to eq({}) }
28
+ specify { expect(field.tap { |r| r.write({ en: 'hello' }) }.read).to eq({ 'en' => 'hello' }) }
29
+ specify { expect(field.tap { |r| r.write({ en: 42 }) }.read).to eq({ 'en' => '42' }) }
30
+ specify { expect(field.tap { |r| r.write({ en: '' }) }.read).to eq({ 'en' => '' }) }
31
+ specify { expect(field.tap { |r| r.write({ en: nil }) }.read).to eq({ 'en' => 'world' }) }
32
+ end
31
33
  end
32
34
 
33
- context 'fallbacks' do
34
- subject { klass.new name_translations: { ru: 'Привет' } }
35
+ describe '#read_before_type_cast' do
36
+ let(:field) { attribute(type: String, default: :world, enum: ['hello', '42']) }
37
+
38
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
39
+ specify { expect(field.tap { |r| r.write({ en: 'hello' }) }.read_before_type_cast).to eq({ 'en' => 'hello' }) }
40
+ specify { expect(field.tap { |r| r.write({ en: 42 }) }.read_before_type_cast).to eq({ 'en' => 42 }) }
41
+ specify { expect(field.tap { |r| r.write({ en: 43 }) }.read_before_type_cast).to eq({ 'en' => 43 }) }
42
+ specify { expect(field.tap { |r| r.write({ en: '' }) }.read_before_type_cast).to eq({ 'en' => '' }) }
43
+ specify { expect(field.tap { |r| r.write({ en: nil }) }.read_before_type_cast).to eq({ 'en' => :world }) }
44
+ specify { expect(field.tap { |r| r.write({ en: 'hello', ru: 42 }) }.read_before_type_cast).to eq({ 'en' => 'hello', 'ru' => 42 }) }
45
+
35
46
  context do
36
- its(:name) { should be_nil }
47
+ let(:field) { attribute(type: String, default: :world) }
48
+
49
+ specify { expect(field.tap { |r| r.write(nil) }.read_before_type_cast).to eq({}) }
50
+ specify { expect(field.tap { |r| r.write({ en: 'hello' }) }.read_before_type_cast).to eq({ 'en' => 'hello' }) }
51
+ specify { expect(field.tap { |r| r.write({ en: 42 }) }.read_before_type_cast).to eq({ 'en' => 42 }) }
52
+ specify { expect(field.tap { |r| r.write({ en: '' }) }.read_before_type_cast).to eq({ 'en' => '' }) }
53
+ specify { expect(field.tap { |r| r.write({ en: nil }) }.read_before_type_cast).to eq({ 'en' => :world }) }
37
54
  end
55
+ end
38
56
 
39
- context do
40
- before do
41
- require "i18n/backend/fallbacks"
42
- I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
43
- I18n.fallbacks.map(en: :ru)
57
+ context 'integration' do
58
+ let(:klass) do
59
+ Class.new do
60
+ include ActiveData::Model
61
+ include ActiveData::Model::Localization
62
+
63
+ localized :name, type: String
64
+ end
65
+ end
66
+ let(:translations) { { en: 'Hello', ru: 'Привет' } }
67
+ before { I18n.locale = :en }
68
+
69
+ describe '#name_translations' do
70
+ subject { klass.new name_translations: translations }
71
+ its(:name_translations) { should == translations.stringify_keys }
72
+ its(:name) { should == translations[:en] }
73
+ end
74
+
75
+ describe '#name' do
76
+ subject { klass.new name: 'Hello' }
77
+ its(:name_translations) { should == { 'en' => 'Hello' } }
78
+ its(:name) { should == 'Hello' }
79
+ end
80
+
81
+ describe '#name_before_type_cast' do
82
+ let(:object) { Object.new }
83
+ subject { klass.new name: object }
84
+ its(:name_before_type_cast) { should == object }
85
+ end
86
+
87
+ context 'fallbacks' do
88
+ subject { klass.new name_translations: { ru: 'Привет' } }
89
+ context do
90
+ its(:name) { should be_nil }
91
+ end
92
+
93
+ context do
94
+ before do
95
+ require "i18n/backend/fallbacks"
96
+ I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
97
+ I18n.fallbacks.map(en: :ru)
98
+ end
99
+ after { I18n.fallbacks = false }
100
+ its(:name) { should == 'Привет' }
44
101
  end
45
- after { I18n.fallbacks = false }
46
- its(:name) { should == 'Привет' }
47
102
  end
48
103
  end
49
- end
104
+ end