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.



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/parse/object.rb', line 31

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



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

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.



5
6
7
# File 'lib/parse/object.rb', line 5

def auto_camel_case
  @auto_camel_case
end

.parse_class_nameObject

Returns the value of attribute parse_class_name.



5
6
7
# File 'lib/parse/object.rb', line 5

def parse_class_name
  @parse_class_name
end

.parse_clientObject

Returns the value of attribute parse_client.



5
6
7
# File 'lib/parse/object.rb', line 5

def parse_client
  @parse_client
end

Instance Attribute Details

#aclObject

Returns the value of attribute acl.



29
30
31
# File 'lib/parse/object.rb', line 29

def acl
  @acl
end

#created_atObject

Returns the value of attribute created_at.



29
30
31
# File 'lib/parse/object.rb', line 29

def created_at
  @created_at
end

#obj_idObject

Returns the value of attribute obj_id.



29
30
31
# File 'lib/parse/object.rb', line 29

def obj_id
  @obj_id
end

#updated_atObject

Returns the value of attribute updated_at.



29
30
31
# File 'lib/parse/object.rb', line 29

def updated_at
  @updated_at
end

Class Method Details

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



7
8
9
10
11
12
13
14
# File 'lib/parse/object.rb', line 7

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

  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

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



24
25
26
# File 'lib/parse/object.rb', line 24

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

Instance Method Details

#create(hash) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/parse/object.rb', line 74

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



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

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)


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

def deleted?
  @deleted
end

#get_column(name) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/parse/object.rb', line 110

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)


44
45
46
# File 'lib/parse/object.rb', line 44

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

#parse_class_nameObject



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

def parse_class_name
  self.class.parse_class_name
end

#parse_clientObject



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

def parse_client
  self.class.parse_client
end

#save(hash = @updated_hash) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/parse/object.rb', line 64

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



125
126
127
128
# File 'lib/parse/object.rb', line 125

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

#to_sObject



130
131
132
# File 'lib/parse/object.rb', line 130

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

#update(hash) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/parse/object.rb', line 88

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)


48
49
50
# File 'lib/parse/object.rb', line 48

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