statham 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 986ac2d10681cc489f76a4b6d5df789374b86470
4
- data.tar.gz: 061bb36945081bfb8b5a435a4c35a4000e25b860
3
+ metadata.gz: 8e38adf86a8b69b7b7c0a084a658b0abd00904cd
4
+ data.tar.gz: 137db91c43083ab60c1702f6bc36c920a6e1b5a3
5
5
  SHA512:
6
- metadata.gz: 64fcbfdc01cfd951be99f099c59c1e91a7bf3d6aabc2e2203673d8b6459e7a529f3d1b2347928622f4f11fa73c1f2c4f3deb8615a805d5c81bea67e6be16283d
7
- data.tar.gz: bd06fc3f406601a1fff2ff2a895b5525e708c75d543eed33b2ed8aeb9a3455fe9c3ebce49740dacbc96366dabb4e512df06a04af3de40580c62acd947a2efb03
6
+ metadata.gz: bfe03b7dde3a96808ef142deff82b332000e97ca760376201e97c6001c2ad4e385cae9d2d5ebd3c7d6e19b27d4f74583bf9dbe63b8d4db6adc3b1ffac620f043
7
+ data.tar.gz: 4d3905daf80514b356d7d33e653c44d33f185923a8c2b39aa19d85fc3c00b9a4ea8e9d4a189a12ec1a29e29c2ea199ac83b75e3e43bb6dfa336ce2d9f17441e8
@@ -35,9 +35,7 @@ module Statham
35
35
  #
36
36
  # Returns deserialized value.
37
37
  def deserialize(value)
38
- deserialized = value.nil? ? nil : casted_to_type(value)
39
-
40
- deserialized.nil? && !@default.nil? ? @default : deserialized
38
+ value
41
39
  end
42
40
 
43
41
  protected
@@ -53,6 +51,8 @@ module Statham
53
51
  ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
54
52
  when :integer
55
53
  value.to_i
54
+ when :array
55
+ value.to_a
56
56
  else
57
57
  value.to_s
58
58
  end
@@ -90,7 +90,13 @@ module Statham
90
90
 
91
91
  attribute_set.attributes.each do |name, attribute|
92
92
  define_method(name) do
93
- attribute.deserialize(send(attribute_set.name)[name])
93
+ parent_attribute = send(attribute_set.name)
94
+
95
+ if parent_attribute.has_key?(name)
96
+ attribute.deserialize(parent_attribute[name])
97
+ else
98
+ attribute.default
99
+ end
94
100
  end
95
101
 
96
102
  define_method("#{name}=") do |value|
@@ -1,3 +1,3 @@
1
1
  module Statham
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -1,5 +1,5 @@
1
1
  ActiveRecord::Schema.define(version: 20131213121829) do
2
- create_table "attributes_test_model", force: true do |t|
2
+ create_table "attributes_test_models", force: true do |t|
3
3
  t.text "foo"
4
4
  end
5
5
  end
@@ -6,10 +6,19 @@ describe Statham::Attribute do
6
6
  let(:attribute) { Statham::Attribute.new(type: :boolean) }
7
7
 
8
8
  it { expect(attribute.serialize(nil)).to be_nil }
9
- it { expect(attribute.serialize(true)).to be_true }
10
- it { expect(attribute.serialize(false)).to be_false }
11
- it { expect(attribute.serialize('1')).to be_true }
12
- it { expect(attribute.serialize('0')).to be_false }
9
+ it { expect(attribute.serialize(true)).to eq(true) }
10
+ it { expect(attribute.serialize(false)).to eq(false) }
11
+ it { expect(attribute.serialize('1')).to eq(true) }
12
+ it { expect(attribute.serialize('0')).to eq(false) }
13
+ end
14
+
15
+ context 'when type is array' do
16
+ let(:attribute) { Statham::Attribute.new(type: :array) }
17
+
18
+ it { expect(attribute.serialize(nil)).to be_nil }
19
+ it { expect { attribute.serialize(true) }.to raise_error }
20
+ it { expect { attribute.serialize('string') }.to raise_error }
21
+ it { expect { attribute.serialize([1, 2, 3]) }.not_to raise_error }
13
22
  end
14
23
 
15
24
  context 'when default value is set' do
@@ -32,4 +32,17 @@ describe Statham::Document do
32
32
  it { expect(ChildTestModel.parent_statham_attribute_set_for(:foo)).to eq(AttributesTestModel.statham_attribute_sets[:foo]) }
33
33
  it { expect(ChildChildTestModel.parent_statham_attribute_set_for(:foo)).to eq(ChildTestModel.statham_attribute_sets[:foo]) }
34
34
  end
35
+
36
+ describe '.apply_statham_attribute_set' do
37
+ let(:parent_class) { Class.new { def self.serialize(*args); end } }
38
+ let(:attribute_set) { Statham::AttributeSet.new }
39
+ let(:attributes) { { foo: { }, bar: { } } }
40
+
41
+ before { parent_class.include Statham::Document }
42
+ before { attributes.each { |name, options| attribute_set.attribute(name, options) } }
43
+
44
+ it { expect(parent_class).to receive(:serialize).once; parent_class.apply_statham_attribute_set(attribute_set) }
45
+ it { expect { parent_class.apply_statham_attribute_set(attribute_set) }.to change { parent_class.new.methods } }
46
+ it { expect { parent_class.apply_statham_attribute_set(attribute_set) }.to change { parent_class.new.methods.sort }.by([:foo, :foo=, :bar, :bar=].sort) }
47
+ end
35
48
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statham
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inbeom Hwang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-27 00:00:00.000000000 Z
11
+ date: 2014-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  version: '0'
141
141
  requirements: []
142
142
  rubyforge_project:
143
- rubygems_version: 2.2.0
143
+ rubygems_version: 2.2.2
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: JSON objects for attributes