simple_ams 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ module SimpleAMS
7
7
  attr_reader :resource, :allowed_options, :injected_options
8
8
 
9
9
  #injected_options is always a Hash object
10
- def initialize(resource, injected_options: {}, allowed_options: nil)
10
+ def initialize(resource = nil, injected_options: {}, allowed_options: nil)
11
11
  @resource = resource
12
12
  @injected_options = injected_options || {}
13
13
  @_internal = @injected_options[:_internal] || {}
@@ -15,6 +15,13 @@ module SimpleAMS
15
15
  end
16
16
  alias collection resource
17
17
 
18
+ #performance enchancement method for non-polymorphic collections
19
+ def with_resource(resource)
20
+ @resource = resource
21
+
22
+ return self
23
+ end
24
+
18
25
  def relation_options_for(relation_name)
19
26
  return _relation_options[relation_name] || {}
20
27
  end
@@ -22,22 +29,15 @@ module SimpleAMS
22
29
  def primary_id
23
30
  return @primary_id if defined?(@primary_id)
24
31
 
25
- _options = injected_options.fetch(:primary_id, nil)
26
- _options = allowed_options.fetch(:primary_id, nil) unless _options
27
-
28
- return @primary_id ||= PrimaryId.new(*_options)
32
+ return @primary_id = array_of_value_hash_for(PrimaryId, :primary_id)
29
33
  end
30
34
 
31
35
  def type
32
36
  return @type if defined?(@type)
33
37
 
34
- _options = injected_options.fetch(:type, nil)
35
- _options = allowed_options.fetch(:type, nil) unless _options
36
-
37
- return @type ||= Type.new(*_options)
38
+ return @type = array_of_value_hash_for(Type, :type)
38
39
  end
39
40
 
40
- #that's handful
41
41
  def name
42
42
  @name ||= injected_options[:name] || allowed_options[:name] || type.name
43
43
  end
@@ -46,86 +46,49 @@ module SimpleAMS
46
46
  def fields
47
47
  return @fields if defined?(@fields)
48
48
 
49
- injected = injected_options.fetch(:fields, nil)
50
-
51
- if injected.nil?
52
- return @fields = Fields.new(allowed_options.fetch(:fields).uniq)
53
- else
54
- return @fields = Fields.new(options_for(
55
- injected: Fields.new(injected_options.fetch(:fields, nil)),
56
- allowed: Fields.new(allowed_options.fetch(:fields).uniq)
57
- ).uniq)
58
- end
49
+ return @fields = array_of_items_for(Fields, :fields)
59
50
  end
60
51
 
61
52
  def includes
62
53
  return @includes if defined?(@includes)
63
54
 
64
- injected = injected_options.fetch(:includes, nil)
65
-
66
- if injected.nil?
67
- return @includes = Includes.new(allowed_options.fetch(:includes).uniq)
68
- else
69
- return @includes = Includes.new(options_for(
70
- injected: Includes.new(injected_options.fetch(:includes, nil)),
71
- allowed: Includes.new(allowed_options.fetch(:includes).uniq)
72
- ).uniq)
73
- end
55
+ return @includes = array_of_items_for(Includes, :includes)
74
56
  end
75
57
 
76
58
  #TODO: correctly loop over injected relations, although should be a rarely used feature
77
59
  def relations
78
- return @relations if defined?(@relations) #||= options_for(
60
+ return @relations if defined?(@relations)
79
61
 
80
62
  relations = injected_options.fetch(:relations, nil)
81
63
  relations = allowed_options.fetch(:relations, []) if relations.nil?
82
64
 
83
- return @relations = relations.map{|rel| Relation.new(*rel)}.select{
84
- |relation| includes.include?(relation.name)
85
- }
65
+ return @relations = Relations.new(relations, includes)
86
66
  end
87
67
 
88
- #TODO: add method-based links, should boost performance
89
68
  def links
90
69
  return @links if defined?(@links)
91
70
 
