Module: TracksAttributes::TracksAttributesInternal
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/tracks_attributes.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#accessors ⇒ Object
Return the array of accessor symbols for instances of this class.
-
#all_attributes ⇒ Object
Return a hash all of the accessor symbols and their values.
-
#all_attributes=(hash = {}) ⇒ Object
Set all attributes with hash of symbols and their values and returns instance.
-
#from_json(json, include_root = false) ⇒ Object
Returns an instance of TracksAttributes from a JSON string.
-
#from_xml(xml) ⇒ Object
Returns an instance of TracksAttributes from an XML string.
-
#to_json(options = nil) ⇒ Object
Convert an TracksAttributes instance to JSON by delegating conversion to Hash.to_json.
-
#to_xml(options = nil) ⇒ Object
Convert an TracksAttributes instance to XML by delegating conversion to Hash.to_xml.
Instance Method Details
#accessors ⇒ Object
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_attributes ⇒ Object
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
179 180 181 |
# File 'lib/tracks_attributes.rb', line 179 def to_json( = nil) all_attributes.to_json() 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
200 201 202 |
# File 'lib/tracks_attributes.rb', line 200 def to_xml( = nil) all_attributes.to_xml( || {}) end |