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.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/parse/object.rb', line 40

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



143
144
145
146
147
148
149
150
151
# File 'lib/parse/object.rb', line 143

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.



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

def acl
  @acl
end

#created_atObject

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#obj_idObject

Returns the value of attribute obj_id.



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

def obj_id
  @obj_id
end

#updated_atObject

Returns the value of attribute updated_at.



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

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

Instance Method Details

#create(hash) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/parse/object.rb', line 83

def create hash
  check_deleted!
  hash = string_keyed_hash hash
  @updated_hash.update hash
  parse_client.create(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

#deleteObject



107
108
109
110
111
112
113
# File 'lib/parse/object.rb', line 107

def delete
  raise 'You cannot delete new object' if new?
  check_deleted!
  parse_client.delete(self).tap do |response|
    @deleted = true
  end
end

#deleted?Boolean

Returns:

  • (Boolean)


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

def deleted?
  @deleted
end

#get_column(name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/parse/object.rb', line 119

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)


53
54
55
# File 'lib/parse/object.rb', line 53

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

#parse_class_nameObject



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

def parse_class_name
  self.class.parse_class_name
end

#parse_clientObject



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

def parse_client
  self.class.parse_client
end

#save(hash = @updated_hash) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/parse/object.rb', line 73

def save hash=@updated_hash
  check_deleted!
  hash = string_keyed_hash hash
  if new?
    create hash
  else
    update hash
  end
end

#set_column(name, value) ⇒ Object



134
135
136
137
# File 'lib/parse/object.rb', line 134

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

#to_sObject



139
140
141
# File 'lib/parse/object.rb', line 139

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

#update(hash) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/parse/object.rb', line 97

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

#updated?Boolean

Returns:

  • (Boolean)


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

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