Class: ActiveModel::Serializers::Xml::Serializer
- Inherits:
-
Object
- Object
- ActiveModel::Serializers::Xml::Serializer
- Defined in:
- lib/active_model/serializers/xml.rb
Overview
:nodoc:
Defined Under Namespace
Classes: Attribute, MethodAttribute
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#attributes_hash ⇒ Object
To replicate the behavior in ActiveRecord#attributes,
:except
takes precedence over:only
. -
#initialize(serializable, options = nil) ⇒ Serializer
constructor
A new instance of Serializer.
- #serializable_attributes ⇒ Object
- #serializable_methods ⇒ Object
- #serialize ⇒ Object
Constructor Details
#initialize(serializable, options = nil) ⇒ Serializer
Returns a new instance of Serializer.
49 50 51 52 53 54 55 |
# File 'lib/active_model/serializers/xml.rb', line 49 def initialize(serializable, = nil) @serializable = serializable @options = ? .dup : {} @options[:only] = Array.wrap(@options[:only]).map { |n| n.to_s } @options[:except] = Array.wrap(@options[:except]).map { |n| n.to_s } end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
47 48 49 |
# File 'lib/active_model/serializers/xml.rb', line 47 def @options end |
Instance Method Details
#attributes_hash ⇒ Object
To replicate the behavior in ActiveRecord#attributes, :except
takes precedence over :only
. If :only
is not set for a N level model but is set for the N+1 level models, then because :except
is set to a default value, the second level model can have both :except
and :only
set. So if :only
is set, always delete :except
.
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_model/serializers/xml.rb', line 63 def attributes_hash attributes = @serializable.attributes if [:only].any? attributes.slice(*[:only]) elsif [:except].any? attributes.except(*[:except]) else attributes end end |
#serializable_attributes ⇒ Object
74 75 76 77 78 |
# File 'lib/active_model/serializers/xml.rb', line 74 def serializable_attributes attributes_hash.map do |name, value| self.class::Attribute.new(name, @serializable, value) end end |
#serializable_methods ⇒ Object
80 81 82 83 84 |
# File 'lib/active_model/serializers/xml.rb', line 80 def serializable_methods Array.wrap([:methods]).map do |name| self.class::MethodAttribute.new(name.to_s, @serializable) if @serializable.respond_to?(name.to_s) end.compact end |
#serialize ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/active_model/serializers/xml.rb', line 86 def serialize require 'builder' unless defined? ::Builder [:indent] ||= 2 [:builder] ||= ::Builder::XmlMarkup.new(:indent => [:indent]) @builder = [:builder] @builder.instruct! unless [:skip_instruct] root = ([:root] || @serializable.class.model_name.element).to_s root = ActiveSupport::XmlMini.rename_key(root, ) args = [root] args << {:xmlns => [:namespace]} if [:namespace] args << {:type => [:type]} if [:type] && ![:skip_types] @builder.tag!(*args) do add_attributes_and_methods add_extra_behavior add_procs yield @builder if block_given? end end |