class RubyJsonApiClient::Store
Attributes
default_adapter[RW]
default_serializer[RW]
Public Class Methods
default(format)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 47 def self.default(format) @store = new(format: format) end
get_adapter(name)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 40 def self.get_adapter(name) @adapters ||= {} if @adapters[name] @adapters[name].klass.new(@adapters[name].options) end end
get_serializer(name)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 33 def self.get_serializer(name) @serializers ||= {} if @serializers[name] @serializers[name].klass.new(@serializers[name].options) end end
instance()
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 51 def self.instance @store end
new(options)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 58 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
register_adapter(name, klass = nil, options = {})
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 3 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 = "RubyJsonApiClient::#{class_name}Adapter".constantize options = temp end @adapters[name] = OpenStruct.new({ klass: klass, options: options }) end
register_serializer(name, klass = nil, options = {})
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 18 def self.register_serializer(name, klass = nil, options = {}) @serializers ||= {} # 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 = "RubyJsonApiClient::#{class_name}Serializer".constantize options = temp end @serializers[name] = OpenStruct.new({ klass: klass, options: options }) end
Public Instance Methods
adapter_for_class(klass)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 72 def adapter_for_class(klass) @default_adapter end
delete(model)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 200 def delete(model) if model.persisted? klass = model.class adapter = adapter_for_class(klass) adapter.delete(model) end # TODO: Handle failures true end
find(klass, id)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 80 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).try(:tap) do |model| model.__origin__ = response end end
find_many_relationship(parent, name, options)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 108 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).map do |model| model.__origin__ = response if model.__origin__.nil? model end # wrap in enumerable proxy to allow reloading collection = RubyJsonApiClient::Collection.new(list) collection.__origin__ = response collection end
find_single_relationship(parent, name, options)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 129 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).tap do |model| model.__origin__ = response if model && model.__origin__.nil? end end
load(klass, data)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 163 def load(klass, data) end
load_collection(klass, url)
click to toggle source
TODO: make the 2 following functions a bit nicer
# File lib/ruby_json_api_client/store.rb, line 143 def load_collection(klass, url) adapter = adapter_for_class(klass) serializer = serializer_for_class(klass) response = adapter.get(url) serializer.extract_many(klass, response).map do |model| model.__origin__ = response model end end
load_single(klass, id, url)
click to toggle source
TODO: make nicer
# File lib/ruby_json_api_client/store.rb, line 154 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).tap do |model| model.__origin__ = response end end
merge(into, from)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 211 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)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 90 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)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 167 def reload(model) new_model = find(model.class, model.id) merge(model, new_model) end
save(model)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 172 def save(model) klass = model.class adapter = adapter_for_class(klass) serializer = serializer_for_class(klass) data = serializer.to_data(model) if model.persisted? response = adapter.update(model, data) else response = adapter.create(model, data) end if response && response != "" # convert response into model if there is one. # should probably rely on adapter status (error, not changed, changed). etc # # TODO: can we just use serializer to load data into model? new_model = serializer.extract_single(klass, model.id, response).tap do |m| m.__origin__ = response end merge(model, new_model) end # TODO: Handle failures true end
serializer_for_class(klass)
click to toggle source
# File lib/ruby_json_api_client/store.rb, line 76 def serializer_for_class(klass) @default_serializer end