Sha256: 2ff9c0026372ee0d888e3d47caf1d5406056d11a2c06989dd9a78892a8fd05d7

Contents?: true

Size: 963 Bytes

Versions: 7

Compression:

Stored size: 963 Bytes

Contents

module Clamp
  
  module AttributeDeclaration

    protected
    
    def define_accessors_for(attribute, &block)
      define_reader_for(attribute)
      define_default_for(attribute)
      define_writer_for(attribute, &block)
    end
    
    def define_reader_for(attribute)
      define_method(attribute.read_method) do
        if instance_variable_defined?(attribute.ivar_name)
          instance_variable_get(attribute.ivar_name)
        elsif respond_to?(attribute.default_method)
          send(attribute.default_method)
        end
      end
    end

    def define_default_for(attribute)
      define_method(attribute.default_method) do
        attribute.default_value
      end
    end

    def define_writer_for(attribute, &block)
      define_method(attribute.write_method) do |value|
        if block
          value = instance_exec(value, &block)
        end
        instance_variable_set(attribute.ivar_name, value)
      end
    end

  end
  
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
clamp-0.2.3 lib/clamp/attribute_declaration.rb
clamp-0.2.2 lib/clamp/attribute_declaration.rb
clamp-0.2.1 lib/clamp/attribute_declaration.rb
clamp-0.2.0 lib/clamp/attribute_declaration.rb
wpb-0.0.5 lib/wpb/clamp/attribute_declaration.rb
clamp-0.1.8 lib/clamp/attribute_declaration.rb
clamp-0.1.7 lib/clamp/attribute_declaration.rb