Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/model/model.rb

Overview

Add extensions to the String class.

Instance Method Summary collapse

Instance Method Details

#columnizeString

This method returns a camel-cased version of the string with the first letter of the string in lower case. This is the standard naming convention for Parse columns and property fields. This has special exception to the string "id", which returns "objectId". This is the default name filtering method for all defined properties and query keys in Parse::Query.

Examples:

"my_field".columnize # => "myField"
"MyDataColumn".columnize # => "myDataColumn"
"id".columnize # => "objectId" (special)

Returns:

See Also:



293
294
295
296
297
# File 'lib/parse/model/model.rb', line 293

def columnize
  return Parse::Model::OBJECT_ID if self == Parse::Model::ID
  u = "_".freeze
  (first == u ? sub(u, "") : self).camelize(:lower)
end

#to_parse_class(singularize: false) ⇒ String

Convert a string to a Parse class name. This method tries to find a responsible Parse::Object subclass that potentially matches the given string. If no match is found, it returns the camelized version of the string. This method is used internally for matching association attributes to registered Parse::Object subclasses. The method can also singularize the name before performing conversion.

Examples:

"users".to_parse_class(singularize: true) # => "_User"
"users".to_parse_class # => "Users"
"song_data".to_parse_class # => "SongData"

Parameters:

  • singularize (Boolean) (defaults to: false)

    whether the string should be singularized first before performing conversion.

Returns:

  • (String)

    the matching Parse class for this string.



313
314
315
316
317
318
319
320
321
322
323
# File 'lib/parse/model/model.rb', line 313

def to_parse_class(singularize: false)
  final_class = singularize ? self.singularize.camelize : self.camelize
  klass = Parse::Model.find_class(final_class) || Parse::Model.find_class(self)
  # A registered class wins (handles a custom parse_class table mapping).
  return klass.parse_class if klass.present?
  # Fallback: resolve a built-in system class by name even when its Ruby
  # class is not yet registered (declaration-time load-order). This keeps a
  # `belongs_to :user` declared before Parse::User loads from freezing the
  # literal "User" instead of "_User" into the schema targetClass.
  Parse::Model::SYSTEM_CLASS_MAP[final_class] || final_class
end