Class: EasySerializer::Field

Inherits:
Struct
  • Object
show all
Includes:
Helpers
Defined in:
lib/easy_serializer/field.rb

Direct Known Subclasses

Attribute, Collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#option_to_value

Instance Attribute Details

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



2
3
4
# File 'lib/easy_serializer/field.rb', line 2

def block
  @block
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/easy_serializer/field.rb', line 2

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



2
3
4
# File 'lib/easy_serializer/field.rb', line 2

def options
  @options
end

Instance Method Details

#cache_keyObject



30
31
32
# File 'lib/easy_serializer/field.rb', line 30

def cache_key
  options[:cache_key]
end

#cache_optionsObject



26
27
28
# File 'lib/easy_serializer/field.rb', line 26

def cache_options
  options[:cache_options]
end

#catch?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/easy_serializer/field.rb', line 17

def catch?
  options[:cache]
end

#get_valueObject



5
6
7
8
9
10
11
# File 'lib/easy_serializer/field.rb', line 5

def get_value
  if block
    block
  else
    method(:send_method)
  end
end

#nested_serializerObject



34
35
36
# File 'lib/easy_serializer/field.rb', line 34

def nested_serializer
  @serializer
end

#send_method(obj) ⇒ Object



13
14
15
# File 'lib/easy_serializer/field.rb', line 13

def send_method(obj)
  obj.send(name)
end

#serialize!(object, serializer_instance) ⇒ Object

Important!!! DO NOT memoize this method



45
46
47
48
49
# File 'lib/easy_serializer/field.rb', line 45

def serialize!(object, serializer_instance)
  serializer = serializer(object, serializer_instance)
  return unless serializer
  serializer.call(object)
end

#serializer(object, serializer_instance) ⇒ Object Also known as: nested_serializer=



38
39
40
41
# File 'lib/easy_serializer/field.rb', line 38

def serializer(object, serializer_instance)
  return unless serializer?
  option_to_value(options[:serializer], object, serializer_instance)
end

#serializer?Boolean Also known as: nested_serializer?

Returns:

  • (Boolean)


21
22
23
# File 'lib/easy_serializer/field.rb', line 21

def serializer?
  !!options[:serializer]
end