Class: Ser::Ializer::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/ser/ializer/field.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options, config, &block)
  @name = name
  @setter = options[:setter] || "#{name}="
  @key = options[:key] || Field.transform(name.to_s, config.key_transformer)
  @deser = options[:deser]
  @if_condition = options[:if]
  @model_class = options[:model_class]
  @description = options[:desc]
  @documentation = options[:documentation]
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



14
15
16
# File 'lib/ser/ializer/field.rb', line 14

def block
  @block
end

#descriptionObject (readonly)

Returns the value of attribute description.



14
15
16
# File 'lib/ser/ializer/field.rb', line 14

def description
  @description
end

#deserObject (readonly)

Returns the value of attribute deser.



14
15
16
# File 'lib/ser/ializer/field.rb', line 14

def deser
  @deser
end

#documentationObject (readonly)

Returns the value of attribute documentation.



14
15
16
# File 'lib/ser/ializer/field.rb', line 14

def documentation
  @documentation
end

#if_conditionObject (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

#keyObject (readonly)

Returns the value of attribute key.



14
15
16
# File 'lib/ser/ializer/field.rb', line 14

def key
  @key
end

#model_classObject (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

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/ser/ializer/field.rb', line 14

def name
  @name
end

#setterObject (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

Returns:

  • (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