Module: Pupa::Model

Extended by:
ActiveSupport::Concern
Included in:
Area, Membership, Motion, Organization, Person, Post, Vote, VoteEvent
Defined in:
lib/pupa/models/model.rb

Overview

Adds methods expected by Pupa processors.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean

Returns whether two objects are identical, ignoring any differences in the objects' machine IDs.

Parameters:

  • other (Object)

    another object

Returns:

  • (Boolean)

    whether the objects are identical



196
197
198
199
200
201
202
# 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) ⇒ Object

Returns the value of a property.

Parameters:

  • property (Symbol)

    a property name



104
105
106
107
108
109
110
# 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) ⇒ Object

Sets the value of a property.

Parameters:

  • property (Symbol)

    a property name

  • value

    a value



117
118
119
120
121
122
123
# 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) ⇒ Object

Sets the object's ID.

Parameters:

  • id (String, BSON::ObjectId)

    an ID



128
129
130
# 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) ⇒ Object

Adds a key-value pair to the object.

Parameters:

  • key (Symbol)

    a key

  • value

    a value



143
144
145
# File 'lib/pupa/models/model.rb', line 143

def add_extra(key, value)
  @extras[key] = value
end

#extras=(extras) ⇒ Object

Sets the extras.

Parameters:

  • extras (Array)

    a list of extras



135
136
137
# File 'lib/pupa/models/model.rb', line 135

def extras=(extras)
  @extras = symbolize_keys(extras)
end

#fingerprintHash

Returns a subset of the object's properties that should uniquely identify the object.

Returns:

  • (Hash)

    a subset of the object's properties



151
152
153
# File 'lib/pupa/models/model.rb', line 151

def fingerprint
  to_h(persist: true).except(:_id)
end

#foreign_propertiesHash

Returns the object's foreign keys and foreign objects.

Returns:

  • (Hash)

    the object's foreign keys and foreign objects



158
159
160
# File 'lib/pupa/models/model.rb', line 158

def foreign_properties
  to_h.slice(*foreign_keys + foreign_objects)
end

#initialize(properties = {}) ⇒ Object

Parameters:

  • properties (Hash) (defaults to: {})

    the object's properties



90
91
92
93
94
95
96
97
98
# 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

#to_h(persist: false) ⇒ Hash

Returns the object as a hash.

Parameters:

  • persist (Boolean) (defaults to: false)

    whether the object is being persisted, validated, or used as a database selector, in which case foreign objects (hints) are excluded

Returns:

  • (Hash)

    the object as a hash



180
181
182
183
184
185
186
187
188
189
# 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!Object

Validates the object against the schema.



165
166
167
168
169
170
171
172
# 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