Class: Parse::Model::Builder
- Inherits:
- Object
- Object
- Parse::Model::Builder
- Defined in:
- lib/parse/model/core/builder.rb
Overview
This class provides a method to automatically generate Parse::Object subclasses, including their properties and inferred associations by importing the schema for the remote collections in a Parse application.
Class Method Summary collapse
- .build!(schema) ⇒ Array
Builds a ruby Parse::Object subclass with the provided schema information.
Class Method Details
.build!(schema) ⇒ Array
Builds a ruby Parse::Object subclass with the provided schema information.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | # File 'lib/parse/model/core/builder.rb', line 34 def self.build!(schema) unless schema.is_a?(Hash) raise ArgumentError, "Schema parameter should be a Parse schema hash object." end schema = schema.with_indifferent_access fields = schema[:fields] || {} className = schema[:className] if className.blank? raise ArgumentError, "No valid className provided for schema hash" end begin klass = Parse::Model.find_class className klass = ::Object.const_get(className.to_parse_class) if klass.nil? rescue => e klass = ::Class.new(Parse::Object) ::Object.const_set(className, klass) end base_fields = Parse::Properties::BASE.keys class_fields = klass.field_map.values + [:className] fields.each do |field, type| field = field.to_sym key = field.to_s.underscore.to_sym next if base_fields.include?(field) || class_fields.include?(field) data_type = type[:type].downcase.to_sym if data_type == :pointer klass.belongs_to key, as: type[:targetClass], field: field elsif data_type == :relation klass.has_many key, through: :relation, as: type[:targetClass], field: field else klass.property key, data_type, field: field end class_fields.push(field) end klass end |