Module: Representable
- Defined in:
- lib/representable.rb,
lib/representable/xml.rb,
lib/representable/json.rb,
lib/representable/binding.rb,
lib/representable/version.rb,
lib/representable/definition.rb,
lib/representable/deprecations.rb,
lib/representable/bindings/xml_bindings.rb,
lib/representable/bindings/json_bindings.rb
Overview
Representable can be used in two ways.
On class level
To try out Representable you might include the format module into the represented class directly and then define the properties.
class Hero < ActiveRecord::Base
include Representable::JSON
property :name
This will give you to_/from_json for each instance. However, this approach limits your class to one representation.
On module level
Modules give you much more flexibility since you can mix them into objects at runtime, roughly following the DCI pattern.
module HeroRepresenter
include Representable::JSON
property :name
end
hero.extend(HeroRepresenter).to_json
Defined Under Namespace
Modules: ClassMethods, Coercion, Deprecations, JSON, XML Classes: Binding, Config, Definition
Constant Summary collapse
- VERSION =
"1.2.1"
Instance Attribute Summary collapse
-
#representable_attrs ⇒ Object
writeonly
Sets the attribute representable_attrs.
Class Method Summary collapse
Instance Method Summary collapse
-
#update_properties_from(doc, options, format) ⇒ Object
Reads values from
doc
and sets properties accordingly.
Instance Attribute Details
#representable_attrs=(value) ⇒ Object
Sets the attribute representable_attrs
29 30 31 |
# File 'lib/representable.rb', line 29 def representable_attrs=(value) @representable_attrs = value end |
Class Method Details
.included(base) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/representable.rb', line 31 def self.included(base) base.class_eval do include Deprecations extend ClassMethods extend ClassMethods::Declarations extend ClassMethods::Accessors def self.included(base) base.representable_attrs.push(*representable_attrs.clone) # "inherit". end # Copies the representable_attrs to the extended object. def self.extended(object) object.representable_attrs=(representable_attrs) end end end |
Instance Method Details
#update_properties_from(doc, options, format) ⇒ Object
Reads values from doc
and sets properties accordingly.
50 51 52 53 54 55 56 57 |
# File 'lib/representable.rb', line 50 def update_properties_from(doc, , format) representable_bindings_for(format).each do |bin| next if skip_property?(bin, ) uncompile_fragment(bin, doc) end self end |