Sha256: 24a13df78a149f885bf2f5157332d5dcb3641eed8dd6db2867a4f852d0fd90d9

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

class DefineField
  class << self
    def on klass, field_name, field, options = {}
      new.on klass, field_name, field, options
    end
    def [] klass, field_name, field, options = {}
      new.on klass, field_name, field, options
    end
  end

  def on klass, field_name, field, options = {}
    var_name     = "@field_#{field_name}"
    field_setter = "#{field_name}="
    actual_field = "__#{field_name}"

    define_field  klass, actual_field, var_name, field_setter
    define_reader klass, field_name, actual_field
    define_setter klass, var_name, field_setter, field, options
  end

  private
    def var name
      "@field_#{name}"
    end

    def define_field klass, actual_field, var_name, field_setter
      klass.class_eval do
        define_method(actual_field) do
          instance_variable_get(var_name) || send(field_setter,nil)
        end
      end
    end

    def define_reader klass, field_name, actual_field
      klass.class_eval do
        define_method(field_name) do
          send(actual_field).raw
        end
      end
    end

    def define_setter klass, var_name, field_setter, field, options
      klass.class_eval do
        define_method(field_setter) do |value|
          instance_variable_set var_name, field.new(value,options)
        end
      end
    end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pr-1.0.0 lib/pr/define_field.rb
pr-0.0.7 lib/pr/define_field.rb
pr-0.0.6 lib/pr/define_field.rb