lib/representable/binding.rb in representable-1.3.2 vs lib/representable/binding.rb in representable-1.3.3

- old
+ new

@@ -3,20 +3,28 @@ module Representable # The Binding wraps the Definition instance for this property and provides methods to read/write fragments. class Binding < SimpleDelegator class FragmentNotFound end + + def self.build(definition, *args) + # DISCUSS: move #create_binding to this class? + return definition.create_binding(*args) if definition.binding + build_for(definition, *args) + end def definition # TODO: remove in 1.4. raise "Binding#definition is no longer supported as all Definition methods are now delegated automatically." end def initialize(definition, represented, user_options={}) # TODO: remove default arg. super(definition) @represented = represented @user_options = user_options end + + attr_reader :user_options, :represented # TODO: make private/remove. # Main entry point for rendering/parsing a property object. def serialize(value) value end @@ -48,9 +56,19 @@ yield value end def read_fragment_for(doc) read(doc) + end + + def get + return represented.instance_exec(user_options, &options[:getter]) if options[:getter] + represented.send(getter) + end + + def set(value) + value = represented.instance_exec(value, user_options, &options[:setter]) if options[:setter] + represented.send(setter, value) end # Hooks into #serialize and #deserialize to extend typed properties # at runtime.