rice_bubble 0.1.0 → 0.1.1

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: 4e81d5917da322d30c15b8def2c989d5b725daae03a2b843cb88534f273b6f49
4
- data.tar.gz: acf5daa7c14f1a059db017ffa3a2dddf91d674b6b5694a5b0b6506e3abe8159e
3
+ metadata.gz: 4a6ac1880406c9b129d4d9f0fa1029acc21a46cb1a3c9be1feaece000fc99e5c
4
+ data.tar.gz: 6f684b2691b37a6ccb30cd73aa4d45ab689546377f184e3fb33d8e5eab8e22e7
5
5
  SHA512:
6
- metadata.gz: c6311322cd18622ebf65dc0dc0e78cf9e89844e5cd3cc9f3564165400a3d9af70d447073025093532cdbdfeadbcc84008756a61d628c239f0de2756710f6dea2
7
- data.tar.gz: 5c025fa2266a0ba755d946a6fc259164609f6549d5f5720fcb36c09790ca86f2c8ebd0a1c27e6dd37d01b3c08c6858c32866f644c67eacbb03c39fb709c97081
6
+ metadata.gz: 4936fab7ed2f4a4d5ba5e044606f11ce6311e51b287f4bcdc4024113b90d2ae0e2323eb63af42b7a860bc9ccba9b4c1bc785ebfd555d8e49fd7e7af57862a261
7
+ data.tar.gz: 04fb49f5fd9e425b2df7311ef7301e6613a279301de442047b171a90a2ee1b063d4de117255c4f75a2e01e96de458220be06ba2704b5eed3f85fa5313fb19b6a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rice_bubble (0.1.0)
4
+ rice_bubble (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -37,9 +37,11 @@ end
37
37
  Then you can use the new serializer to convert your data to a JSON-friendly representation:
38
38
 
39
39
  ```ruby
40
- json = CharacterSerializer.call(my_character) # => { name: "...", ... }
40
+ json = CharacterSerializer.new(my_character).call # => { name: "...", ... }
41
41
  ```
42
42
 
43
+ **Note:** you can also use `CharacterSerializer.call(my_character)`.
44
+
43
45
  ### Attribute types
44
46
 
45
47
  | Name | Expects | Options | Example |
@@ -100,12 +102,25 @@ WrappingSerializer.call({ name: 'Spicy Beef' }) # => { name: '{Spicy Beef}' }
100
102
  ### Fetching values from an object
101
103
 
102
104
  By default, attributes know how to fetch their values from the object being serialized
103
- by either:
105
+ by either (in this order):
106
+
107
+ - invoking an instance method with the same name on the serializer; or
108
+ - invoking an instance method with the same name on the object; or
109
+ - looking up a hash key on the object
110
+
111
+ ```ruby
112
+ class FullNameSerializer
113
+ attributes(
114
+ full_name: string
115
+ )
104
116
 
105
- - invoking an instance method (on an object); or
106
- - looking up a hash key (on a hash)
117
+ def full_name
118
+ [object.first_name, object.last_name].join(' ')
119
+ end
120
+ end
121
+ ```
107
122
 
108
- You can change how a value is retrieved by passing a block to the attribute:
123
+ You can also change how a value is retrieved by passing a block to the attribute:
109
124
 
110
125
  ```ruby
111
126
  class FullNameSerializer
@@ -134,4 +149,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
134
149
 
135
150
  ## Code of Conduct
136
151
 
137
- Everyone interacting in the RiceBubble project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rice_bubble/blob/main/CODE_OF_CONDUCT.md).
152
+ Everyone interacting in the RiceBubble project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://gitlab.com/fauxparse/rice_bubble/blob/main/CODE_OF_CONDUCT.md).
@@ -1,17 +1,33 @@
1
1
  module RiceBubble
2
2
  class Serializer
3
- def call(object, path: nil)
4
- attributes.map do |name, attr|
5
- attr.call(attr.fetch(object, name), path: path || self.class.name)
3
+ attr_reader :object
4
+
5
+ def initialize(object = nil)
6
+ @object = object
7
+ end
8
+
9
+ def call(object_to_serialize = nil, path: nil)
10
+ if object_to_serialize
11
+ self.class.new(object_to_serialize).call(path:)
12
+ elsif object
13
+ self.class.attributes.map do |name, attr|
14
+ attr.call(fetch(name), path: path || self.class.name)
15
+ end
16
+ else
17
+ raise ArgumentError, 'no object to serialize'
6
18
  end
7
19
  end
8
20
 
9
- def attributes
10
- self.class.attributes
21
+ def fetch(name)
22
+ if respond_to?(name)
23
+ public_send(name)
24
+ else
25
+ self.class.attributes[name].fetch(object, name)
26
+ end
11
27
  end
12
28
 
13
29
  def valid?(object)
14
- attributes.all? do |name, attr|
30
+ self.class.attributes.all? do |name, attr|
15
31
  attr.valid?(attr.fetch(object, name))
16
32
  end
17
33
  end
@@ -19,7 +35,7 @@ module RiceBubble
19
35
  def validate!(object, path: '', **)
20
36
  path = self.class.name if path.empty?
21
37
 
22
- attributes.each do |name, attr|
38
+ self.class.attributes.each do |name, attr|
23
39
  value = attr.fetch(object, name)
24
40
  attr.validate!(
25
41
  value,
@@ -34,8 +50,8 @@ module RiceBubble
34
50
  end
35
51
 
36
52
  class << self
37
- def call(...)
38
- new.call(...)
53
+ def call(object = nil, ...)
54
+ new(object).call(...)
39
55
  end
40
56
 
41
57
  def attributes(attrs = nil, &)
@@ -1,3 +1,3 @@
1
1
  module RiceBubble
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rice_bubble
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Powell