Class: RubyJsonApiClient::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_json_api_client/store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Store

Returns a new instance of Store.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ruby_json_api_client/store.rb', line 57

def initialize(options)
  if options[:format]
    options[:adapter] = options[:format]
    options[:serializer] = options[:format]
  end

  @default_adapter = self.class.get_adapter(options[:adapter])
  @default_serializer = self.class.get_serializer(options[:serializer])

  if @default_serializer && @default_serializer.respond_to?(:store=)
    @default_serializer.store = self
  end
end

Instance Attribute Details

#default_adapterObject

Returns the value of attribute default_adapter.



54
55
56
# File 'lib/ruby_json_api_client/store.rb', line 54

def default_adapter
  @default_adapter
end

#default_serializerObject

Returns the value of attribute default_serializer.



55
56
57
# File 'lib/ruby_json_api_client/store.rb', line 55

def default_serializer
  @default_serializer
end

Class Method Details

.default(format) ⇒ Object



46
47
48
# File 'lib/ruby_json_api_client/store.rb', line 46

def self.default(format)
  @store = new(format: format)
end

.get_adapter(name) ⇒ Object



39
40
41
42
43
44
# File 'lib/ruby_json_api_client/store.rb', line 39

def self.get_adapter(name)
  @adapters ||= {}
  if @adapters[name]
    @adapters[name].klass.new(@adapters[name].options)
  end
end

.get_serializer(name) ⇒ Object



32
33
34
35
36
37
# File 'lib/ruby_json_api_client/store.rb', line 32

def self.get_serializer(name)
  @serializers ||= {}
  if @serializers[name]
    @serializers[name].klass.new
  end
end

.instanceObject



50
51
52
# File 'lib/ruby_json_api_client/store.rb', line 50

def self.instance
  @store
end

.register_adapter(name, klass = nil, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby_json_api_client/store.rb', line 5

def self.register_adapter(name, klass = nil, options = {})
  @adapters ||= {}

  # allow for 2 arguments (automatically figure out class if so)
  if !klass.is_a?(Class)
    # klass is options. autoamtically figure out klass and set options
    temp = klass || options
    class_name = name.to_s.camelize
    klass = Kernel.const_get("RubyJsonApiClient::#{class_name}Adapter")
    options = temp
  end

  @adapters[name] = OpenStruct.new({ klass: klass, options: options })
end

.register_serializer(name, klass = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_json_api_client/store.rb', line 21

def self.register_serializer(name, klass = nil)
  @serializers ||= {}

  if klass.nil?
    class_name = name.to_s.camelize
    klass = Kernel.const_get("RubyJsonApiClient::#{class_name}Serializer")
  end

  @serializers[name] = OpenStruct.new({ klass: klass })
end

Instance Method Details

#adapter_for_class(klass) ⇒ Object



71
72
73
# File 'lib/ruby_json_api_client/store.rb', line 71

def adapter_for_class(klass)
  @default_adapter
end

#find(klass, id) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/ruby_json_api_client/store.rb', line 79

def find(klass, id)
  adapter = adapter_for_class(klass)
  serializer = serializer_for_class(klass)

  response = adapter.find(klass, id)
  serializer.extract_single(klass, id, response).tap do |model|
    model.__origin__ = response
  end
end

#find_many_relationship(parent, name, options) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/ruby_json_api_client/store.rb', line 107

def find_many_relationship(parent, name, options)
  # needs to use adapter_for_class
  serializer = @default_serializer

  # ensure parent is loaded
  reload(parent) if parent.__origin__.nil?

  response = parent.__origin__

  # find the relationship
  list = serializer.extract_many_relationship(parent, name, options, response)

  # wrap in enumerable proxy to allow reloading
  collection = RubyJsonApiClient::Collection.new(list)
  collection.__origin__ = response
  collection
end

#find_single_relationship(parent, name, options) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/ruby_json_api_client/store.rb', line 125

def find_single_relationship(parent, name, options)
  # needs to use serializer_for_class
  serializer = @default_serializer

  reload(parent) if parent.__origin__.nil?

  response = parent.__origin__

  serializer.extract_single_relationship(parent, name, options, response)
end

#load(klass, data) ⇒ Object



150
151
152
# File 'lib/ruby_json_api_client/store.rb', line 150

def load(klass, data)

end

#load_collection(klass, url) ⇒ Object



136
137
138
139
140
141
# File 'lib/ruby_json_api_client/store.rb', line 136

def load_collection(klass, url)
  adapter = adapter_for_class(klass)
  serializer = serializer_for_class(klass)
  response = adapter.get(url)
  serializer.extract_many(klass, response)
end

#load_single(klass, id, url) ⇒ Object



143
144
145
146
147
148
# File 'lib/ruby_json_api_client/store.rb', line 143

def load_single(klass, id, url)
  adapter = adapter_for_class(klass)
  serializer = serializer_for_class(klass)
  response = adapter.get(url)
  serializer.extract_single(klass, id, response)
end

#merge(into, from) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/ruby_json_api_client/store.rb', line 159

def merge(into, from)
  into.__origin__ = from.__origin__
  into.meta = from.meta

  from.class.fields.reduce(into) do |model, attr|
    call = "#{attr}="
    model.send(call, from.send(attr)) if model.respond_to?(call)
    model
  end
end

#query(klass, params) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby_json_api_client/store.rb', line 89

def query(klass, params)
  adapter = adapter_for_class(klass)
  serializer = serializer_for_class(klass)

  response = adapter.find_many(klass, params)
  list = serializer.extract_many(klass, response)

  list.each do |model|
    # let the model know where it came from
    model.__origin__ = response
  end

  # cant tap proxy
  collection = RubyJsonApiClient::Collection.new(list)
  collection.__origin__ = response
  collection
end

#reload(model) ⇒ Object



154
155
156
157
# File 'lib/ruby_json_api_client/store.rb', line 154

def reload(model)
  new_model = find(model.class, model.id)
  merge(model, new_model)
end

#serializer_for_class(klass) ⇒ Object



75
76
77
# File 'lib/ruby_json_api_client/store.rb', line 75

def serializer_for_class(klass)
  @default_serializer
end