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 = nil, _adapter_class = nil, parent_serializer = nil) ⇒ Serializer

Returns a new instance of Serializer.



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

def initialize(item, context = nil, _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



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

def method_missing(name, *args, &block)
  if adapter.respond_to?(name)
    adapter.send(name, *args, &block)
  else
    self.class.warn "[#{adapter.class}] does not implement ##{name}. Called with #{args}"
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#adapter_classObject (readonly)

Returns the value of attribute adapter_class.



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

def adapter_class
  @adapter_class
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#itemObject (readonly)

Returns the value of attribute item.



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

def item
  @item
end

Class Method Details

.adapter(adapter_class = nil) ⇒ Object



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

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

.schema(&block) ⇒ Object



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

def self.schema(&block)
  @schema = block if block_given?
  @schema || Proc.new{}
end

.warn(msg) ⇒ Object



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

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

Instance Method Details

#map_properties(*args) ⇒ Object



53
54
55
# File 'lib/oat/serializer.rb', line 53

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

#map_property(name) ⇒ Object



57
58
59
60
# File 'lib/oat/serializer.rb', line 57

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

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

Returns:

  • (Boolean)


42
43
44
# File 'lib/oat/serializer.rb', line 42

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

#to_hashObject



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

def to_hash
  @to_hash ||= (
    self.instance_eval &self.class.schema
    adapter.to_hash
  )
end

#topObject



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

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