Class: Loco::FixtureAdapter
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
-
#update_record(record, &block) ⇒ Object
Methods inherited from Adapter
get_transforms, register_transform, #serialize
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
66
67
68
|
# File 'lib/motion-loco/fixture_adapter.rb', line 66
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
25
|
# File 'lib/motion-loco/fixture_adapter.rb', line 13
def find(record, id, &block)
type = record.class
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).find{|obj| obj[:id] == id }
if data
load(type, record, data)
block.call(record) if block.is_a? Proc
record
else
raise Loco::FixtureAdapter::RecordNotFound, "#{type} with the id `#{id}' could not be loaded."
end
end
|
#find_all(type, records, &block) ⇒ Object
27
28
29
30
31
32
33
34
|
# File 'lib/motion-loco/fixture_adapter.rb', line 27
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)
load(type, records, data)
block.call(records) if block.is_a? Proc
records
end
|
#find_many(type, records, ids, &block) ⇒ Object
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/motion-loco/fixture_adapter.rb', line 36
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)
}
load(type, records, data)
block.call(records) if block.is_a? Proc
records
end
|
#find_query(type, records, query, &block) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/motion-loco/fixture_adapter.rb', line 47
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
}
load(type, records, data)
block.call(records) if block.is_a? Proc
records
end
|
#update_record(record, &block) ⇒ Object
62
63
64
|
# File 'lib/motion-loco/fixture_adapter.rb', line 62
def update_record(record, &block)
raise NoMethodError, "Loco::FixtureAdapter cannot update records."
end
|