Class: Oat::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/oat/serializer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, context = {}, _adapter_class = nil, parent_serializer = nil) ⇒ Serializer

Returns a new instance of Serializer.



24
25
26
27
28
29
# File 'lib/oat/serializer.rb', line 24

def initialize(item, context = {}, _adapter_class = nil, parent_serializer = nil)
  @item, @context = item, context
  @parent_serializer = parent_serializer
  @adapter_class = _adapter_class || self.class.adapter
  @adapter = @adapter_class.new(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/oat/serializer.rb', line 35

def method_missing(name, *args, &block)
  if adapter.respond_to?(name)
    adapter.send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



22
23
24
# File 'lib/oat/serializer.rb', line 22

def adapter
  @adapter
end

#adapter_classObject (readonly)

Returns the value of attribute adapter_class.



22
23
24
# File 'lib/oat/serializer.rb', line 22

def adapter_class
  @adapter_class
end

#contextObject (readonly)

Returns the value of attribute context.



22
23
24
# File 'lib/oat/serializer.rb', line 22

def context
  @context
end

#itemObject (readonly)

Returns the value of attribute item.



22
23
24
# File 'lib/oat/serializer.rb', line 22

def item
  @item
end

Class Method Details

.adapter(adapter_class = nil) ⇒ Object



13
14
15
16
# File 'lib/oat/serializer.rb', line 13

def self.adapter(adapter_class = nil)
  self._adapter = adapter_class if adapter_class
  self._adapter
end

.schema(&block) ⇒ Object



9
10
11
# File 'lib/oat/serializer.rb', line 9

def self.schema(&block)
  self.schemas += [block] if block_given?
end

.warn(msg) ⇒ Object



18
19
20
# File 'lib/oat/serializer.rb', line 18

def self.warn(msg)
  logger ? logger.warning(msg) : Kernel.warn(msg)
end

Instance Method Details

#map_properties(*args) ⇒ Object



63
64
65
# File 'lib/oat/serializer.rb', line 63

def map_properties(*args)
  args.each { |name| map_property name }
end

#map_property(name) ⇒ Object



67
68
69
70
# File 'lib/oat/serializer.rb', line 67

def map_property(name)
  value = item.send(name)
  property name, value
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/oat/serializer.rb', line 49

def respond_to_missing?(method_name, include_private = false)
  adapter.respond_to? method_name
end

#to_hashObject



53
54
55
56
57
58
59
60
61
# File 'lib/oat/serializer.rb', line 53

def to_hash
  @to_hash ||= (
    self.class.schemas.each do |schema|
      instance_eval(&schema)
    end

    adapter.to_hash
  )
end

#topObject



31
32
33
# File 'lib/oat/serializer.rb', line 31

def top
  @top ||= @parent_serializer || self
end

#type(*args) ⇒ Object



43
44
45
46
47
# File 'lib/oat/serializer.rb', line 43

def type(*args)
  if adapter.respond_to?(:type) && adapter.method(:type).arity != 0
    adapter.type(*args)
  end
end