-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbuilder.rb
More file actions
79 lines (69 loc) · 2.89 KB
/
builder.rb
File metadata and controls
79 lines (69 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
73
74
75
76
77
78
79
# encoding: UTF-8
# frozen_string_literal: true
require "active_support"
require "active_support/inflector"
require "active_support/core_ext"
require_relative "../object"
module Parse
# Create all Parse::Object subclasses, including their properties and inferred
# associations by importing the schema for the remote collections in a Parse
# application. Uses the default configured client.
# @return [Array] an array of created Parse::Object subclasses.
# @see Parse::Model::Builder.build!
def self.auto_generate_models!
Parse.schemas.map do |schema|
Parse::Model::Builder.build!(schema)
end
end
class Model
# 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 Builder
# Builds a ruby Parse::Object subclass with the provided schema information.
# @param schema [Hash] the Parse-formatted hash schema for a collection. This hash
# should two keys:
# * className: Contains the name of the collection.
# * field: A hash containg the column fields and their type.
# @raise ArgumentError when the className could not be inferred from the schema.
# @return [Array] an array of Parse::Object subclass constants.
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
# Remove leading underscore, as ruby constants have to start with an uppercase letter
className = className[1..] if className[0] == '_'
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
end
end
end