lib/representable.rb in representable-0.10.3 vs lib/representable.rb in representable-0.11.0

- old
+ new

@@ -7,10 +7,18 @@ extend ClassMethods::Accessors def self.included(base) base.representable_attrs.push(*representable_attrs) # "inherit". end + + # Copies the representable_attrs to the extended object. + def self.extended(object) + attrs = representable_attrs + object.instance_eval do + @representable_attrs = attrs + end + end end end # Reads values from +doc+ and sets properties accordingly. def update_properties_from(doc, &block) @@ -48,22 +56,22 @@ def representable_bindings representable_attrs.map {|attr| binding_for_definition(attr) } end + # Returns the wrapper for the representation. Mostly used in XML. + def representation_wrap + representable_attrs.wrap_for(self.class.name) + end + module ClassMethods # :nodoc: module Declarations def definition_class Definition end - # Returns bindings for all properties. - def representable_bindings - representable_attrs.map {|attr| binding_for_definition(attr) } - end - # Declares a represented document node, which is usually a XML tag or a JSON key. # # Examples: # # representable_property :name @@ -98,37 +106,33 @@ end end module Accessors def representable_attrs - @representable_attrs ||= [] + @representable_attrs ||= Config.new end - def representable_wrap - @representable_wrap ||= false - end - - def representable_wrap=(name) - @representable_wrap = name - end - def representation_wrap=(name) - self.representable_wrap = name + representable_attrs.wrap = name end - - # Returns the wrapper for the representation. Mostly used in XML. - def representation_wrap - return unless representable_wrap - return infer_representation_name if representable_wrap === true - representable_wrap - end - - private - def infer_representation_name - name.split('::').last. - gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). - gsub(/([a-z\d])([A-Z])/,'\1_\2'). - downcase - end + end + end + + class Config < Array + attr_accessor :wrap + + # Computes the wrap string or returns false. + def wrap_for(name) + return unless wrap + return infer_name_for(name) if wrap === true + wrap + end + + private + def infer_name_for(name) + name.to_s.split('::').last. + gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). + gsub(/([a-z\d])([A-Z])/,'\1_\2'). + downcase end end end