Sha256: c81e89cbe7774fc986ce103bcfd72abedf820101e0302e15c82fc7e8a499e4ee

Contents?: true

Size: 1.03 KB

Versions: 7

Compression:

Stored size: 1.03 KB

Contents

module Clamp
  
  module AttributeDeclaration

    protected
    
    def define_accessors_for(attribute, &block)
      define_reader_for(attribute)
      define_writer_for(attribute, &block)
    end
    
    def define_reader_for(attribute)
      reader_name = attribute.attribute_name
      reader_name += "?" if attribute.respond_to?(:flag?) && attribute.flag?
      ivar_name = "@#{attribute.attribute_name}"
      define_method(reader_name) do
        if instance_variable_defined?(ivar_name)
          instance_variable_get(ivar_name)
        elsif parent_command && parent_command.respond_to?(reader_name)
          parent_command.send(reader_name)
        elsif attribute.respond_to?(:default_value)
          attribute.default_value
        end
      end
    end

    def define_writer_for(attribute, &block)
      define_method("#{attribute.attribute_name}=") do |value|
        if block
          value = instance_exec(value, &block)
        end
        instance_variable_set("@#{attribute.attribute_name}", value)
      end
    end

  end
  
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
clamp-0.1.5 lib/clamp/attribute_declaration.rb
clamp-0.1.4 lib/clamp/attribute_declaration.rb
clamp-0.1.3 lib/clamp/attribute_declaration.rb
clamp-0.1.2 lib/clamp/attribute_declaration.rb
clamp-0.1.1 lib/clamp/attribute_declaration.rb
clamp-0.1.0 lib/clamp/attribute_declaration.rb
clamp-0.0.9 lib/clamp/attribute_declaration.rb