Module: Representable
- Included in:
- Decorator
- Defined in:
- lib/representable.rb,
lib/representable/xml.rb,
lib/representable/hash.rb,
lib/representable/json.rb,
lib/representable/yaml.rb,
lib/representable/binding.rb,
lib/representable/version.rb,
lib/representable/decorator.rb,
lib/representable/definition.rb,
lib/representable/deprecations.rb,
lib/representable/hash_methods.rb,
lib/representable/bindings/xml_bindings.rb,
lib/representable/bindings/hash_bindings.rb,
lib/representable/bindings/yaml_bindings.rb,
lib/representable/feature/readable_writeable.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, following the DCI pattern.
module HeroRepresenter
include Representable::JSON
property :name
end
hero.extend(HeroRepresenter).to_json
Defined Under Namespace
Modules: ClassInclusions, ClassMethods, Coercion, Deprecations, Feature, Hash, HashMethods, JSON, ModuleExtensions, XML, YAML Classes: Binding, Config, Decorator, Definition
Constant Summary collapse
- VERSION =
"1.5.0"
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
30 31 32 |
# File 'lib/representable.rb', line 30 def representable_attrs=(value) @representable_attrs = value end |
Class Method Details
.included(base) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/representable.rb', line 32 def self.included(base) base.class_eval do extend ClassInclusions, ModuleExtensions extend ClassMethods extend ClassMethods::Declarations include Deprecations include Feature::ReadableWriteable end end |
Instance Method Details
#update_properties_from(doc, options, format) ⇒ Object
Reads values from doc
and sets properties accordingly.
44 45 46 47 48 49 |
# File 'lib/representable.rb', line 44 def update_properties_from(doc, , format) representable_bindings_for(format, ).each do |bin| deserialize_property(bin, doc, ) end self end |