lib/representable.rb in representable-1.6.0 vs lib/representable.rb in representable-1.6.1
- old
+ new
@@ -9,10 +9,11 @@
def self.included(base)
base.class_eval do
extend ClassInclusions, ModuleExtensions
extend ClassMethods
extend ClassMethods::Declarations
+ extend DSLAdditions
include Deprecations
end
end
@@ -75,11 +76,11 @@
module ModuleExtensions
# Copies the representable_attrs to the extended object.
def extended(object)
super
- object.representable_attrs=(representable_attrs)
+ object.representable_attrs=(representable_attrs) # yes, we want a hard overwrite here and no inheritance.
end
end
module ClassMethods
@@ -114,27 +115,23 @@
# property :name, :default => "Mike"
# property :name, :render_nil => true
# property :name, :readable => false
# property :name, :writeable => false
def property(name, options={}, &block)
- if block_given? # DISCUSS: separate module?
- options[:extend] = inline_representer(representer_engine, &block)
- end
-
(representable_attrs << definition_class.new(name, options)).last
end
# Declares a represented document node collection.
#
# Examples:
#
# collection :products
# collection :products, :from => :item
# collection :products, :class => Product
- def collection(name, options={})
+ def collection(name, options={}, &block)
options[:collection] = true
- property(name, options)
+ property(name, options, &block)
end
def hash(name=nil, options={})
return super() unless name # allow Object.hash.
@@ -148,17 +145,31 @@
end
def build_config
Config.new
end
+ end # Declarations
+ end
- def inline_representer(base_module, &block) # DISCUSS: separate module?
- Module.new do
- include base_module
- instance_exec &block
- end
+ # Internal module for DSL sugar that should not go into the core library.
+ module DSLAdditions
+ def property(name, options={}, &block)
+ return super unless block_given?
+
+ inline = inline_representer(representer_engine, &block)
+ inline.module_eval { include options[:extend] } if options[:extend]
+
+ options[:extend] = inline
+ super
+ end
+
+ private
+ def inline_representer(base_module, &block) # DISCUSS: separate module?
+ Module.new do
+ include base_module
+ instance_exec &block
end
end
- end
+ end # DSLAdditions
end
require 'representable/decorator'