lib/representable.rb in representable-1.2.2 vs lib/representable.rb in representable-1.2.3

- old
+ new

@@ -14,11 +14,11 @@ # # 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 +# 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 @@ -28,23 +28,15 @@ module Representable attr_writer :representable_attrs def self.included(base) base.class_eval do - include Deprecations + extend ClassInclusions, ModuleExtensions 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 + include Deprecations end end # Reads values from +doc+ and sets properties accordingly. def update_properties_from(doc, options, format) @@ -126,32 +118,53 @@ def representation_wrap representable_attrs.wrap_for(self.class.name) end - module ClassMethods # :nodoc: + module ClassInclusions + def included(base) + super + base.representable_attrs.push(*representable_attrs.clone) # "inherit". + end + end + + module ModuleExtensions + # Copies the representable_attrs to the extended object. + def extended(object) + super + object.representable_attrs=(representable_attrs) + end + end + + + module ClassMethods # Create and yield object and options. Called in .from_json and friends. def create_represented(document, *args) new.tap do |represented| yield represented, *args if block_given? end end + module Declarations - def definition_class - Definition + def representable_attrs + @representable_attrs ||= Config.new end + def representation_wrap=(name) + representable_attrs.wrap = name + end + # Declares a represented document node, which is usually a XML tag or a JSON key. # # Examples: # # property :name # property :name, :from => :title # property :name, :class => Name # property :name, :default => "Mike" - # property :name, :include_nil => true + # property :name, :render_nil => true def property(name, options={}) representable_attrs << definition_class.new(name, options) end # Declares a represented document node collection. @@ -170,19 +183,13 @@ return super() unless name # allow Object.hash. options[:hash] = true property(name, options) end - end - - - module Accessors - def representable_attrs - @representable_attrs ||= Config.new - end - def representation_wrap=(name) - representable_attrs.wrap = name + private + def definition_class + Definition end end end