92
- injected = injected_options.fetch(:links, nil)
93
- if injected
94
- injected = Links.new(
95
- injected.map{|l| Links::Link.new(*l.flatten, resource: resource)}
96
- )
97
- end
98
-
99
- allowed = Links.new(
100
- allowed_options.fetch(:links).map{|l| Links::Link.new(*l, resource: resource)}
101
- )
102
-
103
- return @links = Links.new(options_for(
104
- #TODO: correctly loop over injected properties
105
- injected: injected,
106
- allowed: allowed,
107
- ).uniq{|link| link.name})
71
+ return @links = array_of_name_value_hash_for(Links, Links::Link, :links)
108
72
  end
109
73
 
110
- #TODO: add method-based metas, should boost performance
111
74
  def metas
112
75
  return @metas if defined?(@metas)
113
76
 
114
- injected = injected_options.fetch(:metas, nil)
115
- if injected
116
- injected = Metas.new(
117
- injected.map{|l| Metas::Meta.new(*l.flatten, resource: resource)}
118
- )
119
- end
120
- allowed = Metas.new(
121
- allowed_options.fetch(:metas).map{|l| Metas::Meta.new(*l, resource: resource)}
122
- )
77
+ return @metas = array_of_name_value_hash_for(Metas, Metas::Meta, :metas)
78
+ end
123
79
 
124
- return @metas = Metas.new(options_for(
125
- #TODO: correctly loop over injected properties
126
- injected: injected,
127
- allowed: allowed,
128
- ).uniq{|meta| meta.name})
80
+ def forms
81
+ return @forms if defined?(@forms)
82
+
83
+ return @forms = array_of_name_value_hash_for(Forms, Forms::Form, :forms)
84
+ end
85
+
86
+ def generics
87
+ return @generics if defined?(@generics)
88
+
89
+ return @generics = array_of_name_value_hash_for(
90
+ Generics, Generics::Option, :generics
91
+ )
129
92
  end
130
93
 
131
94
  #TODO: handle case of proc
@@ -134,21 +97,26 @@ module SimpleAMS
134
97
 
135
98
  _serializer = injected_options.fetch(:serializer, serializer_class)
136
99
 
137
- return @serializer = _serializer.new.extend(
138
- SimpleAMS::Methy.of(
139
- expose.merge({
140
- object: resource
141
- })
142
- )
143
- )
100
+ return @serializer = instantiated_serializer_for(_serializer)
144
101
  end
145
102
 
146
- def adapter
147
- return @adapter if defined?(@adapter)
103
+ def adapter(_serializer: nil)
104
+ return @adapter if defined?(@adapter) && _serializer.nil?
105
+ serializer = _serializer || serializer
148
106
 
149
- @adapter = Adapter.new(*injected_options.fetch(:adapter, [nil]))
150
- @adapter = Adapter.new(*allowed_options.fetch(:adapter, [nil])) if @adapter.value.nil?
151
- @adapter = Adapter.new(SimpleAMS::Adapters::AMS) if @adapter.value.nil?
107
+ @adapter = Adapter.new(*injected_options.fetch(:adapter, [nil]), {
108
+ resource: resource, serializer: serializer
109
+ })
110
+ if @adapter.value.nil?
111
+ @adapter = Adapter.new(*allowed_options.fetch(:adapter, [nil]), {
112
+ resource: resource, serializer: serializer
113
+ })
114
+ end
115
+ if @adapter.value.nil?
116
+ @adapter = Adapter.new(SimpleAMS::Adapters::AMS, {
117
+ resource: resource, serializer: serializer
118
+ })
119
+ end
152
120
 
153
121
  return @adapter
154
122
  end
@@ -180,7 +148,8 @@ module SimpleAMS
180
148
 
181
149
  #TODO: Do we need that merge ?
182
150
  _injected_options = @injected_options.fetch(:collection, {}).merge({
183
- serializer: collection_serializer_class
151
+ serializer: collection_serializer_class,
152
+ adapter: adapter(_serializer: collection_serializer_class).raw
184
153
  })
