Sha256: 4a9d155e017fa6b48a487273395e35542df132e93d3e15d9373b6ee76fbfa229
Contents?: true
Size: 1.22 KB
Versions: 1
Compression:
Stored size: 1.22 KB
Contents
module Id module Model class Field def initialize(model, name, options) @model = model @name = name @options = options end def define Definer.method_memoize(model, name) { |data| value_of(data) } Definer.method(model, "#{name}?") { |obj| presence_of(obj.data) } hook_define end def hook_define end def value_of(data) value = data.fetch(key, &default_value) optional ? Option[value].map{ |d| cast d } : cast(value) end def presence_of(data) data.has_key?(key) && data.fetch(key) end def cast(value) TypeCasts.cast(options.fetch(:type, false), value) end def key options.fetch(:key, name).to_s end def default_value proc do if default? then default elsif !optional? then raise MissingAttributeError, key end end end def default? options.has_key?(:default) end def default options.fetch(:default) end def optional? options.fetch(:optional, false) end alias_method :optional, :optional? attr_reader :model, :name, :options end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
id-0.0.12 | lib/id/model/field.rb |