Module: TracksAttributes::TracksAttributesInternal

Extended by:
ActiveSupport::Concern
Defined in:
lib/tracks_attributes.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#accessorsObject

Return the array of accessor symbols for instances of this class.



156
157
158
# File 'lib/tracks_attributes.rb', line 156

def accessors
  self.class.accessors
end

#all_attributesObject

Return a hash all of the accessor symbols and their values



161
162
163
164
# File 'lib/tracks_attributes.rb', line 161

def all_attributes
  the_attrs = Hash[accessors.collect {|v| [v, send(v.to_s)] if respond_to? "#{v}".to_sym}]
  (respond_to?(:attributes) && attributes.merge(the_attrs)) || the_attrs
end

#all_attributes=(hash = {}) ⇒ Object

Set all attributes with hash of symbols and their values and returns instance



167
168
169
170
# File 'lib/tracks_attributes.rb', line 167

def all_attributes=(hash = {})
  hash.each { |k, v| set_attribute(k, v) }
  self
end

#from_json(json, include_root = false) ⇒ Object

Returns an instance of TracksAttributes from a JSON string



184
185
186
187
188
# File 'lib/tracks_attributes.rb', line 184

def from_json(json, include_root=false)
  hash = ActiveSupport::JSON.decode(json)
  hash = hash.values.first if include_root
  self.all_attributes = hash
end

#from_xml(xml) ⇒ Object

Returns an instance of TracksAttributes from an XML string



205
206
207
208
# File 'lib/tracks_attributes.rb', line 205

def from_xml(xml)
  hash = Hash.from_xml(xml).values.first
  self.all_attributes = hash
end

#to_json(options = nil) ⇒ Object

Convert an TracksAttributes instance to JSON by delegating conversion to Hash.to_json

Parameters:

  • options (Hash) (defaults to: nil)
    • Without any options, the returned JSON string will include all of the attributes.

    • :only => one, or an array of Hash Keys that define which keys to process

    • :except => one, or an array of Hash Keys that define which keys not to process



179
180
181
# File 'lib/tracks_attributes.rb', line 179

def to_json(options = nil)
  all_attributes.to_json(options)
end

#to_xml(options = nil) ⇒ Object

Convert an TracksAttributes instance to XML by delegating conversion to Hash.to_xml

The :builder option uses key as the :root for the XML result

Parameters:

  • options (Hash) (defaults to: nil)
    • Without any options, the returned XML string will include all of the attributes.

    • :only => one, or an array of Hash Keys that define which keys to process

    • :except => one, or an array of Hash Keys that define which keys not to process



200
201
202
# File 'lib/tracks_attributes.rb', line 200

def to_xml(options = nil)
  all_attributes.to_xml(options || {})
end