185
154
  _allowed_options = @allowed_options.fetch(:collection).options
186
155
 
@@ -219,13 +188,54 @@ module SimpleAMS
219
188
 
220
189
  private
221
190
  attr_reader :_internal
222
- =begin
223
- def is_collection?
224
- _internal[:is_collection] == true
191
+
192
+ #TODO: add method-based links, should boost performance
193
+ def array_of_name_value_hash_for(collection_klass, item_klass, name)
194
+ injected = injected_options.fetch(name, nil)
195
+ if injected
196
+ injected = collection_klass.new(
197
+ injected.map{|l| item_klass.new(*l.flatten, {
198
+ resource: resource, serializer: serializer
199
+ })}
200
+ )
201
+ end
202
+
203
+ allowed = collection_klass.new(
204
+ allowed_options.fetch(name).map{|l| item_klass.new(*l, {
205
+ resource: resource, serializer: serializer
206
+ })}
207
+ )
208
+
209
+ return collection_klass.new(priority_options_for(
210
+ #TODO: correctly loop over injected properties
211
+ injected: injected,
212
+ allowed: allowed,
213
+ ).uniq{|item| item.name})
225
214
  end
226
- =end
227
215
 
228
- def options_for(allowed:, injected:)
216
+ def array_of_value_hash_for(klass, name)
217
+ _options = injected_options.fetch(name, nil)
218
+ _options = allowed_options.fetch(name, nil) unless _options
219
+
220
+ return klass.new(*_options, {
221
+ resource: resource, serializer: serializer
222
+ })
223
+ end
224
+
225
+ def array_of_items_for(klass, name)
226
+ injected = injected_options.fetch(name, nil)
227
+
228
+ if injected.nil?
229
+ return klass.new(allowed_options.fetch(name).uniq)
230
+ else
231
+ return klass.new(priority_options_for(
232
+ injected: klass.new(injected_options.fetch(name, nil)),
233
+ allowed: klass.new(allowed_options.fetch(name).uniq)
234
+ ).uniq)
235
+ end
236
+ end
237
+
238
+ def priority_options_for(allowed:, injected:)
229
239
  if not injected.nil?
230
240
  allowed = injected.class.new(
231
241
  injected.map{|s| s.is_a?(Hash) ? s.keys.first : s}
@@ -235,6 +245,12 @@ module SimpleAMS
235
245
  return allowed
236
246
  end
237
247
 
248
+ =begin
249
+ def options_for(allowed:, injected:)
250
+ (allowed || []).concat(injected || [])
251
+ end
252
+ =end
253
+
238
254
  def _relation_options
239
255
  return @_relation_options if defined?(@_relation_options)
240
256
 
@@ -255,7 +271,7 @@ module SimpleAMS
255
271
  field.is_a?(Hash)
256
272
  }.find{|field_hash|
257
273
  field_hash.keys.first.to_s == relation.name.to_s
258
- }
274
+ }
259
275
 
260
276
  #.. while here just nil will work (pick default fields from serializer)
261
277
  fields_value = fields_value[relation.name] if fields_value
@@ -275,9 +291,21 @@ module SimpleAMS
275
291
  _serializer_class = self.collection_serializer_class
276
292
  end
277
293
 
278
- _allowed_options = _serializer_class&.options
294
+ if _serializer_class.respond_to?(:simple_ams?)
295
+ return _serializer_class&.options
296
+ else
297
+ raise "#{_serializer_class} does not respond to SimpleAMS methods, did you include the DSL module?"
298
+ end
299
+ end
279
300
 
280
- return _allowed_options
301
+ def instantiated_serializer_for(serializer_klass)
302
+ serializer_klass.new.extend(
303
+ SimpleAMS::Methy.of(
304
+ expose.merge({
305
+ object: resource
306
+ })
307
+ )
308
+ )
281
309
  end
282
310
 
