Module: SoftAttributes::Base

Defined in:
lib/soft_attributes/base.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/soft_attributes/base.rb', line 3

def self.included(base)
  base.class_eval do
    def to_xml_with_soft_attributes(opts={})
      attributes_to_include = self.soft_attributes.clone.with_indifferent_access.collect do |k, v|
        include_in_xml = v[:include_in_xml]
        if include_in_xml.present?
          k if (include_in_xml.is_a?(Proc) ? include_in_xml.call(self) : include_in_xml == true)
        end
      end.compact
      to_xml_without_soft_attributes(opts) do |xml|
        attributes_to_include.each do |attr|
          xml.tag!(attr, self.send(attr))
        end
      end
    end
    alias_method_chain :to_xml, :soft_attributes

    # TODO: Should we support to_json here?

    extend SoftAttributes::Base::ClassMethods
    include SoftAttributes::Base::InstanceMethods
  end
end