Class: Parse::Model

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serializers::JSON, Client::Connectable
Defined in:
lib/parse/model/model.rb,
lib/parse/model/core/builder.rb

Overview

The core model of all Parse-Stack classes. This class works by providing a baseline for all subclass objects to support ActiveModel features such as serialization, dirty tracking, callbacks, etc.

See Also:

  • ActiveModel

Direct Known Subclasses

Bytes, File, GeoJSON::Geometry, GeoPoint, Pointer, Polygon

Defined Under Namespace

Classes: Builder

Constant Summary collapse

TYPE_FIELD =

The name of the field in a hash that contains information about the type of data the hash represents.

"__type".freeze
OBJECT_ID =

The objectId field in Parse Objects.

"objectId".freeze
ID =

See Also:

"id".freeze
KEY_CLASS_NAME =

The key field for getting class information.

"className".freeze
KEY_OBJECT_ID =
Deprecated.

Use OBJECT_ID instead.

"objectId".freeze
KEY_CREATED_AT =

The key field for getting the created at date of an object hash.

"createdAt"
KEY_UPDATED_AT =

The key field for getting the updated at date of an object hash.

"updatedAt"
CLASS_USER =

The collection for Users in Parse. Used by Parse::User.

"_User"
CLASS_INSTALLATION =

The collection for Installations in Parse. Used by Parse::Installation.

"_Installation"
CLASS_SESSION =

The collection for revocable Sessions in Parse. Used by Parse::Session.

"_Session"
CLASS_ROLE =

The collection for Roles in Parse. Used by Parse::Role.

"_Role"
CLASS_PRODUCT =

The collection for to store Products (in-App purchases) in Parse. Used by Parse::Product.

"_Product"
CLASS_AUDIENCE =

The collection for Audiences in Parse. Used by Parse::Audience.

"_Audience"
CLASS_PUSH_STATUS =

The collection for Push Status in Parse. Used by Parse::PushStatus.

"_PushStatus"
CLASS_JOB_STATUS =

The collection for background job status in Parse. Used by Parse::JobStatus.

"_JobStatus"
CLASS_JOB_SCHEDULE =

The collection for scheduled background jobs in Parse. Used by Parse::JobSchedule.

"_JobSchedule"
CLASS_SCHEMA =

The internal schema collection in Parse. Managed by Parse Server.

"_SCHEMA"
TYPE_FILE =

The type label for hashes containing file data. Used by Parse::File.

"File"
TYPE_GEOPOINT =

The type label for hashes containing geopoints. Used by Parse::GeoPoint.

"GeoPoint"
TYPE_POLYGON =

The type label for hashes containing polygons. Used by Parse::Polygon.

"Polygon"
TYPE_OBJECT =

The type label for hashes containing a Parse object. Used by Parse::Object and subclasses.

"Object"
TYPE_DATE =

The type label for hashes containing a Parse date object. Used by Parse::Date.

"Date"
TYPE_BYTES =

The type label for hashes containing 'byte' data. Used by Parse::Bytes.

"Bytes"
TYPE_ACL =

The type label for hashes containing ACL data. Used by Parse::ACL

"ACL"
TYPE_NUMBER =

The type label for hashes storing numeric data.

"Number"
TYPE_POINTER =

The type label for hashes containing a Parse pointer.

"Pointer"
TYPE_RELATION =

The type label for hashes representing relational data.

"Relation"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Client::Connectable

#client

Class Attribute Details

.raise_on_save_failureBoolean

By default, we return true or false for save and destroy operations. If you prefer to have Parse::Object raise an exception instead, you can tell to do so either globally or on a per-model basis. When a save fails, it will raise a Parse::RecordNotSaved.

When enabled, if an error is returned by Parse due to saving or destroying a record, due to your before_save or before_delete validation cloud code triggers, Parse::Object will return the a Parse::RecordNotSaved exception type. This exception has an instance method of #object which contains the object that failed to save.

Examples:

Parse::Model.raise_on_save_failure = true # globally across all models
Song.raise_on_save_failure = true          # per-model

# or per-instance raise on failure
song.save!

Returns:

  • (Boolean)


137
138
139
# File 'lib/parse/model/model.rb', line 137

def raise_on_save_failure
  @global_raise_on_save_failure ||= false
end

Class Method Details

.find_class(str) ⇒ Parse::Object

Find a Parse::Model subclass matching this type or Pares collection name. This helper method is useful to find the corresponding class ruby Parse::Object subclass that handles the provided parse class.

Examples:

Parse::Model.find_class('_User') # => Parse::User
Parse::Model.find_class('_Date') # => Parse::Date
Parse::Model.find_class('Installation') # => Parse::Installation

class Artist < Parse::Object
  parse_class "Musician"
end

Parse::Model.find_class("Musician") # => Artist

Parameters:

  • str (String)

    the class name

Returns:

  • (Parse::Object)

    a Parse::Object subclass or a specific Parse type.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/parse/model/model.rb', line 163

def self.find_class(str)
  return Parse::File if str == TYPE_FILE
  return Parse::GeoPoint if str == TYPE_GEOPOINT
  return Parse::Polygon if str == TYPE_POLYGON
  return Parse::Date if str == TYPE_DATE
  return Parse::Bytes if str == TYPE_BYTES
  # return Parse::User if str == "User"
  # return Parse::Installation if str == "Installation"

  str = str.to_s
  # Basically go through all Parse::Object subclasses and see who is has a parse_class
  # set to this string. We will cache the results for future use.
  #
  # Anonymous descendants (`Class.new(Parse::Object)` without an
  # assigned constant) have `model_name.name == nil`, and the default
  # `parse_class` implementation calls `model_name.name` and raises
  # `ActiveModel::Name: Class name cannot be blank`. An unrescued
  # raise here poisons every subsequent `find_class` call (cf.
  # canonical-filter lookups, hidden-class registry, ACLScope role
  # expansion). Some tests, however, override `parse_class` on an
  # anonymous subclass to return a literal String — those are still
  # legitimate findables. Rescue per-descendant so the iteration
  # continues past the unnamed-and-default-parse_class case while
  # still considering anonymous-but-overridden ones.
  # Reference Parse::Model directly: this class method is inherited by
  # subclasses (e.g. Parse::Object.find_class), so a bare `@model_cache`
  # would resolve on the subclass singleton — which has no cache. The
  # cache lives on Parse::Model itself.
  Parse::Model.model_cache_mutex.synchronize do
    cached = Parse::Model.model_cache[str]
    return cached if cached

    result = Parse::Object.descendants.find do |f|
      begin
        cls = f.parse_class
      rescue StandardError
        next false
      end
      cls == str || cls == "_#{str}"
    end
    Parse::Model.model_cache[str] = result if result
    result
  end
end

Instance Method Details

#dirty?(field = nil) ⇒ Boolean

Add dirty? methods as aliases for changed? methods These provide compatibility with expected API

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/parse/model/model.rb', line 34

def dirty?(field = nil)
  if field.nil?
    changed?
  else
    field_changed = "#{field}_changed?"
    respond_to?(field_changed) ? send(field_changed) : false
  end
end