lib/representable.rb in representable-1.7.7 vs lib/representable.rb in representable-1.8.0

- old
+ new

@@ -1,6 +1,5 @@ -require 'representable/deprecations' require 'representable/definition' require 'representable/mapper' require 'representable/config' module Representable @@ -10,12 +9,10 @@ base.class_eval do extend ClassInclusions, ModuleExtensions extend ClassMethods extend ClassMethods::Declarations extend DSLAdditions - - include Deprecations end end private # Reads values from +doc+ and sets properties accordingly. @@ -33,13 +30,11 @@ options = cleanup_options(options) # FIXME: make representable-options and user-options two different hashes. representable_attrs.collect {|attr| representable_binding_for(attr, format, options) } end def representable_binding_for(attribute, format, options) - context = attribute.options[:decorator_scope] ? self : represented # DISCUSS: pass both represented and representer into Binding and do it there? - - format.build(attribute, represented, options, context) + format.build(attribute, represented, self, options) end def cleanup_options(options) # TODO: remove me. this clearly belongs in Representable. options.reject { |k,v| [:include, :exclude].include?(k) } end @@ -52,12 +47,12 @@ bindings = representable_bindings_for(format, options) Mapper.new(bindings, represented, options) # TODO: remove self, or do we need it? and also represented! end - def representation_wrap - representable_attrs.wrap_for(self.class.name) # FIXME: where is this needed? + def representation_wrap(*args) + representable_attrs.wrap_for(self.class.name, represented, *args) end def represented self end @@ -103,32 +98,14 @@ 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, :render_nil => true - # property :name, :readable => false - # property :name, :writeable => false def property(name, options={}, &block) representable_attrs << definition_class.new(name, options) end - # Declares a represented document node collection. - # - # Examples: - # - # collection :products - # collection :products, :from => :item - # collection :products, :class => Product def collection(name, options={}, &block) options[:collection] = true # FIXME: don't override original. property(name, options, &block) end @@ -153,29 +130,34 @@ # Internal module for DSL sugar that should not go into the core library. module DSLAdditions # Allows you to nest a block of properties in a separate section while still mapping them to the outer object. def nested(name, options={}, &block) options = options.merge( - :decorator => true, - :getter => lambda { |*| self }, - :setter => lambda { |*| }, - :instance => lambda { |*| self } - ) + :use_decorator => true, + :getter => lambda { |*| self }, + :setter => lambda { |*| }, + :instance => lambda { |*| self } + ) # DISCUSS: should this be a macro just as :parse_strategy? property(name, options, &block) end def property(name, options={}, &block) - parent = representable_attrs[name] + modules = [] + if options[:inherit] # TODO: move this to Definition. + parent = representable_attrs[name] + modules << parent[:extend].evaluate(nil) if parent[:extend]# we can savely assume this is _not_ a lambda. # DISCUSS: leave that in #representer_module? + end # FIXME: can we handle this in super/Definition.new ? + if block_given? - options[:extend] = inline_representer_for(parent, name, options, &block) # FIXME: passing parent sucks since we don't use it all the time. + handle_deprecated_inline_extend!(modules, options) + + options[:extend] = inline_representer_for(modules, name, options, &block) end - if options[:inherit] - parent.options.merge!(options) and return parent - end # FIXME: can we handle this in super/Definition.new ? + return parent.merge!(options) if options.delete(:inherit) super end def inline_representer(base_module, name, options, &block) # DISCUSS: separate module? @@ -184,18 +166,32 @@ instance_exec &block end end private - def inline_representer_for(parent, name, options, &block) - representer = options[:decorator] ? Decorator : self + def inline_representer_for(modules, name, options, &block) + representer = options[:use_decorator] ? Decorator : self + modules = [representer_engine] + modules - modules = [representer_engine] - modules << parent.representer_module if options[:inherit] - modules << options[:extend] - representer.inline_representer(modules.compact.reverse, name, options, &block) end + + def handle_deprecated_inline_extend!(modules, options) # TODO: remove in 2.0. + return unless include_module = options.delete(:extend) and not options[:inherit] + + warn "[Representable] Using :extend with an inline representer is deprecated. Include the module in the inline block." + modules << include_module + end end # DSLAdditions end -require 'representable/decorator' + +module Representable + autoload :Hash, 'representable/hash' + + module Hash + autoload :AllowSymbols, 'representable/hash/allow_symbols' + autoload :Collection, 'representable/hash/collection' + end + + autoload :Decorator, 'representable/decorator' +end