lib/representable.rb in representable-1.1.7 vs lib/representable.rb in representable-1.2.0

- old
+ new

@@ -1,6 +1,7 @@ require 'representable/definition' +require 'representable/deprecations' # Representable can be used in two ways. # # == On class level # @@ -27,10 +28,11 @@ module Representable attr_writer :representable_attrs def self.included(base) base.class_eval do + include Deprecations extend ClassMethods extend ClassMethods::Declarations extend ClassMethods::Accessors def self.included(base) @@ -65,18 +67,17 @@ doc end # Checks and returns if the property should be included. def skip_property?(binding, options) - return true if skip_excluded_property?(binding, options) # no need for further evaluation when :except'ed + return true if skip_excluded_property?(binding, options) # no need for further evaluation when :exclude'ed skip_conditional_property?(binding) end def skip_excluded_property?(binding, options) - return unless props = options[:except] || options[:include] - props = options[:except] || options[:include] + return unless props = options[:exclude] || options[:include] res = props.include?(binding.definition.name.to_sym) options[:include] ? !res : res end def skip_conditional_property?(binding) @@ -84,22 +85,26 @@ not instance_exec(&condition) end # Retrieve value and write fragment to the doc. def compile_fragment(bin, doc) - value = send(bin.definition.getter) || bin.definition.default # DISCUSS: eventually move back to Ref. + value = send(bin.definition.getter) + value = bin.definition.default_for(value) + write_fragment_for(bin, value, doc) end # Parse value from doc and update the model property. def uncompile_fragment(bin, doc) - value = read_fragment_for(bin, doc) || bin.definition.default + value = read_fragment_for(bin, doc) + value = bin.definition.default_for(value) + send(bin.definition.setter, value) end def write_fragment_for(bin, value, doc) # DISCUSS: move to Binding? - return unless value + return if bin.definition.skipable_nil_value?(value) bin.write(doc, value) end def read_fragment_for(bin, doc) # DISCUSS: move to Binding? bin.read(doc) @@ -138,9 +143,10 @@ # # property :name # property :name, :from => :title # property :name, :class => Name # property :name, :default => "Mike" + # property :name, :include_nil => true def property(name, options={}) representable_attrs << definition_class.new(name, options) end # Declares a represented document node collection.