statham 0.1.4 → 0.1.5

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: a6416303d3bcbeb8cfe0342f29ded044aa259cc5
4
- data.tar.gz: c5c71a1b6aae26ae0b58acd14b20ed14c5e1311d
3
+ metadata.gz: fd967b72b964ecbf39c69833c38424e80bc876c4
4
+ data.tar.gz: 4c69fe952d786af7a610fe0353e28ad2b59e0343
5
5
  SHA512:
6
- metadata.gz: 3195ffe3daf9df99e9d7141ef3cd9a58158b012331fcb8a9b493bf841ecde2d2419f421c0578323d03021824f2d87c9aaa1c77985d9f8fbda97b41a94a47e0d1
7
- data.tar.gz: e36dcc66f17214fa19ad7776710af34b71d68563ae384aafd17062044f5c50a5804c170e4cf425e6d3fb98bb1c0a71470d8cebb6eabe6d47f56bd3cca2680b07
6
+ metadata.gz: 4034b0d3d328b5902bff7eddcbdfd9fd49dc3b5078ad4ccfa3913842a02005da0679756c5746660bab0c615b0accb0ce348c20eea0d71d6a17793e4064f82e37
7
+ data.tar.gz: 3c9909c9c011c600543e858c7e7921672699be9fc4997911dc9a14b6c0809423a9ff3fcaa7d15e34d32a71630fec5182bcd4588d11f9a05fa51aad4d4f21fc2f
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Statham
2
2
 
3
- Do serialization like boss.
3
+ Statham is ActiveRecord library for easier access to attributes stored in JSON-
4
+ serialized columns. It supports text (blob) or JSON type columns on MySQL,
5
+ Sqlite and PostgreSQL.
4
6
 
5
7
  ## Installation
6
8
 
@@ -34,8 +34,12 @@ module Statham
34
34
  # value - Value to deserialize.
35
35
  #
36
36
  # Returns deserialized value.
37
- def deserialize(value)
38
- value
37
+ def deserialize(value, options = {})
38
+ if options[:fallback_to_default]
39
+ value || @default
40
+ else
41
+ value
42
+ end
39
43
  end
40
44
 
41
45
  protected
@@ -59,7 +59,7 @@ module Statham
59
59
  # Returns Hash containing deserialized values.
60
60
  def deserialize_with_attributes(object)
61
61
  @attribute_set.attributes.inject(object) do |serialized, (name, attribute)|
62
- serialized.merge name => attribute.deserialize(object[name])
62
+ serialized.merge name => attribute.deserialize(object[name], fallback_to_default: !object.has_key?(name))
63
63
  end
64
64
  end
65
65
  end
@@ -1,3 +1,3 @@
1
1
  module Statham
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -24,8 +24,24 @@ describe Statham::Serializer do
24
24
  describe '#load' do
25
25
  let(:statham_column) { Statham::Serializer.new(attribute_set) }
26
26
  let(:object_json) { ActiveSupport::JSON.encode({ foo: 'bar' }) }
27
+ let(:baz_default) { 'bazbaz' }
27
28
 
28
- it { expect(statham_column.load(object_json)).to eq({ 'foo' => 'bar' }) }
29
- it { expect(statham_column.load(object_json)[:foo]).to eq('bar') }
29
+ before do
30
+ attribute_set.attribute :baz, default: baz_default
31
+ end
32
+
33
+ context 'when object_json does not have attribute with default value' do
34
+ it { expect(statham_column.load(object_json)).to eq({ 'foo' => 'bar', 'baz' => baz_default }) }
35
+ it { expect(statham_column.load(object_json)[:foo]).to eq('bar') }
36
+ it { expect(statham_column.load(object_json)[:baz]).to eq(baz_default) }
37
+ end
38
+
39
+ context 'when object_json has attribute with default value' do
40
+ let(:object_json) { ActiveSupport::JSON.encode({ foo: 'bar', baz: nil }) }
41
+
42
+ it { expect(statham_column.load(object_json)).to eq({ 'foo' => 'bar', 'baz' => nil }) }
43
+ it { expect(statham_column.load(object_json)[:foo]).to eq('bar') }
44
+ it { expect(statham_column.load(object_json)[:baz]).to be_nil }
45
+ end
30
46
  end
31
47
  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
4
+ version: 0.1.5
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-08-07 00:00:00.000000000 Z
11
+ date: 2014-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport