Class: ActiveModel::VersionSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/version_serializer.rb,
lib/active_model/version_serializers/version.rb

Constant Summary collapse

VERSION =
"0.0.5"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ VersionSerializer

Returns a new instance of VersionSerializer.



18
19
20
21
22
23
# File 'lib/active_model/version_serializer.rb', line 18

def initialize(object, options = {})
  @object, @options = object, options
  @version = @options[:version] || _default
  @vklass = self._versions[@version]
  @vinstance = @vklass.new(@object, @options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/active_model/version_serializer.rb', line 97

def method_missing(method, *args)
  if @vinstance.respond_to?(method)
    @vinstance.send(method, *args)
  else
    super
  end
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



16
17
18
# File 'lib/active_model/version_serializer.rb', line 16

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/active_model/version_serializer.rb', line 16

def options
  @options
end

#versionObject (readonly)

Returns the value of attribute version.



16
17
18
# File 'lib/active_model/version_serializer.rb', line 16

def version
  @version
end

#vklassObject (readonly)

Returns the value of attribute vklass.



16
17
18
# File 'lib/active_model/version_serializer.rb', line 16

def vklass
  @vklass
end

Class Method Details

.default(version) ⇒ Object



25
26
27
# File 'lib/active_model/version_serializer.rb', line 25

def self.default(version)
  self._default = version
end

.inherited(klass) ⇒ Object

Replication of inherited from active_model_serializer but we get the name so we can define the correct name on each version

of defined active model serializers



38
39
40
41
42
43
44
45
46
# File 'lib/active_model/version_serializer.rb', line 38

def self.inherited(klass) #:nodoc:
  return if klass.anonymous?
  name = klass.name.demodulize.underscore.sub(/_serializer$/, '')
  self._name = name
  klass.class_eval do
    alias_method name.to_sym, :object
    root name.to_sym unless self._root == false
  end
end

.root(name) ⇒ Object

Defines the root used on serialization. If false, disables the root.



30
31
32
# File 'lib/active_model/version_serializer.rb', line 30

def self.root(name)
  self._root = name
end

.version(version, superclass = ActiveModel::Serializer, &block) ⇒ Object

creates a anoymous subclass of ActiveModel::Serializer setting the root from base alias of the serialbe object with the name

version_attributes so less typing if the versions are similar options :with and :without.

 @param [Symbol] version

Examples:

version :v1 do
  attributes :beans, :eggs
end

version :v2 do
  version_attributes :v1, with: :toast
end

Parameters:

  • block (Proc)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/active_model/version_serializer.rb', line 66

def self.version(version, superclass = ActiveModel::Serializer, &block)
  base_class = self
  vklass = Class.new(superclass) do
    self.root(base_class._root)
    alias_method base_class._name.to_sym, :object

    singleton_class.class_eval do
      define_method(:to_s) do
        "(#{base_class.name} VERSION: #{version})"
      end
      alias inspect to_s
    end

    define_singleton_method(:version_attributes) do |v, options = {}|
      version_attributes = base_class._versions[v]._attributes.keys
      version_options = options.extract!(:without, :with)
      if with_attributes = version_options[:with]
        version_attributes = (version_attributes + [*with_attributes])
      end
      if without_attributes = version_options[:without]
        version_attributes = (version_attributes - [*without_attributes])
      end
      attributes(*version_attributes)
    end
    self.class_eval(&block)
  end

  # mutables with class attribute use setters!
  self._versions = self._versions.merge({version => vklass})
end

Instance Method Details

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

lets be nice to method

Returns:

  • (Boolean)


106
107
108
# File 'lib/active_model/version_serializer.rb', line 106

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