Module: Representable
- 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/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
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, Hash, HashMethods, JSON, ModuleExtensions, XML, YAML Classes: Binding, Config, Definition
Constant Summary collapse
- VERSION =
"1.2.7"
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 |
# File 'lib/representable.rb', line 31 def self.included(base) base.class_eval do extend ClassInclusions, ModuleExtensions extend ClassMethods extend ClassMethods::Declarations include Deprecations end end |
Instance Method Details
#update_properties_from(doc, options, format) ⇒ Object
Reads values from doc
and sets properties accordingly.
42 43 44 45 46 47 48 49 |
# File 'lib/representable.rb', line 42 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 |