Sha256: c96249d62b86e6acfa951771901ac92a3c6d69cd7bde44e1c6c5d9989107cd06

Contents?: true

Size: 838 Bytes

Versions: 2

Compression:

Stored size: 838 Bytes

Contents

module Interfaces
  module TypedAccessors
    # use like:
    # typed_attr_accessor :field_name => InterfaceName
    def typed_attr_accessor(attrs)
      # attrs.each_pair |attr_name,interface|
      #   inst_variable_name = "@#{attr_name}"
      #   define_method method_name do
      #     instance_variable_get inst_variable_name
      #   end
      # end

      # use the standard reader
      attrs.keys.each do |attr|
        attr_reader attr
      end

      # also define writers
      typed_attr_writer attrs
    end

    def typed_attr_writer(attrs)
      attrs.each_pair do |attr_name,interface|
        inst_variable_name = "@#{attr_name}"
        define_method "#{attr_name}=" do |new_value|
          instance_variable_set inst_variable_name, new_value ? new_value.as(interface) : nil
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
interfaces-0.2.0 lib/interfaces/typed_accessors.rb
interfaces-0.0.2.pre lib/interfaces/typed_accessors.rb