Module: Quiver::Adapter::Memory

Includes:
HelpersHelpers
Defined in:
lib/quiver/adapter/memory_helpers.rb,
lib/quiver/adapter/memory_uuid_primary_key.rb

Defined Under Namespace

Modules: ClassMethods, UuidPrimaryKey

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host) ⇒ Object



8
9
10
11
12
13
# File 'lib/quiver/adapter/memory_helpers.rb', line 8

def self.included(host)
  super

  host.adapter_type(:memory)
  host.send(:extend, ClassMethods)
end

Instance Method Details

#countObject



33
34
35
36
37
# File 'lib/quiver/adapter/memory_helpers.rb', line 33

def count
  count = stores.get(store_key).count

  Quiver::Adapter::AdapterResult.new(count)
end

#create(attributes, transaction) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/quiver/adapter/memory_helpers.rb', line 39

def create(attributes, transaction)
  primary_key = new_primary_key

  if attributes[:__type__]
    attributes = attributes.merge(
      primary_key_name => primary_key,
      attributes[:__type__][:name] => attributes[:__type__][:value]
    )

    attributes.delete(:__type__)
  else
    attributes = attributes.merge(
      primary_key_name => primary_key
    )
  end

  store[primary_key] = attributes

  Quiver::Adapter::AdapterResult.new(attributes.dup)
end

#find(primary_key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/quiver/adapter/memory_helpers.rb', line 22

def find(primary_key)
  Quiver::Adapter::AdapterResult.new do |errors|
    if store.key?(primary_key)
      load_additional([store[primary_key].dup]).first
    else
      errors << Quiver::Mapper::NotFoundError.new('record', 'not_found')
      nil
    end
  end
end

#hard_delete(attributes, transaction) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/quiver/adapter/memory_helpers.rb', line 81

def hard_delete(attributes, transaction)
  store = stores.get(store_key)

  Quiver::Adapter::AdapterResult.new do |errors|
    object = store.delete(attributes[primary_key_name])

    if object
      {}
    else
      errors << Quiver::Mapper::NotFoundError.new('record', 'not_found')
      nil
    end
  end
end

#query(q = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/quiver/adapter/memory_helpers.rb', line 96

def query(q={})
  store = stores.get(store_key)

  filter_params = q[:filter] || {}
  sort_params = q[:sort] || {}
  pagination_params = q[:page] || {}

  objects = filter_klass.new(
    store.values,
    filter_params
  ).filter

  if sort_params.any?
    objects = objects.sort do |a, b|
      sort_params.reduce(0) do |memo, (attr, asc)|
        attr = attr.to_sym

        # A memo of 0 means either no sorting has happened yet,
        # or there has been a tie that might need breaking.
        if memo == 0
          sign = asc ? 1 : -1
          sign * (a[attr] <=> b[attr])
        else
          memo
        end
      end
    end
  end

  offset = pagination_params['offset'] || 0
  limit  = pagination_params['limit'] || -1
  total_count = objects.count

  if limit == -1
    range_end = -1
  else
    range_end = offset + limit - 1
  end
  objects = objects[offset..range_end]

  objects = load_additional(objects.map do |attrs|
    attrs.dup
  end)

  result = Quiver::Adapter::AdapterResult.new(objects)

  if pagination_params.any?
    result.data[:pagination_offset] = offset
    result.data[:pagination_limit] = limit
    result.data[:total_count] = total_count
  end

  result
end

#update(attributes, transaction) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/quiver/adapter/memory_helpers.rb', line 60

def update(attributes, transaction)
  primary_key = attributes[primary_key_name]

  if attributes[:__type__]
    attributes = attributes.merge(
      attributes[:__type__][:name] => attributes[:__type__][:value]
    )

    attributes.delete(:__type__)
  end

  Quiver::Adapter::AdapterResult.new do |errors|
    if store.key?(primary_key)
      store[primary_key] = attributes.dup
    else
      errors << Quiver::Mapper::NotFoundError.new('record', 'does_not_exist')
      nil
    end
  end
end