Class: RubySerializer::Field
- Inherits:
-
Object
- Object
- RubySerializer::Field
- Defined in:
- lib/ruby_serializer/field.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(field, namespace, options) ⇒ Field
constructor
A new instance of Field.
- #only?(resource, serializer) ⇒ Boolean
- #present?(resource, serializer) ⇒ Boolean
- #serialize(resource, serializer) ⇒ Object
- #unless?(resource, serializer) ⇒ Boolean
Constructor Details
#initialize(field, namespace, options) ⇒ Field
Returns a new instance of Field.
6 7 8 9 10 11 12 13 14 |
# File 'lib/ruby_serializer/field.rb', line 6 def initialize(field, namespace, ) @field = field.to_sym @as = [:as] || @field @from = [:from] || @field @value = [:value] @only = [:only] @unless = [:unless] @namespace = namespace.dup end |
Instance Attribute Details
#as ⇒ Object (readonly)
Returns the value of attribute as.
4 5 6 |
# File 'lib/ruby_serializer/field.rb', line 4 def as @as end |
#field ⇒ Object (readonly)
Returns the value of attribute field.
4 5 6 |
# File 'lib/ruby_serializer/field.rb', line 4 def field @field end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
4 5 6 |
# File 'lib/ruby_serializer/field.rb', line 4 def from @from end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
4 5 6 |
# File 'lib/ruby_serializer/field.rb', line 4 def namespace @namespace end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
4 5 6 |
# File 'lib/ruby_serializer/field.rb', line 4 def value @value end |
Instance Method Details
#only?(resource, serializer) ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruby_serializer/field.rb', line 30 def only?(resource, serializer) case @only when nil then true when true then true when false then false when Symbol then resource.send(@only) when Proc then serializer.instance_exec(&@only) else raise ArgumentError, "unexpected #{@only}" end end |
#present?(resource, serializer) ⇒ Boolean
26 27 28 |
# File 'lib/ruby_serializer/field.rb', line 26 def present?(resource, serializer) only?(resource, serializer) && !unless?(resource, serializer) end |
#serialize(resource, serializer) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/ruby_serializer/field.rb', line 16 def serialize(resource, serializer) case value when nil then resource.send(from) when Symbol then resource.send(value) when Proc then serializer.instance_exec(&value) else value end end |
#unless?(resource, serializer) ⇒ Boolean
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ruby_serializer/field.rb', line 42 def unless?(resource, serializer) case @unless when nil then false when true then true when false then false when Symbol then resource.send(@unless) when Proc then serializer.instance_exec(&@unless) else raise ArgumentError, "unexpected #{@unless}" end end |