Sha256: 11075da9a4111ee0a7c521ca9fea47421489c0ae14c36eee8042d871e3fae5fe

Contents?: true

Size: 951 Bytes

Versions: 1

Compression:

Stored size: 951 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

1 entries across 1 versions & 1 rubygems

Version Path
clamp-0.3.0 lib/clamp/attribute_declaration.rb