module Pupa::Model

Adds methods expected by Pupa processors.

Attributes

_id[R]

@return [String] The object’s unique identifier.

_type[RW]

@return [String] The underscored, lowercase form of the object’s class.

document[RW]

@return [BSON::Document,nil] The object’s matching document in

the database. Set before persisting the object to the database.
extras[R]

@return [Hash] The object’s non-schema properties.

Public Class Methods

new(properties = {}) click to toggle source

@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

==(other) click to toggle source

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
[](property) click to toggle source

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
[]=(property, value) click to toggle source

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
_id=(id) click to toggle source

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
add_extra(key, value) click to toggle source

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
extras=(extras) click to toggle source

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
fingerprint() click to toggle source

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
foreign_properties() click to toggle source

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
to_h(persist: false) click to toggle source

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
validate!() click to toggle source

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