Class: MotionParse::Base
- Inherits:
-
Object
- Object
- MotionParse::Base
- Defined in:
- lib/motion-parse/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#parse_object ⇒ Object
Returns the value of attribute parse_object.
Class Method Summary collapse
- .all(&block) ⇒ Object
- .attribute(*fields) ⇒ Object
- .find(hash, &block) ⇒ Object
- .get(query, &block) ⇒ Object
- .has_many(association) ⇒ Object
- .query ⇒ Object
Instance Method Summary collapse
-
#initialize(arg = nil) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(arg = nil) ⇒ Base
Returns a new instance of Base.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/motion-parse/base.rb', line 20 def initialize(arg = nil) if arg.is_a?(PFObject) @parse_object = arg else @parse_object = PFObject.objectWithClassName(self.class.name) if arg.is_a?(Hash) arg.each do |key, value| @parse_object.setObject(value, forKey:key) if attributes.include?(key) end end end end |
Instance Attribute Details
#parse_object ⇒ Object
Returns the value of attribute parse_object.
3 4 5 |
# File 'lib/motion-parse/base.rb', line 3 def parse_object @parse_object end |
Class Method Details
.all(&block) ⇒ Object
42 43 44 |
# File 'lib/motion-parse/base.rb', line 42 def self.all(&block) find({}, &block) end |
.attribute(*fields) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/motion-parse/base.rb', line 8 def self.attribute(*fields) fields.each do |field| define_method field do @parse_object.objectForKey(field) end define_method "#{field}=" do |value| @parse_object.setObject(value, forKey:field) end end self.attributes += fields end |
.find(hash, &block) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/motion-parse/base.rb', line 33 def self.find(hash, &block) q = query hash.each do |key, value| q.whereKey(key, equalTo:value) end get(q, &block) end |
.get(query, &block) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/motion-parse/base.rb', line 59 def self.get(query, &block) if block query.findObjectsInBackgroundWithBlock(lambda { |objects, error| objects = objects.map { |obj| new(obj) } if objects block.call(objects, error) }) else query.findObjects.map { |obj| new(obj) } end end |
.has_many(association) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/motion-parse/base.rb', line 46 def self.has_many(association) define_method association do |&block| klass = association.to_s.classify.constantize klass.find(self.class.name.foreign_key => parse_object, &block) end end |
.query ⇒ Object
55 56 57 |
# File 'lib/motion-parse/base.rb', line 55 def self.query PFQuery.alloc.initWithClassName(self.name) end |