simple_ams 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +1 -1
- data/lib/simple_ams/adapters/jsonapi.rb +24 -5
- data/lib/simple_ams/document/forms.rb +2 -2
- data/lib/simple_ams/document/generics.rb +39 -2
- data/lib/simple_ams/document/links.rb +2 -40
- data/lib/simple_ams/document/metas.rb +2 -2
- data/lib/simple_ams/document/relations.rb +2 -2
- data/lib/simple_ams/document.rb +10 -6
- data/lib/simple_ams/dsl.rb +8 -7
- data/lib/simple_ams/options/adapter.rb +1 -1
- data/lib/simple_ams/options/concerns/filterable.rb +4 -4
- data/lib/simple_ams/options/concerns/name_value_hash.rb +8 -3
- data/lib/simple_ams/options/concerns/tracked_properties.rb +25 -0
- data/lib/simple_ams/options/concerns/value_hash.rb +9 -5
- data/lib/simple_ams/options/forms.rb +2 -2
- data/lib/simple_ams/options/generics.rb +4 -0
- data/lib/simple_ams/options/links.rb +2 -2
- data/lib/simple_ams/options/metas.rb +2 -2
- data/lib/simple_ams/options.rb +29 -24
- data/lib/simple_ams/version.rb +1 -1
- data/lib/simple_ams.rb +24 -3
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9b09157cf3099fe306077aeac488a0f2d4a9674f554d98b7ad1e73772b2beffb
|
4
|
+
data.tar.gz: 5134001b312f8602fb2bee3712f81ba9e859e247cacef59d1e4abeb8e15d66de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 993d7b6f1ad749b2b992885e4f13d606f237895df3858261431c1b3369f281782e6d26e4cc690dd4af032dd54a9f903e006a7c10f12b3d04496fdfd5beefce97
|
7
|
+
data.tar.gz: 8abc4dab396f0d31955c9a19290525cf50b46c583feefc812833fd2d1f946461c99f7b41ef47fe907e7d2ea11d44dd78e057040628a7a7ca2d97d59b09d02b45
|
data/README.md
CHANGED
@@ -44,7 +44,7 @@ class UserSerializer
|
|
44
44
|
has_one :profile, serializer: ProfileSerializer
|
45
45
|
#belongs_to is just an alias to has_one
|
46
46
|
belongs_to :organization, serializer: OrganizationSerializer
|
47
|
-
has_many :videos, serializer: VideosSerializer
|
47
|
+
has_many :videos, serializer: VideosSerializer do
|
48
48
|
#rarely used: if you need more options, you can pas a block
|
49
49
|
#which adheres to the same DSL as described here
|
50
50
|
#it goes to an option called `embedded`
|
@@ -2,7 +2,8 @@ require "simple_ams"
|
|
2
2
|
|
3
3
|
class SimpleAMS::Adapters::JSONAPI
|
4
4
|
DEFAULT_OPTIONS = {
|
5
|
-
skip_id_in_attributes:
|
5
|
+
skip_id_in_attributes: false,
|
6
|
+
key_transform: nil
|
6
7
|
}
|
7
8
|
|
8
9
|
attr_reader :document, :options
|
@@ -45,7 +46,16 @@ class SimpleAMS::Adapters::JSONAPI
|
|
45
46
|
end
|
46
47
|
|
47
48
|
def transform_key(key)
|
48
|
-
|
49
|
+
case options[:key_transform]
|
50
|
+
#when :camel
|
51
|
+
#when :camel_lower
|
52
|
+
when :dash
|
53
|
+
key.to_s.gsub('_','-')
|
54
|
+
when :underscore
|
55
|
+
key.to_s.gsub('-', '_')
|
56
|
+
else
|
57
|
+
key
|
58
|
+
end
|
49
59
|
end
|
50
60
|
|
51
61
|
def relationships
|
@@ -53,6 +63,7 @@ class SimpleAMS::Adapters::JSONAPI
|
|
53
63
|
|
54
64
|
@relationships ||= document.relations.inject({}){ |hash, relation|
|
55
65
|
_hash = {}
|
66
|
+
|
56
67
|
embedded_relation_data = embedded_relation_data_for(relation)
|
57
68
|
unless embedded_relation_data.empty?
|
58
69
|
_hash.merge!(data: embedded_relation_data_for(relation))
|
@@ -124,9 +135,9 @@ class SimpleAMS::Adapters::JSONAPI
|
|
124
135
|
|
125
136
|
@included ||= document.relations.available.inject([]){ |array, relation|
|
126
137
|
if relation.folder?
|
127
|
-
array << relation.map{|doc| self.class.new(doc).as_json[:data]}
|
138
|
+
array << relation.map{|doc| self.class.new(doc, options).as_json[:data]}
|
128
139
|
else
|
129
|
-
array << self.class.new(relation).as_json[:data]
|
140
|
+
array << self.class.new(relation, options).as_json[:data]
|
130
141
|
end
|
131
142
|
|
132
143
|
array
|
@@ -148,16 +159,24 @@ class SimpleAMS::Adapters::JSONAPI
|
|
148
159
|
}
|
149
160
|
hash.merge!(meta: metas) unless metas.empty?
|
150
161
|
hash.merge!(links: links) unless links.empty?
|
162
|
+
hash.merge!(included: included) unless included.empty?
|
151
163
|
|
152
164
|
return hash
|
153
165
|
end
|
154
166
|
|
155
167
|
def documents
|
168
|
+
@included = []
|
156
169
|
return folder.map{|document|
|
157
|
-
adapter.new(document).as_json
|
170
|
+
_doc = adapter.new(document, options).as_json
|
171
|
+
@included << _doc[:included]
|
172
|
+
_doc[:data]
|
158
173
|
} || []
|
159
174
|
end
|
160
175
|
|
176
|
+
def included
|
177
|
+
(@included || []).flatten
|
178
|
+
end
|
179
|
+
|
161
180
|
def metas
|
162
181
|
@metas ||= folder.metas.inject({}){ |hash, meta|
|
163
182
|
hash[transform_key(meta.name)] = meta.value
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require "simple_ams"
|
2
2
|
|
3
3
|
module SimpleAMS
|
4
|
-
class Document::Forms < Document::
|
4
|
+
class Document::Forms < Document::Generics
|
5
5
|
def initialize(options)
|
6
6
|
@options = options
|
7
7
|
@members = options.forms
|
8
8
|
end
|
9
9
|
|
10
|
-
class Form < Document::
|
10
|
+
class Form < Document::Generics::Generic
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -1,14 +1,51 @@
|
|
1
1
|
require "simple_ams"
|
2
2
|
|
3
3
|
module SimpleAMS
|
4
|
-
class Document::Generics
|
4
|
+
class Document::Generics
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
Generic = Struct.new(:name, :value, :options)
|
8
|
+
|
5
9
|
def initialize(options)
|
6
10
|
@options = options
|
7
11
|
@members = options.generics
|
8
12
|
end
|
9
13
|
|
10
|
-
|
14
|
+
def [](key)
|
15
|
+
found = members.find{|generic| generic.name == key}
|
16
|
+
return nil unless found
|
17
|
+
|
18
|
+
return with_decorator(found)
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(&block)
|
22
|
+
return enum_for(:each) unless block_given?
|
23
|
+
|
24
|
+
members.each{ |member|
|
25
|
+
yield with_decorator(member)
|
26
|
+
}
|
27
|
+
|
28
|
+
self
|
11
29
|
end
|
30
|
+
|
31
|
+
def any?
|
32
|
+
members.any?
|
33
|
+
end
|
34
|
+
|
35
|
+
def empty?
|
36
|
+
members.empty?
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
attr_reader :members, :options
|
41
|
+
|
42
|
+
def with_decorator(generic)
|
43
|
+
Generic.new(
|
44
|
+
generic.name,
|
45
|
+
generic.respond_to?(:call) ? generic.value.call : generic.value,
|
46
|
+
generic.options
|
47
|
+
)
|
48
|
+
end
|
12
49
|
end
|
13
50
|
end
|
14
51
|
|
@@ -1,51 +1,13 @@
|
|
1
1
|
require "simple_ams"
|
2
2
|
|
3
3
|
module SimpleAMS
|
4
|
-
class Document::Links
|
5
|
-
include Enumerable
|
6
|
-
|
7
|
-
Link = Struct.new(:name, :value, :options)
|
8
|
-
|
4
|
+
class Document::Links < Document::Generics
|
9
5
|
def initialize(options)
|
10
6
|
@options = options
|
11
7
|
@members = options.links
|
12
8
|
end
|
13
9
|
|
14
|
-
|
15
|
-
found = members.find{|link| link.name == key}
|
16
|
-
return nil unless found
|
17
|
-
|
18
|
-
return with_decorator(found)
|
19
|
-
end
|
20
|
-
|
21
|
-
def each(&block)
|
22
|
-
return enum_for(:each) unless block_given?
|
23
|
-
|
24
|
-
members.each{ |member|
|
25
|
-
yield with_decorator(member)
|
26
|
-
}
|
27
|
-
|
28
|
-
self
|
29
|
-
end
|
30
|
-
|
31
|
-
def any?
|
32
|
-
members.any?
|
33
|
-
end
|
34
|
-
|
35
|
-
def empty?
|
36
|
-
members.empty?
|
10
|
+
class Generic < Document::Generics::Generic
|
37
11
|
end
|
38
|
-
|
39
|
-
private
|
40
|
-
attr_reader :members, :options
|
41
|
-
|
42
|
-
def with_decorator(link)
|
43
|
-
Link.new(
|
44
|
-
link.name,
|
45
|
-
link.respond_to?(:call) ? link.value.call : link.value,
|
46
|
-
link.options
|
47
|
-
)
|
48
|
-
end
|
49
12
|
end
|
50
13
|
end
|
51
|
-
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require "simple_ams"
|
2
2
|
|
3
3
|
module SimpleAMS
|
4
|
-
class Document::Metas < Document::
|
4
|
+
class Document::Metas < Document::Generics
|
5
5
|
def initialize(options)
|
6
6
|
@options = options
|
7
7
|
@members = options.metas
|
8
8
|
end
|
9
9
|
|
10
|
-
class Meta < Document::
|
10
|
+
class Meta < Document::Generics::Generic
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
@@ -30,7 +30,7 @@ module SimpleAMS
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def empty?
|
33
|
-
|
33
|
+
relations.length == 0
|
34
34
|
end
|
35
35
|
|
36
36
|
def available
|
@@ -83,7 +83,7 @@ module SimpleAMS
|
|
83
83
|
)
|
84
84
|
).merge(
|
85
85
|
_internal: {
|
86
|
-
module: serializer.class.to_s.rpartition('::')
|
86
|
+
module: serializer.class.to_s.rpartition('::')[0]
|
87
87
|
}
|
88
88
|
)
|
89
89
|
}
|
data/lib/simple_ams/document.rb
CHANGED
@@ -28,15 +28,15 @@ class SimpleAMS::Document
|
|
28
28
|
end
|
29
29
|
|
30
30
|
def name
|
31
|
-
options.name
|
31
|
+
@name ||= options.name
|
32
32
|
end
|
33
33
|
|
34
34
|
def type
|
35
|
-
options.type
|
35
|
+
@type ||= options.type
|
36
36
|
end
|
37
37
|
|
38
38
|
def adapter
|
39
|
-
options.adapter
|
39
|
+
@adapter ||= options.adapter
|
40
40
|
end
|
41
41
|
|
42
42
|
def links
|
@@ -64,7 +64,7 @@ class SimpleAMS::Document
|
|
64
64
|
end
|
65
65
|
|
66
66
|
def folder?
|
67
|
-
self.is_a?(self.class::Folder)
|
67
|
+
@is_folder ||= self.is_a?(self.class::Folder)
|
68
68
|
end
|
69
69
|
|
70
70
|
def document?
|
@@ -80,6 +80,7 @@ class SimpleAMS::Document
|
|
80
80
|
class Folder < self
|
81
81
|
include Enumerable
|
82
82
|
attr_reader :members
|
83
|
+
alias collection resource
|
83
84
|
|
84
85
|
def initialize(options, embedded_options = nil)
|
85
86
|
@_options = options
|
@@ -87,6 +88,7 @@ class SimpleAMS::Document
|
|
87
88
|
@options = @_options.collection_options
|
88
89
|
|
89
90
|
@members = options.collection
|
91
|
+
@resource = options.resource
|
90
92
|
end
|
91
93
|
|
92
94
|
def each(&block)
|
@@ -102,6 +104,7 @@ class SimpleAMS::Document
|
|
102
104
|
#do we really need this method ?
|
103
105
|
def documents
|
104
106
|
@members.map do |resource|
|
107
|
+
#need optimization here!
|
105
108
|
SimpleAMS::Document.new(options_for(resource))
|
106
109
|
end
|
107
110
|
end
|
@@ -122,8 +125,8 @@ class SimpleAMS::Document
|
|
122
125
|
allowed_options: serializer_for(resource).options
|
123
126
|
})
|
124
127
|
else
|
125
|
-
|
126
|
-
|
128
|
+
resource_options.with_resource(resource)
|
129
|
+
=begin
|
127
130
|
# we need to optimize that using tracked properties
|
128
131
|
SimpleAMS::Options.new(resource, {
|
129
132
|
injected_options: resource_options.injected_options.merge({
|
@@ -131,6 +134,7 @@ class SimpleAMS::Document
|
|
131
134
|
}),
|
132
135
|
allowed_options: serializer_for(resource).options
|
133
136
|
})
|
137
|
+
=end
|
134
138
|
end
|
135
139
|
end
|
136
140
|
|
data/lib/simple_ams/dsl.rb
CHANGED
@@ -33,7 +33,7 @@ module SimpleAMS::DSL
|
|
33
33
|
options.merge(
|
34
34
|
#TODO: maybe add another group of elements under dsl?
|
35
35
|
#this could be DSL::Type.new(type).explicit?
|
36
|
-
type
|
36
|
+
type[-1][:_explicit] ? {} : {type: nil}
|
37
37
|
)
|
38
38
|
)
|
39
39
|
|
@@ -59,7 +59,7 @@ module SimpleAMS::DSL
|
|
59
59
|
self::Collection_.options.merge(
|
60
60
|
#TODO: maybe add another group of elements under dsl?
|
61
61
|
#this could be DSL::Type.new(type).explicit?
|
62
|
-
type
|
62
|
+
type[-1][:_explicit] ? {} : {type: nil}
|
63
63
|
)
|
64
64
|
)
|
65
65
|
subclass.const_set('Collection_', _klass)
|
@@ -80,11 +80,11 @@ module SimpleAMS::DSL
|
|
80
80
|
'Serializer',''
|
81
81
|
).gsub(
|
82
82
|
'::Collection_', ''
|
83
|
-
).downcase.split('::')
|
83
|
+
).downcase.split('::')[-1]
|
84
84
|
|
85
85
|
return "#{_name}_collection".to_sym
|
86
86
|
else
|
87
|
-
return self.to_s.gsub('Serializer','').downcase.split('::').
|
87
|
+
return self.to_s.gsub('Serializer','').downcase.split('::')[-1].to_sym
|
88
88
|
end
|
89
89
|
end
|
90
90
|
def with_options(options = {})
|
@@ -101,14 +101,15 @@ module SimpleAMS::DSL
|
|
101
101
|
collection{}.with_options(value)
|
102
102
|
end
|
103
103
|
elsif meths.include?(key)
|
104
|
-
if (value.is_a?(Array) && value.
|
104
|
+
if (value.is_a?(Array) && value[0].is_a?(Array)) || value.is_a?(Hash)
|
105
105
|
self.send(key, value)
|
106
106
|
else
|
107
107
|
self.send(key, *value)
|
108
108
|
end
|
109
109
|
else
|
110
|
-
|
111
|
-
|
110
|
+
SimpleAMS.configuration.logger.info(
|
111
|
+
"SimpeAMS: #{key} is not recognized, ignoring (from #{self.to_s})"
|
112
|
+
)
|
112
113
|
end
|
113
114
|
end
|
114
115
|
|
@@ -7,7 +7,7 @@ class SimpleAMS::Options
|
|
7
7
|
#for optimizing performance, ask only the first element
|
8
8
|
#other idea is to create another module just for (Name)ValueHash objects
|
9
9
|
def &(other_filterables)
|
10
|
-
other_is_object = other_filterables.
|
10
|
+
other_is_object = other_filterables[0].respond_to?(:name)
|
11
11
|
|
12
12
|
return self.class.new(
|
13
13
|
self.select{|m|
|
@@ -23,18 +23,18 @@ class SimpleAMS::Options
|
|
23
23
|
#for optimizing performance, ask only the first element of self and save it as state
|
24
24
|
def include?(member)
|
25
25
|
unless defined?(@self_is_object)
|
26
|
-
@self_is_object = self.
|
26
|
+
@self_is_object = self[0].respond_to?(:name)
|
27
27
|
end
|
28
28
|
|
29
29
|
if @self_is_object
|
30
|
-
self.
|
30
|
+
self.any?{|s| s.name == member}
|
31
31
|
else
|
32
32
|
super
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
def raw
|
37
|
-
if self.
|
37
|
+
if self[0].respond_to?(:raw)
|
38
38
|
self.map(&:raw)
|
39
39
|
else
|
40
40
|
self.map{|i| i}
|
@@ -7,11 +7,12 @@ class SimpleAMS::Options
|
|
7
7
|
|
8
8
|
def initialize(name, value, options = {}, resource:, serializer:)
|
9
9
|
@name = name.is_a?(String) ? name.to_sym : name
|
10
|
-
if value.
|
10
|
+
if value.respond_to?(:call)
|
11
|
+
@volatile = true
|
11
12
|
_value = value.call(resource, serializer)
|
12
13
|
if _value.is_a?(Array) && _value.length > 1
|
13
|
-
@value = _value
|
14
|
-
@options = (_value
|
14
|
+
@value = _value[0]
|
15
|
+
@options = (_value[-1] || {}).merge(options || {})
|
15
16
|
else
|
16
17
|
@value = _value
|
17
18
|
@options = options || {}
|
@@ -26,6 +27,10 @@ class SimpleAMS::Options
|
|
26
27
|
[name, value, options]
|
27
28
|
end
|
28
29
|
|
30
|
+
def volatile?
|
31
|
+
return @volatile || false
|
32
|
+
end
|
33
|
+
|
29
34
|
private
|
30
35
|
attr_writer :name, :value, :options
|
31
36
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class SimpleAMS::Options
|
2
|
+
module Concerns
|
3
|
+
module TrackedProperties
|
4
|
+
Tracked = Struct.new(:value) do
|
5
|
+
def volatile?
|
6
|
+
return @volatile if defined?(@volatile)
|
7
|
+
return @volatile ||= self.value.volatile?
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize_tracking!
|
12
|
+
@tracked_properties = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def clean_volatile_properties!
|
16
|
+
@tracked_properties = @tracked_properties.select{|k, v| !v.volatile?}
|
17
|
+
end
|
18
|
+
|
19
|
+
def tracked(meth)
|
20
|
+
@tracked_properties[meth] ||= Tracked.new
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -4,13 +4,15 @@ class SimpleAMS::Options
|
|
4
4
|
module Concerns
|
5
5
|
module ValueHash
|
6
6
|
attr_reader :value, :options
|
7
|
+
alias :name :value
|
7
8
|
|
8
9
|
def initialize(value, options = {}, resource:, serializer:)
|
9
|
-
if value.
|
10
|
+
if value.respond_to?(:call)
|
11
|
+
@volatile = true
|
10
12
|
_value = value.call(resource, serializer)
|
11
13
|
if _value.is_a?(Array) && _value.length > 1
|
12
|
-
@value = _value
|
13
|
-
@options = (_value
|
14
|
+
@value = _value[0]
|
15
|
+
@options = (_value[-1] || {}).merge(options || {})
|
14
16
|
else
|
15
17
|
@value = _value
|
16
18
|
@options = options || {}
|
@@ -21,12 +23,14 @@ class SimpleAMS::Options
|
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
alias_method :name, :value
|
25
|
-
|
26
26
|
def raw
|
27
27
|
[value, options]
|
28
28
|
end
|
29
29
|
|
30
|
+
def volatile?
|
31
|
+
return @volatile || false
|
32
|
+
end
|
33
|
+
|
30
34
|
private
|
31
35
|
attr_writer :value, :options
|
32
36
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require "simple_ams"
|
2
2
|
|
3
3
|
class SimpleAMS::Options
|
4
|
-
class Forms <
|
4
|
+
class Forms < Generics
|
5
5
|
include SimpleAMS::Options::Concerns::Filterable
|
6
6
|
|
7
|
-
class Form
|
7
|
+
class Form < Generics::Option
|
8
8
|
include SimpleAMS::Options::Concerns::NameValueHash
|
9
9
|
end
|
10
10
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require "simple_ams"
|
2
2
|
|
3
3
|
class SimpleAMS::Options
|
4
|
-
class Links <
|
4
|
+
class Links < Generics
|
5
5
|
include SimpleAMS::Options::Concerns::Filterable
|
6
6
|
|
7
|
-
class Link
|
7
|
+
class Link < Generics::Option
|
8
8
|
include SimpleAMS::Options::Concerns::NameValueHash
|
9
9
|
end
|
10
10
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require "simple_ams"
|
2
2
|
|
3
3
|
class SimpleAMS::Options
|
4
|
-
class Metas <
|
4
|
+
class Metas < Generics
|
5
5
|
include SimpleAMS::Options::Concerns::Filterable
|
6
6
|
|
7
|
-
class Meta
|
7
|
+
class Meta < Generics::Option
|
8
8
|
include SimpleAMS::Options::Concerns::NameValueHash
|
9
9
|
end
|
10
10
|
end
|
data/lib/simple_ams/options.rb
CHANGED
@@ -2,12 +2,15 @@ require "simple_ams"
|
|
2
2
|
|
3
3
|
module SimpleAMS
|
4
4
|
class Options
|
5
|
+
include Concerns::TrackedProperties
|
6
|
+
|
5
7
|
class Collection < self; end
|
6
8
|
|
7
9
|
attr_reader :resource, :allowed_options, :injected_options
|
8
10
|
|
9
11
|
#injected_options is always a Hash object
|
10
12
|
def initialize(resource = nil, injected_options: {}, allowed_options: nil)
|
13
|
+
initialize_tracking!
|
11
14
|
@resource = resource
|
12
15
|
@injected_options = injected_options || {}
|
13
16
|
@_internal = @injected_options[:_internal] || {}
|
@@ -19,6 +22,8 @@ module SimpleAMS
|
|
19
22
|
def with_resource(resource)
|
20
23
|
@resource = resource
|
21
24
|
|
25
|
+
clean_volatile_properties!
|
26
|
+
|
22
27
|
return self
|
23
28
|
end
|
24
29
|
|
@@ -27,15 +32,15 @@ module SimpleAMS
|
|
27
32
|
end
|
28
33
|
|
29
34
|
def primary_id
|
30
|
-
return
|
35
|
+
return tracked(__method__).value if tracked(__method__).value
|
31
36
|
|
32
|
-
return
|
37
|
+
return tracked(__method__).value = array_of_value_hash_for(PrimaryId, :primary_id)
|
33
38
|
end
|
34
39
|
|
35
40
|
def type
|
36
|
-
return
|
41
|
+
return tracked(__method__).value if tracked(__method__).value
|
37
42
|
|
38
|
-
return
|
43
|
+
return tracked(__method__).value = array_of_value_hash_for(Type, :type)
|
39
44
|
end
|
40
45
|
|
41
46
|
def name
|
@@ -66,27 +71,27 @@ module SimpleAMS
|
|
66
71
|
end
|
67
72
|
|
68
73
|
def links
|
69
|
-
return
|
74
|
+
return tracked(__method__).value if tracked(__method__).value
|
70
75
|
|
71
|
-
return
|
76
|
+
return tracked(__method__).value = array_of_name_value_hash_for(Links, Links::Link, :links)
|
72
77
|
end
|
73
78
|
|
74
79
|
def metas
|
75
|
-
return
|
80
|
+
return tracked(__method__).value if tracked(__method__).value
|
76
81
|
|
77
|
-
return
|
82
|
+
return tracked(__method__).value = array_of_name_value_hash_for(Metas, Metas::Meta, :metas)
|
78
83
|
end
|
79
84
|
|
80
85
|
def forms
|
81
|
-
return
|
86
|
+
return tracked(__method__).value if tracked(__method__).value
|
82
87
|
|
83
|
-
return
|
88
|
+
return tracked(__method__).value = array_of_name_value_hash_for(Forms, Forms::Form, :forms)
|
84
89
|
end
|
85
90
|
|
86
91
|
def generics
|
87
|
-
return
|
92
|
+
return tracked(__method__).value if tracked(__method__).value
|
88
93
|
|
89
|
-
return
|
94
|
+
return tracked(__method__).value = array_of_name_value_hash_for(
|
90
95
|
Generics, Generics::Option, :generics
|
91
96
|
)
|
92
97
|
end
|
@@ -112,11 +117,13 @@ module SimpleAMS
|
|
112
117
|
resource: resource, serializer: serializer
|
113
118
|
})
|
114
119
|
end
|
120
|
+
=begin
|
115
121
|
if @adapter.value.nil?
|
116
122
|
@adapter = Adapter.new(SimpleAMS::Adapters::AMS, {
|
117
123
|
resource: resource, serializer: serializer
|
118
124
|
})
|
119
125
|
end
|
126
|
+
=end
|
120
127
|
|
121
128
|
return @adapter
|
122
129
|
end
|
@@ -175,7 +182,7 @@ module SimpleAMS
|
|
175
182
|
def collection_serializer_class
|
176
183
|
return @collection_serializer_class if defined?(@collection_serializer_class)
|
177
184
|
|
178
|
-
if serializer_class.is_a?(Proc)
|
185
|
+
if serializer_class.is_a?(Proc) #TODO: maybe we should do duck typing instead?
|
179
186
|
@collection_serializer_class = injected_options[:collection_serializer]
|
180
187
|
if @collection_serializer_class.nil?
|
181
188
|
raise "In case of a proc serializer, you need to specify a collection_serializer"
|
@@ -239,7 +246,7 @@ module SimpleAMS
|
|
239
246
|
def priority_options_for(allowed:, injected:)
|
240
247
|
if not injected.nil?
|
241
248
|
allowed = injected.class.new(
|
242
|
-
injected.map{|s| s.is_a?(Hash) ? s.
|
249
|
+
injected.map{|s| s.is_a?(Hash) ? (s.first && s.first[0]) : s}
|
243
250
|
) & allowed
|
244
251
|
end
|
245
252
|
|
@@ -256,10 +263,9 @@ module SimpleAMS
|
|
256
263
|
return @_relation_options if defined?(@_relation_options)
|
257
264
|
|
258
265
|
@_relation_options = relations.inject({}){|memo, relation|
|
259
|
-
includes_value = (injected_options[:includes] || {}).
|
260
|
-
|
261
|
-
|
262
|
-
incl_hash.keys.first.to_s == relation.name.to_s
|
266
|
+
includes_value = (injected_options[:includes] || {}).find{|incl_hash|
|
267
|
+
incl_hash.is_a?(Hash) &&
|
268
|
+
(incl_hash.first && incl_hash.first[0]).to_s == relation.name.to_s
|
263
269
|
}
|
264
270
|
if includes_value
|
265
271
|
includes_value = includes_value[relation.name]
|
@@ -268,10 +274,9 @@ module SimpleAMS
|
|
268
274
|
includes_value = []
|
269
275
|
end
|
270
276
|
|
271
|
-
fields_value = (injected_options[:fields] || {}).
|
272
|
-
|
273
|
-
|
274
|
-
field_hash.keys.first.to_s == relation.name.to_s
|
277
|
+
fields_value = (injected_options[:fields] || {}).find{|field_hash|
|
278
|
+
field_hash.is_a?(Hash) &&
|
279
|
+
(field_hash.first && field_hash.first[0]).to_s == relation.name.to_s
|
275
280
|
}
|
276
281
|
|
277
282
|
#.. while here just nil will work (pick default fields from serializer)
|
@@ -288,7 +293,7 @@ module SimpleAMS
|
|
288
293
|
#TODO: raise exception if both are nil!
|
289
294
|
def fetch_allowed_options
|
290
295
|
_serializer_class = self.serializer_class
|
291
|
-
if _serializer_class.is_a?(Proc)
|
296
|
+
if _serializer_class.is_a?(Proc) #TODO: maybe we should do duck typing instead?
|
292
297
|
_serializer_class = self.collection_serializer_class
|
293
298
|
end
|
294
299
|
|
@@ -311,7 +316,7 @@ module SimpleAMS
|
|
311
316
|
|
312
317
|
def infer_serializer
|
313
318
|
namespace = _internal[:module] ? "#{_internal[:module]}::" : ""
|
314
|
-
resource_klass = resource.kind_of?(Array) ? resource.
|
319
|
+
resource_klass = resource.kind_of?(Array) ? resource[0].class : resource.class
|
315
320
|
if resource_klass == NilClass
|
316
321
|
return EmptySerializer
|
317
322
|
else
|
data/lib/simple_ams/version.rb
CHANGED
data/lib/simple_ams.rb
CHANGED
@@ -8,17 +8,18 @@ require "simple_ams/adapters/ams"
|
|
8
8
|
require "simple_ams/adapters/jsonapi"
|
9
9
|
require "simple_ams/renderer"
|
10
10
|
|
11
|
-
require "simple_ams/options"
|
12
11
|
require "simple_ams/options/concerns/filterable"
|
13
12
|
require "simple_ams/options/concerns/name_value_hash"
|
14
13
|
require "simple_ams/options/concerns/value_hash"
|
14
|
+
require "simple_ams/options/concerns/tracked_properties"
|
15
|
+
require "simple_ams/options"
|
15
16
|
require "simple_ams/options/adapter"
|
16
17
|
require "simple_ams/options/fields"
|
17
18
|
require "simple_ams/options/includes"
|
19
|
+
require "simple_ams/options/generics"
|
18
20
|
require "simple_ams/options/links"
|
19
21
|
require "simple_ams/options/metas"
|
20
22
|
require "simple_ams/options/forms"
|
21
|
-
require "simple_ams/options/generics"
|
22
23
|
require "simple_ams/options/primary_id"
|
23
24
|
require "simple_ams/options/type"
|
24
25
|
require "simple_ams/options/relations"
|
@@ -26,10 +27,30 @@ require "simple_ams/options/relations"
|
|
26
27
|
require "simple_ams/document/primary_id"
|
27
28
|
require "simple_ams/document/fields"
|
28
29
|
require "simple_ams/document/relations"
|
30
|
+
require "simple_ams/document/generics"
|
29
31
|
require "simple_ams/document/links"
|
30
32
|
require "simple_ams/document/metas"
|
31
33
|
require "simple_ams/document/forms"
|
32
|
-
require "
|
34
|
+
require "logger"
|
33
35
|
|
34
36
|
module SimpleAMS
|
37
|
+
class << self
|
38
|
+
def configuration
|
39
|
+
@configuration ||= Configuration.new
|
40
|
+
end
|
41
|
+
attr_writer :configuration
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.configure
|
45
|
+
self.configuration ||= Configuration.new
|
46
|
+
yield(self.configuration)
|
47
|
+
end
|
48
|
+
|
49
|
+
class Configuration
|
50
|
+
attr_accessor :logger
|
51
|
+
|
52
|
+
def initialize
|
53
|
+
@logger = ::Logger.new(STDOUT)
|
54
|
+
end
|
55
|
+
end
|
35
56
|
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.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Filippos Vasilakis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -127,6 +127,7 @@ files:
|
|
127
127
|
- lib/simple_ams/options/adapter.rb
|
128
128
|
- lib/simple_ams/options/concerns/filterable.rb
|
129
129
|
- lib/simple_ams/options/concerns/name_value_hash.rb
|
130
|
+
- lib/simple_ams/options/concerns/tracked_properties.rb
|
130
131
|
- lib/simple_ams/options/concerns/value_hash.rb
|
131
132
|
- lib/simple_ams/options/fields.rb
|
132
133
|
- lib/simple_ams/options/forms.rb
|
@@ -158,8 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
159
|
- !ruby/object:Gem::Version
|
159
160
|
version: '0'
|
160
161
|
requirements: []
|
161
|
-
|
162
|
-
rubygems_version: 2.6.12
|
162
|
+
rubygems_version: 3.0.2
|
163
163
|
signing_key:
|
164
164
|
specification_version: 4
|
165
165
|
summary: ActiveModel Serializers, simplified.
|