Class: ActiveModel::Serializers::Xml::Serializer
- Inherits:
-
Object
- Object
- ActiveModel::Serializers::Xml::Serializer
- Defined in:
- activemodel/lib/active_model/serializers/xml.rb
Overview
:nodoc:
Instance Attribute Summary (collapse)
-
- (Object) options
readonly
Returns the value of attribute options.
Instance Method Summary (collapse)
-
- (Object) attributes_hash
To replicate the behavior in ActiveRecord#attributes, :except takes precedence over :only.
-
- (Serializer) initialize(serializable, options = nil)
constructor
A new instance of Serializer.
- - (Object) serializable_attributes
- - (Object) serializable_methods
- - (Object) serialize
Constructor Details
- (Serializer) initialize(serializable, options = nil)
A new instance of Serializer
47 48 49 50 51 52 53 |
# File 'activemodel/lib/active_model/serializers/xml.rb', line 47 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
- (Object) options (readonly)
Returns the value of attribute options
45 46 47 |
# File 'activemodel/lib/active_model/serializers/xml.rb', line 45 def @options end |
Instance Method Details
- (Object) attributes_hash
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.
61 62 63 64 65 66 67 68 69 70 |
# File 'activemodel/lib/active_model/serializers/xml.rb', line 61 def attributes_hash attributes = @serializable.attributes if [:only].any? attributes.slice(*[:only]) elsif [:except].any? attributes.except(*[:except]) else attributes end end |
- (Object) serializable_attributes
72 73 74 75 76 |
# File 'activemodel/lib/active_model/serializers/xml.rb', line 72 def serializable_attributes attributes_hash.map do |name, value| self.class::Attribute.new(name, @serializable, value) end end |
- (Object) serializable_methods
78 79 80 81 82 83 |
# File 'activemodel/lib/active_model/serializers/xml.rb', line 78 def serializable_methods Array.wrap([:methods]).inject([]) do |methods, name| methods << self.class::MethodAttribute.new(name.to_s, @serializable) if @serializable.respond_to?(name.to_s) methods end end |
- (Object) serialize
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'activemodel/lib/active_model/serializers/xml.rb', line 85 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 |