class Tainbox::AttributeDefiner

Attributes

attribute_name[R]
klass[R]
requested_args[R]
requested_type[R]

Public Class Methods

new(klass, attribute_name, requested_type, requested_args) click to toggle source
# File lib/tainbox/attribute_definer.rb, line 9
def initialize(klass, attribute_name, requested_type, requested_args)
  @klass = klass
  @attribute_name = attribute_name.to_sym
  @requested_type = requested_type
  @requested_args = requested_args
end

Public Instance Methods

define_getter() click to toggle source
# File lib/tainbox/attribute_definer.rb, line 16
def define_getter
  klass.tainbox_register_attribute(attribute_name)
  attribute = attribute_name

  klass.tainbox_layer.instance_eval do
    define_method(attribute) do
      value = instance_variable_get(:"@tainbox_#{attribute}")
      value.is_a?(Tainbox::DeferredValue) ? instance_exec(&value.proc) : value
    end
  end
end
define_setter() click to toggle source
# File lib/tainbox/attribute_definer.rb, line 28
def define_setter
  klass.tainbox_register_attribute(attribute_name)

  attribute = attribute_name
  args = requested_args
  type = requested_type

  klass.tainbox_layer.instance_eval do

    define_method("#{attribute}=") do |value|
      tainbox_register_attribute_provided(attribute)
      value = Tainbox::TypeConverter.new(type, value, options: args).convert if type
      instance_variable_set(:"@tainbox_#{attribute}", value)
    end

    define_method("tainbox_set_default_#{attribute}") do
      if args.has_key?(:default)
        tainbox_register_attribute_provided(attribute)
        value = args[:default].deep_dup
        value = Tainbox::DeferredValue.new(value) if value.is_a?(Proc)
        instance_variable_set(:"@tainbox_#{attribute}", value)

      else
        tainbox_unregister_attribute_provided(attribute)
        instance_variable_set(:"@tainbox_#{attribute}", nil)
      end
    end
  end
end