Class: Oat::Serializer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassAttribute

class_attribute, singleton_class, singleton_class?

Constructor Details

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

Returns a new instance of Serializer.



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

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/oat/serializer.rb', line 45

def method_missing(name, *args, &block)
  if adapter.respond_to?(name)
    self.class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
      private

      def #{name}(*args, &block)
        adapter.#{name}(*args, &block)
      end
    RUBY

    send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#adapter_classObject (readonly)

Returns the value of attribute adapter_class.



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

def adapter_class
  @adapter_class
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#itemObject (readonly)

Returns the value of attribute item.



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

def item
  @item
end

Class Method Details

.adapter(adapter_class = nil) ⇒ Object



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

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

.schema(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/oat/serializer.rb', line 11

def self.schema(&block)
  if block_given?
    schema_method_name = :"schema_block_#{self.schema_methods.count}"

    self.schemas += [block]
    self.schema_methods += [schema_method_name]

    define_method(schema_method_name, &block)
    private(schema_method_name)
  end
end

.warn(msg) ⇒ Object



28
29
30
# File 'lib/oat/serializer.rb', line 28

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

Instance Method Details

#map_properties(*args) ⇒ Object



81
82
83
# File 'lib/oat/serializer.rb', line 81

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

#map_property(name) ⇒ Object



85
86
87
88
# File 'lib/oat/serializer.rb', line 85

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

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

Returns:

  • (Boolean)


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

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

#to_hashObject



71
72
73
74
75
76
77
78
79
# File 'lib/oat/serializer.rb', line 71

def to_hash
  @to_hash ||= (
    self.class.schema_methods.each do |schema_method_name|
      send(schema_method_name)
    end

    adapter.to_hash
  )
end

#topObject



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

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

#type(*args) ⇒ Object



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

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