lib/moosex.rb in moosex-0.0.4 vs lib/moosex.rb in moosex-0.0.5

- old
+ new

@@ -68,20 +68,22 @@ DEFAULTS= { :clearer => false, :required => false, :predicate => false, :isa => lambda { |x| true }, + :handles => {}, } REQUIRED = [ :is ] VALIDATE = { :is => lambda do |is, field_name| unless [:rw, :rwp, :ro, :lazy].include?(is) raise "invalid value for field '#{field_name}' is '#{is}', must be one of :rw, :rwp, :ro or :lazy" end end, + :handles => lambda {|handles, field_name| true }, # TODO: add implementation }; COERCE = { :is => lambda do |is, field_name| is.to_sym @@ -128,10 +130,16 @@ clearer.to_sym rescue => e # create a nested exception here raise "cannot coerce field clearer to a symbol for #{field_name}: #{e}" end + end, + :handles => lambda do |handles, field_name| + # TODO: + # if single method + # if array of methods + handles end, }; def initialize(a, o) # todo extract this to a framework, see issue #21 on facebook @@ -158,16 +166,24 @@ @isa = o[:isa] @default = o[:default] @required = o[:required] @predicate = o[:predicate] @clearer = o[:clearer] + @handles = o[:handles] end def init(object, args) inst_variable_name = "@#{@attr_symbol}".to_sym setter = @attr_symbol.to_s.concat("=").to_sym value = nil + + attr_symbol = @attr_symbol + @handles.each_pair do | method, target_method | + object.define_singleton_method method do |*args| + self.send(attr_symbol).send(target_method, *args) + end + end if @predicate object.define_singleton_method @predicate do instance_variable_defined? inst_variable_name end \ No newline at end of file