Class: JSONAPI::Store

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

#<<(entity) ⇒ JSONAPI::Store

Returns store itself.

Parameters:

Returns:



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

#all(type) ⇒ <JSONAPI::Store::Entity>

Parameters:

  • type (#to_s)

Returns:



41
42
43
44
# File 'lib/jsonapi/store.rb', line 41

def all(type)
  type = type.to_s
  select { |entity| entity.type == type }
end

#each(&block) ⇒ <JSONAPI::Store::Entity> #eachEnumerator<JSONAPI::Store::Entity>

Overloads:



65
66
67
# File 'lib/jsonapi/store.rb', line 65

def each(&block)
  entities.each(&block)
end

#entities<JSONAPI::Store::Entity>

Returns:



52
53
54
# File 'lib/jsonapi/store.rb', line 52

def entities
  @entities ||= []
end

#fetch(identifier) ⇒ JSONAPI::Store::Entity? #fetch(type, id) ⇒ JSONAPI::Store::Entity? Also known as: []

Overloads:



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

#sizeInteger

Returns:

  • (Integer)


57
58
59
# File 'lib/jsonapi/store.rb', line 57

def size
  entities.size
end

#types<String>

Returns:

  • (<String>)


47
48
49
# File 'lib/jsonapi/store.rb', line 47

def types
  entities.map(&:type).uniq
end