Class: Parse::Object

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

Direct Known Subclasses

User

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Object

Returns a new instance of Object.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/parse/object.rb', line 44

def initialize hash={}
  hash = string_keyed_hash hash
  if hash.has_key? 'objectId'
    @obj_id = hash['objectId']
    @raw_hash = hash
    @updated_hash = {}
  else
    @raw_hash = {}
    @updated_hash = hash
  end
  @deleted = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/parse/object.rb', line 174

def method_missing name, *args, &block
  if name =~ /^\w+$/ && args.empty? && block.nil?
    get_column name
  elsif name[-1] == '=' && args.size == 1 && block.nil?
    set_column name[0..-2], args.first
  else
    super
  end
end

Class Attribute Details

.auto_camel_caseObject

Returns the value of attribute auto_camel_case.



10
11
12
# File 'lib/parse/object.rb', line 10

def auto_camel_case
  @auto_camel_case
end

.parse_class_nameObject

Returns the value of attribute parse_class_name.



10
11
12
# File 'lib/parse/object.rb', line 10

def parse_class_name
  @parse_class_name
end

.parse_clientObject

Returns the value of attribute parse_client.



10
11
12
# File 'lib/parse/object.rb', line 10

def parse_client
  @parse_client
end

Instance Attribute Details

#aclObject

Returns the value of attribute acl.



42
43
44
# File 'lib/parse/object.rb', line 42

def acl
  @acl
end

#created_atObject

Returns the value of attribute created_at.



42
43
44
# File 'lib/parse/object.rb', line 42

def created_at
  @created_at
end

#obj_idObject

Returns the value of attribute obj_id.



42
43
44
# File 'lib/parse/object.rb', line 42

def obj_id
  @obj_id
end

#updated_atObject

Returns the value of attribute updated_at.



42
43
44
# File 'lib/parse/object.rb', line 42

def updated_at
  @updated_at
end

Class Method Details

.create(parse_class_name, mod = ::Object) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/parse/object.rb', line 12

def create parse_class_name, mod=::Object
  raise 'already defined' if mod.const_defined? parse_class_name

  if RESERVED_PARSE_CLASS.has_key? parse_class_name.to_s
    eval RESERVED_PARSE_CLASS[parse_class_name.to_s]
  else
    klass = Class.new(Parse::Object)
    klass.parse_class_name = parse_class_name.to_sym
    klass.auto_camel_case = true
    mod.const_set parse_class_name, klass
  end
end

.find(object_id_or_conditions, opts = {}) ⇒ Object



33
34
35
# File 'lib/parse/object.rb', line 33

def find object_id_or_conditions, opts={}
  parse_client.find self, object_id_or_conditions, opts
end

.find!(object_id_or_conditions, opts = {}) ⇒ Object



37
38
39
# File 'lib/parse/object.rb', line 37

def find! object_id_or_conditions, opts={}
  parse_client.find! self, object_id_or_conditions, opts
end

Instance Method Details

#create(hash, use_master_key = false) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/parse/object.rb', line 96

def create hash, use_master_key=false
  check_deleted!
  hash = string_keyed_hash hash
  @updated_hash.update hash
  #parse_client.create(self, @updated_hash).tap do |response|
  method = use_master_key ? :create! : :create
  parse_client.send(method, self, @updated_hash).tap do |response|
    @obj_id = response['objectId']
    @created_at = Date.parse response['createdAt']
    @updated_at = @created_at
    @raw_hash.update @updated_hash
    @raw_hash.update response
    @updated_hash.clear
  end
end

#create!(hash) ⇒ Object



112
113
114
# File 'lib/parse/object.rb', line 112

def create! hash
  create hash, true
end

#delete(use_master_key = false) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/parse/object.rb', line 132

def delete use_master_key=false
  raise 'You cannot delete new object' if new?
  check_deleted!
  #parse_client.delete(self).tap do |response|
  method = use_master_key ? :delete! : :delete
  parse_client.send(method, self).tap do |response|
    @deleted = true
  end
end

#delete!Object



142
143
144
# File 'lib/parse/object.rb', line 142

def delete!
  delete true
end

#deleted?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/parse/object.rb', line 65

def deleted?
  @deleted
end

#get_column(name) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/parse/object.rb', line 150

def get_column name
  name = name.to_s
  ret = @updated_hash[name]
  if ret.nil? && self.class.auto_camel_case
    ret = @updated_hash[name.camelize :lower]
  end
  if ret.nil?
    ret = @raw_hash[name]
    if ret.nil? && self.class.auto_camel_case
      ret = @raw_hash[name.camelize :lower]
    end
  end
  ret
end

#new?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/parse/object.rb', line 57

def new?
  !@deleted && @raw_hash.empty?
end

#parse_class_nameObject



73
74
75
# File 'lib/parse/object.rb', line 73

def parse_class_name
  self.class.parse_class_name
end

#parse_clientObject



69
70
71
# File 'lib/parse/object.rb', line 69

def parse_client
  self.class.parse_client
end

#save(hash = @updated_hash, use_master_key = false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/parse/object.rb', line 77

def save hash=@updated_hash, use_master_key=false
  check_deleted!

  unless hash.is_a? Hash
    hash = @updated_hash
    use_master_key = hash 
  end
  hash = string_keyed_hash hash
  if new?
    create hash, use_master_key
  else
    update hash, use_master_key
  end
end

#save!(hash = @updated_hash) ⇒ Object



92
93
94
# File 'lib/parse/object.rb', line 92

def save! hash=@updated_hash
  save hash, true
end

#set_column(name, value) ⇒ Object



165
166
167
168
# File 'lib/parse/object.rb', line 165

def set_column name, value
  check_deleted!
  @updated_hash[name] = value
end

#to_sObject



170
171
172
# File 'lib/parse/object.rb', line 170

def to_s
  "<#{parse_class_name}: #{{}.update(@raw_hash).update(@updated_hash).to_s}>"
end

#update(hash, use_master_key = false) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/parse/object.rb', line 116

def update hash, use_master_key=false
  check_deleted!
  hash = string_keyed_hash hash
  #parse_client.update(self, hash).tap do |response|
  method = use_master_key ? :update! : :update
  parse_client.send(method, self, hash).tap do |response|
    @updated_at = Date.parse response['updatedAt']
    @raw_hash.update @updated_hash
    @updated_hash.clear
  end
end

#update!(hash) ⇒ Object



128
129
130
# File 'lib/parse/object.rb', line 128

def update! hash
  update hash, true
end

#updated?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/parse/object.rb', line 61

def updated?
  !@deleted && !@updated_hash.empty?
end