Class: Loco::FixtureAdapter

Inherits:
Adapter
  • Object
show all
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

Methods inherited from Adapter

get_transforms, register_transform, #serialize

Instance Method Details

#create_record(record, &block) ⇒ Object

Raises:

  • (NoMethodError)


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

Raises:

  • (NoMethodError)


67
68
69
# File 'lib/motion-loco/fixture_adapter.rb', line 67

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
26
# File 'lib/motion-loco/fixture_adapter.rb', line 13

def find(record, id, &block)
  type = record.class
  error = Pointer.new(:id)
  filename = File.join(NSBundle.mainBundle.resourcePath, "fixtures", "#{type.to_s.underscore.gsub('nskvo_notifying_', '').pluralize}.json")
  file = File.read(filename)
  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



28
29
30
31
32
33
34
35
# File 'lib/motion-loco/fixture_adapter.rb', line 28

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



37
38
39
40
41
42
43
44
45
46
# File 'lib/motion-loco/fixture_adapter.rb', line 37

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/motion-loco/fixture_adapter.rb', line 48

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

Raises:

  • (NoMethodError)


63
64
65
# File 'lib/motion-loco/fixture_adapter.rb', line 63

def update_record(record, &block)
  raise NoMethodError, "Loco::FixtureAdapter cannot update records."
end