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
-
#==(other) ⇒ Boolean
Returns whether two objects are identical, ignoring any differences in the objects' machine IDs.
-
#[](property) ⇒ Object
Returns the value of a property.
-
#[]=(property, value) ⇒ Object
Sets the value of a property.
-
#_id=(id) ⇒ Object
Sets the object's ID.
-
#add_extra(key, value) ⇒ Object
Adds a key-value pair to the object.
-
#extras=(extras) ⇒ Object
Sets the extras.
-
#fingerprint ⇒ Hash
Returns a subset of the object's properties that should uniquely identify the object.
-
#foreign_properties ⇒ Hash
Returns the object's foreign keys and foreign objects.
- #initialize(properties = {}) ⇒ Object
-
#to_h(persist: false) ⇒ Hash
Returns the object as a hash.
-
#validate! ⇒ Object
Validates the object against the schema.
Instance Method Details
#==(other) ⇒ Boolean
Returns whether two objects are identical, ignoring any differences in the objects' machine IDs.
198 199 200 201 202 203 204 |
# File 'lib/pupa/models/model.rb', line 198 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.
109 110 111 112 113 114 115 |
# File 'lib/pupa/models/model.rb', line 109 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.
122 123 124 125 126 127 128 |
# File 'lib/pupa/models/model.rb', line 122 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.
133 134 135 |
# File 'lib/pupa/models/model.rb', line 133 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.
148 149 150 |
# File 'lib/pupa/models/model.rb', line 148 def add_extra(key, value) @extras[key] = value end |
#extras=(extras) ⇒ Object
Sets the extras.
140 141 142 |
# File 'lib/pupa/models/model.rb', line 140 def extras=(extras) @extras = symbolize_keys(extras) end |
#fingerprint ⇒ Hash
Returns a subset of the object's properties that should uniquely identify the object.
156 157 158 |
# File 'lib/pupa/models/model.rb', line 156 def fingerprint to_h(persist: true).except(:_id) end |
#foreign_properties ⇒ Hash
Returns the object's foreign keys and foreign objects.
163 164 165 |
# File 'lib/pupa/models/model.rb', line 163 def foreign_properties to_h.slice(*foreign_keys + foreign_objects) end |
#initialize(properties = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/pupa/models/model.rb', line 95 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.
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/pupa/models/model.rb', line 182 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.
170 171 172 173 174 |
# File 'lib/pupa/models/model.rb', line 170 def validate! if self.class.json_schema JSON::Validator.validate!(self.class.json_schema, stringify_keys(to_h(persist: true))) end end |