Class: Ser::Ializer::Field
- Inherits:
-
Object
- Object
- Ser::Ializer::Field
- Defined in:
- lib/ser/ializer/field.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#deser ⇒ Object
readonly
Returns the value of attribute deser.
-
#documentation ⇒ Object
readonly
Returns the value of attribute documentation.
-
#if_condition ⇒ Object
readonly
Returns the value of attribute if_condition.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#setter ⇒ Object
readonly
Returns the value of attribute setter.
Class Method Summary collapse
Instance Method Summary collapse
- #get_value(object, context) ⇒ Object
-
#initialize(name, options, config, &block) ⇒ Field
constructor
A new instance of Field.
- #parse(value) ⇒ Object
- #serialize(value, context) ⇒ Object
- #valid_for_context?(object, context) ⇒ Boolean
Constructor Details
#initialize(name, options, config, &block) ⇒ Field
Returns a new instance of Field.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ser/ializer/field.rb', line 16 def initialize(name, , config, &block) @name = name @setter = [:setter] || "#{name}=" @key = [:key] || Field.transform(name.to_s, config.key_transformer) @deser = [:deser] @if_condition = [:if] @model_class = [:model_class] @description = [:desc] @documentation = [:documentation] @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
14 15 16 |
# File 'lib/ser/ializer/field.rb', line 14 def block @block end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
14 15 16 |
# File 'lib/ser/ializer/field.rb', line 14 def description @description end |
#deser ⇒ Object (readonly)
Returns the value of attribute deser.
14 15 16 |
# File 'lib/ser/ializer/field.rb', line 14 def deser @deser end |
#documentation ⇒ Object (readonly)
Returns the value of attribute documentation.
14 15 16 |
# File 'lib/ser/ializer/field.rb', line 14 def documentation @documentation end |
#if_condition ⇒ Object (readonly)
Returns the value of attribute if_condition.
14 15 16 |
# File 'lib/ser/ializer/field.rb', line 14 def if_condition @if_condition end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
14 15 16 |
# File 'lib/ser/ializer/field.rb', line 14 def key @key end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
14 15 16 |
# File 'lib/ser/ializer/field.rb', line 14 def model_class @model_class end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/ser/ializer/field.rb', line 14 def name @name end |
#setter ⇒ Object (readonly)
Returns the value of attribute setter.
14 15 16 |
# File 'lib/ser/ializer/field.rb', line 14 def setter @setter end |
Class Method Details
.transform(key, key_transformer) ⇒ Object
7 8 9 10 11 |
# File 'lib/ser/ializer/field.rb', line 7 def transform(key, key_transformer) return key unless key_transformer key_transformer.call(key) end |
Instance Method Details
#get_value(object, context) ⇒ Object
28 29 30 31 32 |
# File 'lib/ser/ializer/field.rb', line 28 def get_value(object, context) return object.public_send(name) unless block block.call(object, context) end |
#parse(value) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/ser/ializer/field.rb', line 38 def parse(value) if model_class deser.parse(value, model_class) else deser.parse(value) end end |
#serialize(value, context) ⇒ Object
34 35 36 |
# File 'lib/ser/ializer/field.rb', line 34 def serialize(value, context) deser.serialize(value, context) end |
#valid_for_context?(object, context) ⇒ Boolean
46 47 48 49 50 |
# File 'lib/ser/ializer/field.rb', line 46 def valid_for_context?(object, context) return true if if_condition.nil? if_condition.call(object, context) end |