Class: RubyJsonApiClient::Store
- Inherits:
-
Object
- Object
- RubyJsonApiClient::Store
- Defined in:
- lib/ruby_json_api_client/store.rb
Instance Attribute Summary collapse
-
#default_adapter ⇒ Object
Returns the value of attribute default_adapter.
-
#default_serializer ⇒ Object
Returns the value of attribute default_serializer.
Class Method Summary collapse
- .default(format) ⇒ Object
- .get_adapter(name) ⇒ Object
- .get_serializer(name) ⇒ Object
- .instance ⇒ Object
- .register_adapter(name, klass = nil, options = {}) ⇒ Object
- .register_serializer(name, klass = nil, options = {}) ⇒ Object
Instance Method Summary collapse
- #adapter_for_class(klass) ⇒ Object
- #delete(model) ⇒ Object
- #find(klass, id) ⇒ Object
- #find_many_relationship(parent, name, options) ⇒ Object
- #find_single_relationship(parent, name, options) ⇒ Object
-
#initialize(options) ⇒ Store
constructor
A new instance of Store.
- #load(klass, data) ⇒ Object
-
#load_collection(klass, url) ⇒ Object
TODO: make the 2 following functions a bit nicer.
-
#load_single(klass, id, url) ⇒ Object
TODO: make nicer.
- #merge(into, from) ⇒ Object
- #query(klass, params) ⇒ Object
- #reload(model) ⇒ Object
- #save(model) ⇒ Object
- #serializer_for_class(klass) ⇒ Object
Constructor Details
#initialize(options) ⇒ Store
Returns a new instance of Store.
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ruby_json_api_client/store.rb', line 58 def initialize() if [:format] [:adapter] = [:format] [:serializer] = [:format] end @default_adapter = self.class.get_adapter([:adapter]) @default_serializer = self.class.get_serializer([:serializer]) if @default_serializer && @default_serializer.respond_to?(:store=) @default_serializer.store = self end end |
Instance Attribute Details
#default_adapter ⇒ Object
Returns the value of attribute default_adapter.
55 56 57 |
# File 'lib/ruby_json_api_client/store.rb', line 55 def default_adapter @default_adapter end |
#default_serializer ⇒ Object
Returns the value of attribute default_serializer.
56 57 58 |
# File 'lib/ruby_json_api_client/store.rb', line 56 def default_serializer @default_serializer end |
Class Method Details
.default(format) ⇒ Object
47 48 49 |
# File 'lib/ruby_json_api_client/store.rb', line 47 def self.default(format) @store = new(format: format) end |
.get_adapter(name) ⇒ Object
40 41 42 43 44 45 |
# 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].) end end |
.get_serializer(name) ⇒ Object
33 34 35 36 37 38 |
# 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].) end end |
.instance ⇒ Object
51 52 53 |
# File 'lib/ruby_json_api_client/store.rb', line 51 def self.instance @store end |
.register_adapter(name, klass = nil, options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/ruby_json_api_client/store.rb', line 3 def self.register_adapter(name, klass = nil, = {}) @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 || class_name = name.to_s.camelize klass = "RubyJsonApiClient::#{class_name}Adapter".constantize = temp end @adapters[name] = OpenStruct.new({ klass: klass, options: }) end |
.register_serializer(name, klass = nil, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ruby_json_api_client/store.rb', line 18 def self.register_serializer(name, klass = nil, = {}) @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 || class_name = name.to_s.camelize klass = "RubyJsonApiClient::#{class_name}Serializer".constantize = temp end @serializers[name] = OpenStruct.new({ klass: klass, options: }) end |
Instance Method Details
#adapter_for_class(klass) ⇒ Object
72 73 74 |
# File 'lib/ruby_json_api_client/store.rb', line 72 def adapter_for_class(klass) @default_adapter end |
#delete(model) ⇒ Object
200 201 202 203 204 205 206 207 208 209 |
# 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) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# 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) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ruby_json_api_client/store.rb', line 108 def find_many_relationship(parent, name, ) # 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, , 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) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ruby_json_api_client/store.rb', line 129 def find_single_relationship(parent, name, ) # needs to use serializer_for_class serializer = @default_serializer reload(parent) if parent.__origin__.nil? response = parent.__origin__ serializer.extract_single_relationship(parent, name, , response).tap do |model| model.__origin__ = response if model && model.__origin__.nil? end end |
#load(klass, data) ⇒ Object
163 164 165 |
# File 'lib/ruby_json_api_client/store.rb', line 163 def load(klass, data) end |
#load_collection(klass, url) ⇒ Object
TODO: make the 2 following functions a bit nicer
143 144 145 146 147 148 149 150 151 |
# 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) ⇒ Object
TODO: make nicer
154 155 156 157 158 159 160 161 |
# 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) ⇒ Object
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/ruby_json_api_client/store.rb', line 211 def merge(into, from) into.__origin__ = from.__origin__ into. = from. 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# 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) ⇒ Object
167 168 169 170 |
# 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) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# 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) ⇒ Object
76 77 78 |
# File 'lib/ruby_json_api_client/store.rb', line 76 def serializer_for_class(klass) @default_serializer end |