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.3"
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.
- #method_missing(method, *args) ⇒ Object
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
lets be nice to method.
Constructor Details
#initialize(object, options = {}) ⇒ VersionSerializer
Returns a new instance of VersionSerializer.
17 18 19 20 21 22 |
# File 'lib/active_model/version_serializer.rb', line 17 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
96 97 98 99 100 101 102 |
# File 'lib/active_model/version_serializer.rb', line 96 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.
15 16 17 |
# File 'lib/active_model/version_serializer.rb', line 15 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/active_model/version_serializer.rb', line 15 def @options end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
15 16 17 |
# File 'lib/active_model/version_serializer.rb', line 15 def version @version end |
#vklass ⇒ Object (readonly)
Returns the value of attribute vklass.
15 16 17 |
# File 'lib/active_model/version_serializer.rb', line 15 def vklass @vklass end |
Class Method Details
.default(version) ⇒ Object
24 25 26 |
# File 'lib/active_model/version_serializer.rb', line 24 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
37 38 39 40 41 42 43 44 45 |
# File 'lib/active_model/version_serializer.rb', line 37 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.
29 30 31 |
# File 'lib/active_model/version_serializer.rb', line 29 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
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 90 91 92 93 94 |
# File 'lib/active_model/version_serializer.rb', line 65 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 |
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
lets be nice to method
105 106 107 |
# File 'lib/active_model/version_serializer.rb', line 105 def respond_to_missing?(method_name, include_private = false) @vinstance.respond_to_missing?(method_name, include_private) end |