Sha256: 9306146a48d9b4c8531023be56c8422165013bdfaaa93774d2ebf6be27fbda7b

Contents?: true

Size: 585 Bytes

Versions: 1

Compression:

Stored size: 585 Bytes

Contents

class DefineField

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

    klass.class_eval do
      define_method(actual_field) do
        instance_variable_get(var_name) || send(field_setter,nil)
      end
      define_method(field_name) do
        send(actual_field).raw
      end
      define_method(field_setter) do |value|
        instance_variable_set var_name, field.new(*([value].compact))
      end
    end
  end

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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pr-0.0.1 lib/pr/define_field.rb