hash_serializers 0.1.0 → 0.2.0

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
  SHA256:
3
- metadata.gz: 503147e93e2a7c841e56cbfb82595af790f9c113c124c1aef6a1a9690cfa45e7
4
- data.tar.gz: 7ad6401190a3478e180b08dc070f05141ce9ff1a67c1c528e1169d370dd187a7
3
+ metadata.gz: e3674968a12ba32324a49a95ca09b2b6b476831470b3b41174a0675fec628682
4
+ data.tar.gz: c5c199369841565c7e08ac5777ce7b91308af4357aff99e098beacc74b064a39
5
5
  SHA512:
6
- metadata.gz: 22838c0645336863b67c4f6c6221d724fb74d7a716b9939ac0eb53c4db414814786519458076e9ac77615ca6596814c1a3527f844ec79483564ada315e4db6c8
7
- data.tar.gz: b15f3f563cb2634e20e07307e6ade37e1c2af74b8bf7e80774f02da7cbac95947bd6afd2d3e44530830998234ab6b24b2c026921b6761239f3a92913aede148b
6
+ metadata.gz: b738228d0328f4b34cc59c8a1face6008dda3b8c6acda740883c79db9fa34780d1fdb6ec88651967e2bcf6aba0bcc721ccbd80277327fa99a33514d043411818
7
+ data.tar.gz: bcd294b825f6815218dfe546d1098376998d9ee83fd335a2b105c01b5077d05a8b49dda14d676a38ec06d5b1d8db51228b2c1c1cb3fb9d4ee4fb410b4e2da49b
data/README.md CHANGED
@@ -20,19 +20,34 @@ Or install it yourself as:
20
20
 
21
21
  ```ruby
22
22
  class UserSerializer < HashSerializer::JSON
23
+ format :boolean do |val|
24
+ !!val
25
+ end
26
+ format :date do |val|
27
+ val.iso8601
28
+ end
29
+
23
30
  reveal :first_name, :email
24
31
  reveal :last_name, as: :surname
32
+
33
+ reveal :is_admin, format: :boolean
34
+
35
+ with_format :date do
36
+ reveal :registered_at
37
+ end
25
38
  end
26
39
 
27
40
  serializable_user = UserSerializer.new(
28
41
  first_name: 'Bob',
29
42
  last_name: 'Marley',
30
- email: 'email@exmple.com'
43
+ email: 'email@exmple.com',
44
+ is_admin: nil,
45
+ registered_at: Time.now
31
46
  )
32
47
 
33
- serializable_user.as_json # {:first_name=>"Bob", :email=>"email@exmple.com", :surname=>"Marley"}
48
+ serializable_user.as_json # {:first_name=>"Bob", :email=>"email@exmple.com", :surname=>"Marley", :is_admin=>false, :registered_at => "2019-03-05T23:18:33+02:00"}
34
49
 
35
- serializable_user.to_json # '{"first_name":"Bob","email":"email@exmple.com","surname":"Marley"}'
50
+ serializable_user.to_json # '{"first_name":"Bob","email":"email@exmple.com","surname":"Marley","is_admin":false,"registered_at":"2019-03-05T23:18:33+02:00"}'
36
51
  ```
37
52
 
38
53
  ## Development
@@ -10,5 +10,14 @@ module HashSerializer
10
10
  def key_name
11
11
  options[:as] || name
12
12
  end
13
+
14
+ def as_json(object)
15
+ value = object.public_send(name)
16
+ case options[:format]
17
+ when Symbol
18
+ value = object.formats[options[:format]].call(value)
19
+ end
20
+ { key_name => value }
21
+ end
13
22
  end
14
23
  end
@@ -10,21 +10,40 @@ module HashSerializer
10
10
  end
11
11
 
12
12
  class << self
13
- def reveal(*attrs, as: nil)
13
+ def reveal(*attrs, as: nil, format: nil)
14
14
  @attributes ||= []
15
- @attributes.push(*attrs.map {|name| HashSerializer::Attribute.new(name, as: as)})
15
+ attribute_options = {as: as, format: format}.merge(default_attribute_options)
16
+ @attributes.push(*attrs.map {|name| HashSerializer::Attribute.new(name, attribute_options)})
16
17
  end
17
18
 
18
19
  def attributes
19
20
  @attributes
20
21
  end
22
+
23
+ def default_attribute_options
24
+ @default_attribute_options || {}
25
+ end
26
+
27
+ def formats
28
+ @formats
29
+ end
30
+
31
+ def with_format(format_name)
32
+ @default_attribute_options = { format: format_name }
33
+ yield
34
+ @default_attribute_options = nil
35
+ end
36
+
37
+ def format(format_name, &formatter)
38
+ @formats ||= {}
39
+ @formats[format_name] = formatter
40
+ end
21
41
  end
22
42
 
23
43
  def as_json
24
44
  hash_to_object!
25
45
  attributes.inject({}) do |memo, attribute|
26
- memo[attribute.key_name] = self.public_send(attribute.name)
27
- memo
46
+ memo.merge(attribute.as_json(self))
28
47
  end
29
48
  end
30
49
 
@@ -32,6 +51,10 @@ module HashSerializer
32
51
  ::JSON.generate(as_json)
33
52
  end
34
53
 
54
+ def formats
55
+ self.class.formats
56
+ end
57
+
35
58
  private
36
59
 
37
60
  def attributes
@@ -1,3 +1,3 @@
1
1
  module HashSerializer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_serializers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Bondarenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-20 00:00:00.000000000 Z
11
+ date: 2019-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler