Class: Loco::SQLiteAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/motion-loco/sqlite_adapter.rb

Defined Under Namespace

Classes: RecordNotFound

Instance Method Summary collapse

Methods inherited from Adapter

get_transforms, register_transform

Instance Method Details

#create_record(record, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/motion-loco/sqlite_adapter.rb', line 8

def create_record(record, &block)
  type = record.class
  record.id = generate_id(type)
  data = NSEntityDescription.insertNewObjectForEntityForName(type.to_s, inManagedObjectContext:context(type))
  record.serialize(root: false, include_id: true).each do |key, value|
    data.setValue(value, forKey:key)
  end
  save_data_for_type(type)
  load(type, record, data)
  save_has_many(record)
  block.call(record) if block.is_a? Proc
  record
end

#delete_record(record, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/motion-loco/sqlite_adapter.rb', line 22

def delete_record(record, &block)
  type = record.class
  data = request(type, { id: record.id }).first
  if data
    context(type).deleteObject(data)
    save_data_for_type(type)
    block.call(record) if block.is_a? Proc
    record
  else
    raise Loco::FixtureAdapter::RecordNotFound, "#{type} with the id `#{record.id}' could not be deleted."
  end
end

#find(record, id, &block) ⇒ Object



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

def find(record, id, &block)
  type = record.class
  data = request(type, { id: record.id }).first
  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



47
48
49
50
51
52
# File 'lib/motion-loco/sqlite_adapter.rb', line 47

def find_all(type, records, &block)
  data = request(type)
  load(type, records, data)
  block.call(records) if block.is_a? Proc
  records
end

#find_many(type, records, ids, &block) ⇒ Object



54
55
56
57
58
59
# File 'lib/motion-loco/sqlite_adapter.rb', line 54

def find_many(type, records, ids, &block)
  data = request(type, { id: ids })
  load(type, records, data)
  block.call(records) if block.is_a? Proc
  records
end

#find_query(type, records, query, &block) ⇒ Object



61
62
63
64
65
66
# File 'lib/motion-loco/sqlite_adapter.rb', line 61

def find_query(type, records, query, &block)
  data = request(type, query)
  load(type, records, data)
  block.call(records) if block.is_a? Proc
  records
end

#serialize(record, options = {}) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/motion-loco/sqlite_adapter.rb', line 85

def serialize(record, options={})
  json = {}
  record.class.get_class_relationships.select{|relationship| relationship[:belongs_to] }.each do |relationship|
    key = "#{relationship[:belongs_to]}_id".to_sym
    json[key] = record.valueForKey(key)
  end
  super(record, options, json)
end

#update_record(record, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/motion-loco/sqlite_adapter.rb', line 68

def update_record(record, &block)
  type = record.class
  data = request(type, { id: record.id }).first
  if data
    record.serialize(root: false).each do |key, value|
      data.setValue(value, forKey:key)
    end
    save_data_for_type(type)
    load(type, record, data)
    save_has_many(record)
    block.call(record) if block.is_a? Proc
    record
  else
    raise Loco::FixtureAdapter::RecordNotFound, "#{type} with the id `#{record.id}' could not be updated."
  end
end