Class: Loco::FixtureAdapter
- Defined in:
- lib/motion-loco/fixture_adapter.rb
Defined Under Namespace
Classes: RecordNotFound
Constant Summary collapse
- JSON_OPTIONS =
NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves | NSJSONReadingAllowFragments
Instance Method Summary collapse
- #create_record(record, &block) ⇒ Object
- #delete_record(record, &block) ⇒ Object
- #find(record, id, &block) ⇒ Object
- #find_all(type, records, &block) ⇒ Object
- #find_many(type, records, ids, &block) ⇒ Object
- #find_query(type, records, query, &block) ⇒ Object
- #save_record(record, &block) ⇒ Object
Instance Method Details
#create_record(record, &block) ⇒ Object
9 10 11 |
# File 'lib/motion-loco/fixture_adapter.rb', line 9 def create_record(record, &block) raise NoMethodError, "Loco::FixtureAdapter cannot create records." end |
#delete_record(record, &block) ⇒ Object
65 66 67 |
# File 'lib/motion-loco/fixture_adapter.rb', line 65 def delete_record(record, &block) raise NoMethodError, "Loco::FixtureAdapter cannot delete records." end |
#find(record, id, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/motion-loco/fixture_adapter.rb', line 13 def find(record, id, &block) error = Pointer.new(:id) file = File.read(File.join(NSBundle.mainBundle.resourcePath, "fixtures", "#{record.class.to_s.underscore.pluralize}.json")) data = NSJSONSerialization.JSONObjectWithData(file.to_data, options:JSON_OPTIONS, error:error).find{|obj| obj[:id] == id } if data record.load(id, data) block.call(record) if block.is_a? Proc record else raise Loco::FixtureAdapter::RecordNotFound, "#{record.class} with the id `#{id}' could not be loaded." end end |
#find_all(type, records, &block) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/motion-loco/fixture_adapter.rb', line 26 def find_all(type, records, &block) error = Pointer.new(:id) file = File.read(File.join(NSBundle.mainBundle.resourcePath, "fixtures", "#{type.to_s.underscore.pluralize}.json")) data = NSJSONSerialization.JSONObjectWithData(file.to_data, options:JSON_OPTIONS, error:error) records.load(type, data) block.call(records) if block.is_a? Proc records end |
#find_many(type, records, ids, &block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/motion-loco/fixture_adapter.rb', line 35 def find_many(type, records, ids, &block) error = Pointer.new(:id) file = File.read(File.join(NSBundle.mainBundle.resourcePath, "fixtures", "#{type.to_s.underscore.pluralize}.json")) data = NSJSONSerialization.JSONObjectWithData(file.to_data, options:JSON_OPTIONS, error:error).select{|obj| ids.map(&:to_s).include?(obj[:id].to_s) } records.load(type, data) block.call(records) if block.is_a? Proc records end |
#find_query(type, records, query, &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/motion-loco/fixture_adapter.rb', line 46 def find_query(type, records, query, &block) error = Pointer.new(:id) file = File.read(File.join(NSBundle.mainBundle.resourcePath, "fixtures", "#{type.to_s.underscore.pluralize}.json")) data = NSJSONSerialization.JSONObjectWithData(file.to_data, options:JSON_OPTIONS, error:error).select{|obj| match = true query.each do |key, value| match = false if obj[key.to_sym] != value end match } records.load(type, data) block.call(records) if block.is_a? Proc records end |
#save_record(record, &block) ⇒ Object
61 62 63 |
# File 'lib/motion-loco/fixture_adapter.rb', line 61 def save_record(record, &block) raise NoMethodError, "Loco::FixtureAdapter cannot save records." end |