Sha256: 1ec0a35cf34335a66c03ce9a525d3bb42ac3943882301d3a4aeb04f92a09a727

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Clamp
  module Attribute

    module Declaration

      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)
          else
            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
          if attribute.multivalued?
            unless instance_variable_defined?(attribute.ivar_name)
              instance_variable_set(attribute.ivar_name, [])
            end
            instance_variable_get(attribute.ivar_name) << value
          else
            instance_variable_set(attribute.ivar_name, value)
          end
        end
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clamp-0.6.0 lib/clamp/attribute/declaration.rb