Sha256: 27fa7ffda7828f5f75517179879d0ea8a45b7370a68e5a7913799d7bd4c345d0

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module PureForm
  class Attribute
    attr_reader :context, :name, :options, :value_type

    def initialize(context, name, options)
      @context, @name, @options = context, name.to_s, options
      @value_type = build_value_type
    end

    def define
      define_setter
      define_complex_setter
      define_getter
      define_predicate
    end

    private

    def define_setter
      define_for_context "#{name}=", &setter_proc
    end

    def define_complex_setter
      return unless value_type.respond_to?(:complex_typecast)
      method_name = "set_complex_#{name}_value"
      define_for_context method_name, &complex_setter_proc
    end

    def define_getter
      define_for_context name, &getter_proc
    end

    def define_predicate
      define_for_context "#{name}?", &predicate_proc
    end

    def define_for_context(name, &block)
      context.instance_eval do
        define_method name, &block
      end

      nil
    end

    def setter_proc
      attribute_name = name
      type = value_type
      ->(value){ store_attribute(attribute_name, type.typecast(value)) }
    end

    def complex_setter_proc
      setter_name = "#{name}="
      type = value_type
      ->(*values){ public_send setter_name, type.complex_typecast(*values) }
    end

    def getter_proc
      attribute_name = name
      ->{ read_attribute(attribute_name) }
    end

    def predicate_proc
      attribute_name = name
      ->{ public_send(attribute_name).present? }
    end

    def build_value_type
      return Types::BaseType.new(options) unless options.key?(:type)
      type = options.delete(:type).to_s
      type = "date_time" if type == "datetime"
      "PureForm::Types::#{type.camelize}Type".constantize.new(options)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pure_form-0.0.2 lib/pure_form/attribute.rb