Sha256: 8bb1a43dd3ce91e7d09e8c6ecd215293ec1d9c396a56c13801ed1306ee86728e
Contents?: true
Size: 1.65 KB
Versions: 2
Compression:
Stored size: 1.65 KB
Contents
module Id module Model class Field def initialize(model, name, options) @model = model @name = name @options = options end def define_getter field = self model.send :define_method, name do memoize field.name do field.cast data.fetch(field.key, &field.default_value) end end end def define_setter model.send(:builder_class).define_setter name end def define_is_present field = self model.send :define_method, "#{name}?" do data.has_key?(field.key) && !data.fetch(field.key).nil? end end def define define_getter define_setter define_is_present 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 attr_reader :model, :name, :options end class FieldOption < Field def define_getter field = self model.send :define_method, name do memoize field.name do if d = data.fetch(field.key, &field.default_value) Some[field.cast d] else None end end end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
id-0.0.8 | lib/id/model/field.rb |
id-0.0.7 | lib/id/model/field.rb |