Class: Nitron::Model
- Inherits:
-
NSManagedObject
- Object
- NSManagedObject
- Nitron::Model
- Defined in:
- lib/nitron/data/model.rb
Class Method Summary collapse
- .all ⇒ Object
- .create(attributes = {}) ⇒ Object
- .destroy(object) ⇒ Object
- .entityDescription ⇒ Object
- .find(object_id) ⇒ Object
- .first ⇒ Object
- .method_missing(method, *args, &block) ⇒ Object
- .new(attributes = {}) ⇒ Object
- .order(*args) ⇒ Object
- .respond_to?(method) ⇒ Boolean
- .where(*args) ⇒ Object
Instance Method Summary collapse
Class Method Details
.all ⇒ Object
4 5 6 |
# File 'lib/nitron/data/model.rb', line 4 def all Data::Relation.alloc.initWithClass(self) end |
.create(attributes = {}) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/nitron/data/model.rb', line 8 def create(attributes={}) model = new(attributes) model.save model end |
.destroy(object) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/nitron/data/model.rb', line 15 def destroy(object) if context = object.managedObjectContext context.deleteObject(object) error = Pointer.new(:object) context.save(error) end end |
.entityDescription ⇒ Object
24 25 26 |
# File 'lib/nitron/data/model.rb', line 24 def entityDescription @_metadata ||= UIApplication.sharedApplication.delegate.managedObjectModel.entitiesByName[name] end |
.find(object_id) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/nitron/data/model.rb', line 28 def find(object_id) unless entity = find_by_id(object_id) raise "No record found!" end entity end |
.first ⇒ Object
36 37 38 |
# File 'lib/nitron/data/model.rb', line 36 def first relation.first end |
.method_missing(method, *args, &block) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/nitron/data/model.rb', line 40 def method_missing(method, *args, &block) if method.start_with?("find_by_") attribute = method.gsub("find_by_", "") relation.where("#{attribute} = ?", *args).first else super end end |
.new(attributes = {}) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/nitron/data/model.rb', line 49 def new(attributes={}) self.alloc.initWithEntity(entityDescription, insertIntoManagedObjectContext:nil).tap do |model| attributes.each do |keyPath, value| model.setValue(value, forKey:keyPath) end end end |
.order(*args) ⇒ Object
65 66 67 |
# File 'lib/nitron/data/model.rb', line 65 def order(*args) relation.order(*args) end |
.respond_to?(method) ⇒ Boolean
57 58 59 60 61 62 63 |
# File 'lib/nitron/data/model.rb', line 57 def respond_to?(method) if method.start_with?("find_by_") true else super end end |
.where(*args) ⇒ Object
69 70 71 |
# File 'lib/nitron/data/model.rb', line 69 def where(*args) relation.where(*args) end |
Instance Method Details
#destroy ⇒ Object
80 81 82 |
# File 'lib/nitron/data/model.rb', line 80 def destroy self.class.destroy(self) end |
#inspect ⇒ Object
84 85 86 87 88 |
# File 'lib/nitron/data/model.rb', line 84 def inspect properties = entity.properties.map { |property| "#{property.name}: #{valueForKey(property.name).inspect}" } "#<#{entity.name} #{properties.join(", ")}>" end |
#save ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/nitron/data/model.rb', line 90 def save unless context = managedObjectContext context = UIApplication.sharedApplication.delegate.managedObjectContext context.insertObject(self) end error = Pointer.new(:object) context.save(error) end |