Class: RiceBubble::Serializer
- Inherits:
-
Object
- Object
- RiceBubble::Serializer
- Defined in:
- lib/rice_bubble/serializer.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
- .as ⇒ Object (also: of)
- .attributes(attrs = nil) ⇒ Object
- .call(object = nil) ⇒ Object
- .method_missing(name, *args, **kwargs) ⇒ Object
- .respond_to_missing?(name, include_private = false) ⇒ Boolean
Instance Method Summary collapse
- #call(object_to_serialize = nil, path: nil) ⇒ Object
- #fetch(name) ⇒ Object
-
#initialize(object = nil) ⇒ Serializer
constructor
A new instance of Serializer.
- #valid?(object) ⇒ Boolean
Constructor Details
#initialize(object = nil) ⇒ Serializer
Returns a new instance of Serializer.
5 6 7 |
# File 'lib/rice_bubble/serializer.rb', line 5 def initialize(object = nil) @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
3 4 5 |
# File 'lib/rice_bubble/serializer.rb', line 3 def object @object end |
Class Method Details
.as ⇒ Object Also known as: of
61 62 63 |
# File 'lib/rice_bubble/serializer.rb', line 61 def as(...) serialized(...) end |
.attributes(attrs = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rice_bubble/serializer.rb', line 40 def attributes(attrs = nil, &) @attributes ||= if superclass == Object Attributes.new else superclass.attributes.dup end @attributes.instance_eval(&) if block_given? attrs&.each_pair { |name, attr| @attributes[name] = attr } @attributes end |
.call(object = nil) ⇒ Object
36 37 38 |
# File 'lib/rice_bubble/serializer.rb', line 36 def call(object = nil, ...) new(object).call(...) end |
.method_missing(name, *args, **kwargs) ⇒ Object
57 58 59 |
# File 'lib/rice_bubble/serializer.rb', line 57 def method_missing(name, *args, **kwargs, &) Attributes[name]&.new(*args, **kwargs, &) || super end |
.respond_to_missing?(name, include_private = false) ⇒ Boolean
53 54 55 |
# File 'lib/rice_bubble/serializer.rb', line 53 def respond_to_missing?(name, include_private = false) !Attributes[name].nil? || super end |
Instance Method Details
#call(object_to_serialize = nil, path: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rice_bubble/serializer.rb', line 9 def call(object_to_serialize = nil, path: nil) if object_to_serialize self.class.new(object_to_serialize).call(path:) elsif object self.class.attributes.map do |name, attr| attr.call(fetch(name), path: "#{path || self.class.name}.#{name}") end else raise ArgumentError, 'no object to serialize' end end |
#fetch(name) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/rice_bubble/serializer.rb', line 21 def fetch(name) if respond_to?(name) public_send(name) else self.class.attributes[name].fetch(object, name) end end |
#valid?(object) ⇒ Boolean
29 30 31 32 33 |
# File 'lib/rice_bubble/serializer.rb', line 29 def valid?(object) self.class.attributes.all? do |name, attr| attr.valid?(attr.fetch(object, name)) end end |