Class: JSONAPI::Store
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/jsonapi/store.rb,
lib/jsonapi/store/types.rb,
lib/jsonapi/store/entity.rb,
lib/jsonapi/store/version.rb,
lib/jsonapi/store/identifier.rb
Overview
Basic in-memory store implementation compliant with JSON API
Defined Under Namespace
Modules: Types
Classes: Entity, Identifier
Constant Summary
collapse
- VERSION =
'0.1.0'
Instance Method Summary
collapse
Instance Method Details
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/jsonapi/store.rb', line 13
def <<(entity)
entity = Entity.new(entity)
entities << entity
entity.relationships.each do |_name, data|
Array(data).each do |relationship|
relationship = Entity.new(relationship)
self << relationship unless self[relationship.identifier]
end
end
self
end
|
41
42
43
44
|
# File 'lib/jsonapi/store.rb', line 41
def all(type)
type = type.to_s
select { |entity| entity.type == type }
end
|
65
66
67
|
# File 'lib/jsonapi/store.rb', line 65
def each(&block)
entities.each(&block)
end
|
52
53
54
|
# File 'lib/jsonapi/store.rb', line 52
def entities
@entities ||= []
end
|
32
33
34
35
|
# File 'lib/jsonapi/store.rb', line 32
def fetch(*args)
identifier = args.size == 2 ? args.join('/') : args.first.to_s
detect { |entity| entity.identifier == identifier }
end
|
#size ⇒ Integer
57
58
59
|
# File 'lib/jsonapi/store.rb', line 57
def size
entities.size
end
|
#types ⇒ <String>
47
48
49
|
# File 'lib/jsonapi/store.rb', line 47
def types
entities.map(&:type).uniq
end
|