Class: ActiveModel::VersionSerializer
- Inherits:
-
Object
- Object
- ActiveModel::VersionSerializer
- Defined in:
- lib/active_model/version_serializer.rb,
lib/active_model/version_serializers/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
-
#vklass ⇒ Object
readonly
Returns the value of attribute vklass.
Class Method Summary collapse
- .default(version) ⇒ Object
-
.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.
-
.root(name) ⇒ Object
Defines the root used on serialization.
-
.version(version, &block) ⇒ Object
creates a anoymous subclass of ActiveModel::Serializer setting the root from base alias of the serialbe object with the name.
Instance Method Summary collapse
-
#initialize(object, options = {}) ⇒ VersionSerializer
constructor
A new instance of VersionSerializer.
Constructor Details
#initialize(object, options = {}) ⇒ VersionSerializer
Returns a new instance of VersionSerializer.
12 13 14 15 16 17 |
# File 'lib/active_model/version_serializer.rb', line 12 def initialize(object, = {}) @object, @options = object, @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 (private)
92 93 94 95 96 97 98 |
# File 'lib/active_model/version_serializer.rb', line 92 def method_missing(method, *args) if @vinstance.respond_to?(method) @vinstance.send(method, *args) else super end end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
10 11 12 |
# File 'lib/active_model/version_serializer.rb', line 10 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/active_model/version_serializer.rb', line 10 def @options end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
10 11 12 |
# File 'lib/active_model/version_serializer.rb', line 10 def version @version end |
#vklass ⇒ Object (readonly)
Returns the value of attribute vklass.
10 11 12 |
# File 'lib/active_model/version_serializer.rb', line 10 def vklass @vklass end |
Class Method Details
.default(version) ⇒ Object
19 20 21 |
# File 'lib/active_model/version_serializer.rb', line 19 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
32 33 34 35 36 37 38 39 40 |
# File 'lib/active_model/version_serializer.rb', line 32 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.
24 25 26 |
# File 'lib/active_model/version_serializer.rb', line 24 def self.root(name) self._root = name end |
.version(version, &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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/active_model/version_serializer.rb', line 60 def self.version(version, &block) base_class = self vklass = Class.new(ActiveModel::Serializer) 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, = {}| version_attributes = base_class._versions[v]._attributes.keys = .extract!(:without, :with) if with_attributes = [:with] version_attributes = (version_attributes + [*with_attributes]) end if without_attributes = [: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 |