module Pupa::Model
Adds methods expected by Pupa
processors.
Attributes
@return [String] The object’s unique identifier.
@return [String] The underscored, lowercase form of the object’s class.
@return [BSON::Document,nil] The object’s matching document in
the database. Set before persisting the object to the database.
@return [Hash] The object’s non-schema properties.
Public Class Methods
@param [Hash] properties the object’s properties
# File lib/pupa/models/model.rb, line 90 def initialize(properties = {}) @_type = self.class.to_s.underscore @_id = SecureRandom.uuid @extras = {} properties.each do |key,value| self[key] = value end end
Public Instance Methods
Returns whether two objects are identical, ignoring any differences in the objects’ machine IDs.
@param [Object] other another object @return [Boolean] whether the objects are identical
# File lib/pupa/models/model.rb, line 196 def ==(other) a = to_h b = other.to_h a.delete(:_id) b.delete(:_id) a == b end
Returns the value of a property.
@param [Symbol] property a property name @raises [Pupa::Errors::MissingAttributeError] if class is missing the property
# File lib/pupa/models/model.rb, line 104 def [](property) if properties.include?(property.to_sym) send(property) else raise Errors::MissingAttributeError, "missing attribute: #{property}" end end
Sets the value of a property.
@param [Symbol] property a property name @param value a value @raises [Pupa::Errors::MissingAttributeError] if class is missing the property
# File lib/pupa/models/model.rb, line 117 def []=(property, value) if properties.include?(property.to_sym) send("#{property}=", value) else raise Errors::MissingAttributeError, "missing attribute: #{property}" end end
Sets the object’s ID.
@param [String,BSON::ObjectId] id an ID
# File lib/pupa/models/model.rb, line 128 def _id=(id) @_id = id.to_s # in case of BSON::ObjectId end
Adds a key-value pair to the object.
@param [Symbol] key a key @param value a value
# File lib/pupa/models/model.rb, line 143 def add_extra(key, value) @extras[key] = value end
Sets the extras.
@param [Array] extras a list of extras
# File lib/pupa/models/model.rb, line 135 def extras=(extras) @extras = symbolize_keys(extras) end
Returns a subset of the object’s properties that should uniquely identify the object.
@return [Hash] a subset of the object’s properties
# File lib/pupa/models/model.rb, line 151 def fingerprint to_h(persist: true).except(:_id) end
Returns the object’s foreign keys and foreign objects.
@return [Hash] the object’s foreign keys and foreign objects
# File lib/pupa/models/model.rb, line 158 def foreign_properties to_h.slice(*foreign_keys + foreign_objects) end
Returns the object as a hash.
@param [Boolean] persist whether the object is being persisted, validated,
or used as a database selector, in which case foreign objects (hints) are excluded
@return [Hash] the object as a hash
# File lib/pupa/models/model.rb, line 180 def to_h(persist: false) {}.tap do |hash| (persist ? properties - foreign_objects : properties).each do |property| value = self[property] if value == false || value.present? hash[property] = value end end end end
Validates the object against the schema.
@raises [JSON::Schema::ValidationError] if the object is invalid
# File lib/pupa/models/model.rb, line 165 def validate! if self.class.json_schema self.class.validator.instance_variable_set('@errors', []) self.class.validator.instance_variable_set('@data', stringify_keys(to_h(persist: true))) self.class.validator.validate true end end