statham 0.1.2 → 0.1.3
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 +4 -4
- data/lib/statham/attribute.rb +3 -3
- data/lib/statham/document.rb +7 -1
- data/lib/statham/version.rb +1 -1
- data/spec/support/schema.rb +1 -1
- data/spec/unit/attribute_spec.rb +13 -4
- data/spec/unit/document_spec.rb +13 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e38adf86a8b69b7b7c0a084a658b0abd00904cd
|
4
|
+
data.tar.gz: 137db91c43083ab60c1702f6bc36c920a6e1b5a3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfe03b7dde3a96808ef142deff82b332000e97ca760376201e97c6001c2ad4e385cae9d2d5ebd3c7d6e19b27d4f74583bf9dbe63b8d4db6adc3b1ffac620f043
|
7
|
+
data.tar.gz: 4d3905daf80514b356d7d33e653c44d33f185923a8c2b39aa19d85fc3c00b9a4ea8e9d4a189a12ec1a29e29c2ea199ac83b75e3e43bb6dfa336ce2d9f17441e8
|
data/lib/statham/attribute.rb
CHANGED
@@ -35,9 +35,7 @@ module Statham
|
|
35
35
|
#
|
36
36
|
# Returns deserialized value.
|
37
37
|
def deserialize(value)
|
38
|
-
|
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
|
data/lib/statham/document.rb
CHANGED
@@ -90,7 +90,13 @@ module Statham
|
|
90
90
|
|
91
91
|
attribute_set.attributes.each do |name, attribute|
|
92
92
|
define_method(name) do
|
93
|
-
|
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|
|
data/lib/statham/version.rb
CHANGED
data/spec/support/schema.rb
CHANGED
data/spec/unit/attribute_spec.rb
CHANGED
@@ -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
|
10
|
-
it { expect(attribute.serialize(false)).to
|
11
|
-
it { expect(attribute.serialize('1')).to
|
12
|
-
it { expect(attribute.serialize('0')).to
|
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
|
data/spec/unit/document_spec.rb
CHANGED
@@ -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.
|
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-
|
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.
|
143
|
+
rubygems_version: 2.2.2
|
144
144
|
signing_key:
|
145
145
|
specification_version: 4
|
146
146
|
summary: JSON objects for attributes
|