Module: TypedModel::ModelBase
- Defined in:
- lib/typed_model/model_base.rb
Overview
Introduce a ModelBase class for declaring structure
- data mapping (data -> object -> data)
- declared types
- declared validations
- Nested models
e.g. class Address include ModelBase attribute :street, type: string, validations: [:required] end
class Employee include ModelBase attribute :name, type: :string, validations: [:required] attribute :primary_address, type: Address, validations: [:required] attribute :alternate_addresses, seq_of: Address end
Class Method Summary collapse
Instance Method Summary collapse
- #attributes=(values = {}) ⇒ Object
- #initialize(values = {}) ⇒ Object
- #to_data ⇒ Object
- #validate_declared_attributes ⇒ Object
Class Method Details
.included(base) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/typed_model/model_base.rb', line 25 def self.included(base) super base.class_eval do include ModelValidations before_validation :validate_declared_attributes class << self def attribute(name, opts = {}) @declared_attributes ||= {} attribute_def = AttributeDefinition.new(**opts, name: name) @declared_attributes[name.to_sym] = attribute_def attr_reader name define_method :"#{name}=" do |v| instance_variable_set(:"@#{name}", attribute_def.typecast_value(v)) end end def declared_attributes ancestors.reverse.each_with_object({}) do |c, attributes| attributes.merge!(c.instance_variable_get(:@declared_attributes) || {}) end end def declared_attributes_with_alternates declared_attributes.each_with_object({}) do |(name, attr_def), h| h[name] = attr_def h[attr_def.mapping_key.to_sym] = attr_def end end end end end |
Instance Method Details
#attributes=(values = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/typed_model/model_base.rb', line 63 def attributes=(values = {}) return if values.nil? attrs_with_alternates = self.class.declared_attributes_with_alternates values.each_pair do |k, v| if (attr_def = attrs_with_alternates[k.to_sym]) setter = :"#{attr_def.name}=" send(setter, v) if respond_to?(setter) end end end |
#initialize(values = {}) ⇒ Object
59 60 61 |
# File 'lib/typed_model/model_base.rb', line 59 def initialize(values = {}) self.attributes = values end |
#to_data ⇒ Object
81 82 83 84 85 86 |
# File 'lib/typed_model/model_base.rb', line 81 def to_data self.class.declared_attributes.each_with_object({}) do |(_, attr), h| data = attr.to_data(self) h[attr.mapping_key] = data unless data.nil? end end |
#validate_declared_attributes ⇒ Object
75 76 77 78 79 |
# File 'lib/typed_model/model_base.rb', line 75 def validate_declared_attributes self.class.declared_attributes.each do |(_, attr)| attr.validate(self) end end |