283
311
  def infer_serializer
@@ -1,3 +1,3 @@
1
1
  module SimpleAMS
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/simple_ams.rb CHANGED
@@ -5,6 +5,7 @@ require "simple_ams/document"
5
5
  require "simple_ams/dsl"
6
6
  require "simple_ams/adapters"
7
7
  require "simple_ams/adapters/ams"
8
+ require "simple_ams/adapters/jsonapi"
8
9
  require "simple_ams/renderer"
9
10
 
10
11
  require "simple_ams/options"
@@ -16,14 +17,19 @@ require "simple_ams/options/fields"
16
17
  require "simple_ams/options/includes"
17
18
  require "simple_ams/options/links"
18
19
  require "simple_ams/options/metas"
20
+ require "simple_ams/options/forms"
21
+ require "simple_ams/options/generics"
19
22
  require "simple_ams/options/primary_id"
20
23
  require "simple_ams/options/type"
21
- require "simple_ams/options/relation"
24
+ require "simple_ams/options/relations"
22
25
 
26
+ require "simple_ams/document/primary_id"
23
27
  require "simple_ams/document/fields"
24
28
  require "simple_ams/document/relations"
25
29
  require "simple_ams/document/links"
26
30
  require "simple_ams/document/metas"
31
+ require "simple_ams/document/forms"
32
+ require "simple_ams/document/generics"
27
33
 
28
34
  module SimpleAMS
29
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_ams
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Filippos Vasilakis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-26 00:00:00.000000000 Z
11
+ date: 2018-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -112,10 +112,14 @@ files:
112
112
  - lib/simple_ams.rb
113
113
  - lib/simple_ams/adapters.rb
114
114
  - lib/simple_ams/adapters/ams.rb
115
+ - lib/simple_ams/adapters/jsonapi.rb
115
116
  - lib/simple_ams/document.rb
116
117
  - lib/simple_ams/document/fields.rb
118
+ - lib/simple_ams/document/forms.rb
119
+ - lib/simple_ams/document/generics.rb
117
120
  - lib/simple_ams/document/links.rb
118
121
  - lib/simple_ams/document/metas.rb
122
+ - lib/simple_ams/document/primary_id.rb
119
123
  - lib/simple_ams/document/relations.rb
120
124
  - lib/simple_ams/dsl.rb
121
125
  - lib/simple_ams/methy.rb
@@ -125,11 +129,13 @@ files:
125
129
  - lib/simple_ams/options/concerns/name_value_hash.rb
126
130
  - lib/simple_ams/options/concerns/value_hash.rb
127
131
  - lib/simple_ams/options/fields.rb
132
+ - lib/simple_ams/options/forms.rb
133
+ - lib/simple_ams/options/generics.rb
128
134
  - lib/simple_ams/options/includes.rb
129
135
  - lib/simple_ams/options/links.rb
130
136
  - lib/simple_ams/options/metas.rb
131
137
  - lib/simple_ams/options/primary_id.rb
132
- - lib/simple_ams/options/relation.rb
138
+ - lib/simple_ams/options/relations.rb
133
139
  - lib/simple_ams/options/type.rb
134
140
  - lib/simple_ams/renderer.rb
135
141
  - lib/simple_ams/version.rb
@@ -1,31 +0,0 @@
1
- require "simple_ams"
2
-
3
- class SimpleAMS::Options
4
- class Relation
5
- attr_reader :type, :name, :options
6
- def initialize(type, name, options = {})
7
- @type = type.to_sym
8
- @name = name.is_a?(String) ? name.to_sym : name
9
- @options = options
10
-
11
- @many = type == :has_many ? true : false
12
- end
13
-
14
- alias relation name
15
-
16
- def raw
17
- [type, name, options]
18
- end
19
-
20
- def collection?
21
- @many
22
- end
23
-
24
- def single?
25
- !array
26
- end
27
-
28
- private
29
- attr_writer :type, :name, :options
30
- end
31
- end