simple_ams 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +28 -0
- data/.rubocop.yml +56 -0
- data/CHANGELOG.md +22 -0
- data/Gemfile +2 -2
- data/README.md +663 -116
- data/Rakefile +3 -3
- data/bin/console +3 -3
- data/lib/simple_ams.rb +34 -33
- data/lib/simple_ams/adapters/ams.rb +26 -32
- data/lib/simple_ams/adapters/jsonapi.rb +46 -65
- data/lib/simple_ams/document.rb +38 -28
- data/lib/simple_ams/document/fields.rb +36 -37
- data/lib/simple_ams/document/forms.rb +7 -9
- data/lib/simple_ams/document/generics.rb +35 -37
- data/lib/simple_ams/document/links.rb +7 -9
- data/lib/simple_ams/document/metas.rb +7 -11
- data/lib/simple_ams/document/primary_id.rb +14 -17
- data/lib/simple_ams/document/relations.rb +99 -109
- data/lib/simple_ams/dsl.rb +73 -71
- data/lib/simple_ams/methy.rb +2 -2
- data/lib/simple_ams/options.rb +268 -266
- data/lib/simple_ams/options/adapter.rb +2 -2
- data/lib/simple_ams/options/concerns/filterable.rb +29 -34
- data/lib/simple_ams/options/concerns/mod.rb +4 -0
- data/lib/simple_ams/options/concerns/name_value_hash.rb +25 -26
- data/lib/simple_ams/options/concerns/tracked_properties.rb +15 -17
- data/lib/simple_ams/options/concerns/value_hash.rb +25 -26
- data/lib/simple_ams/options/fields.rb +1 -1
- data/lib/simple_ams/options/forms.rb +1 -2
- data/lib/simple_ams/options/generics.rb +2 -4
- data/lib/simple_ams/options/includes.rb +1 -1
- data/lib/simple_ams/options/links.rb +1 -1
- data/lib/simple_ams/options/metas.rb +1 -1
- data/lib/simple_ams/options/primary_id.rb +1 -1
- data/lib/simple_ams/options/relations.rb +9 -7
- data/lib/simple_ams/options/type.rb +1 -2
- data/lib/simple_ams/renderer.rb +43 -41
- data/lib/simple_ams/version.rb +1 -1
- data/simple_ams.gemspec +17 -16
- metadata +38 -21
- data/.travis.yml +0 -15
@@ -1,44 +1,39 @@
|
|
1
|
-
require
|
1
|
+
require 'simple_ams'
|
2
2
|
|
3
3
|
class SimpleAMS::Options
|
4
|
-
module Concerns
|
5
|
-
#
|
6
|
-
module
|
7
|
-
|
8
|
-
|
9
|
-
def &(other_filterables)
|
10
|
-
other_is_object = other_filterables[0].respond_to?(:name)
|
4
|
+
module Concerns::Filterable
|
5
|
+
# for optimizing performance, ask only the first element
|
6
|
+
# other idea is to create another module just for (Name)ValueHash objects
|
7
|
+
def &(other)
|
8
|
+
other_is_object = (other[0].respond_to?(:name) && other[0].class != Symbol)
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
}
|
20
|
-
)
|
21
|
-
end
|
22
|
-
|
23
|
-
#for optimizing performance, ask only the first element of self and save it as state
|
24
|
-
def include?(member)
|
25
|
-
unless defined?(@self_is_object)
|
26
|
-
@self_is_object = self[0].respond_to?(:name)
|
10
|
+
self.class.new(
|
11
|
+
self.select do |m|
|
12
|
+
if other_is_object
|
13
|
+
other.include?(m.name)
|
14
|
+
else
|
15
|
+
other.include?(m)
|
16
|
+
end
|
27
17
|
end
|
18
|
+
)
|
19
|
+
end
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
21
|
+
# for optimizing performance, ask only the first element of self and save it as state
|
22
|
+
def include?(member)
|
23
|
+
@self_is_object = self[0].respond_to?(:name) && self[0].class != Symbol unless defined?(@self_is_object)
|
24
|
+
|
25
|
+
if @self_is_object
|
26
|
+
any? { |s| s.name == member }
|
27
|
+
else
|
28
|
+
super
|
34
29
|
end
|
30
|
+
end
|
35
31
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
end
|
32
|
+
def raw
|
33
|
+
if self[0].respond_to?(:raw)
|
34
|
+
map(&:raw)
|
35
|
+
else
|
36
|
+
map { |i| i }
|
42
37
|
end
|
43
38
|
end
|
44
39
|
end
|
@@ -1,38 +1,37 @@
|
|
1
|
-
require
|
1
|
+
require 'simple_ams'
|
2
2
|
|
3
3
|
class SimpleAMS::Options
|
4
|
-
module Concerns
|
5
|
-
|
6
|
-
attr_reader :name, :value, :options
|
4
|
+
module Concerns::NameValueHash
|
5
|
+
attr_reader :name, :value, :options
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
else
|
17
|
-
@value = _value
|
18
|
-
@options = options || {}
|
19
|
-
end
|
7
|
+
def initialize(name, value, options = {}, resource:, serializer:)
|
8
|
+
@name = name.is_a?(String) ? name.to_sym : name
|
9
|
+
if value.respond_to?(:call)
|
10
|
+
@volatile = true
|
11
|
+
computed_value = value.call(resource, serializer)
|
12
|
+
if computed_value.is_a?(Array) && computed_value.length > 1
|
13
|
+
@value = computed_value[0]
|
14
|
+
@options = (computed_value[-1] || {}).merge(options || {})
|
20
15
|
else
|
21
|
-
@value =
|
16
|
+
@value = computed_value
|
22
17
|
@options = options || {}
|
23
18
|
end
|
19
|
+
else
|
20
|
+
@value = value
|
21
|
+
@options = options || {}
|
24
22
|
end
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
def volatile?
|
31
|
-
return @volatile || false
|
32
|
-
end
|
25
|
+
def raw
|
26
|
+
[name, value, options]
|
27
|
+
end
|
33
28
|
|
34
|
-
|
35
|
-
|
29
|
+
def volatile?
|
30
|
+
@volatile || false
|
36
31
|
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
attr_writer :name, :value, :options
|
37
36
|
end
|
38
37
|
end
|
@@ -1,25 +1,23 @@
|
|
1
1
|
class SimpleAMS::Options
|
2
|
-
module Concerns
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
return @volatile if defined?(@volatile)
|
7
|
-
return @volatile ||= self.value.volatile?
|
8
|
-
end
|
9
|
-
end
|
2
|
+
module Concerns::TrackedProperties
|
3
|
+
Tracked = Struct.new(:value) do
|
4
|
+
def volatile?
|
5
|
+
return @volatile if defined?(@volatile)
|
10
6
|
|
11
|
-
|
12
|
-
@tracked_properties = {}
|
7
|
+
@volatile ||= value.volatile?
|
13
8
|
end
|
9
|
+
end
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
def initialize_tracking!
|
12
|
+
@tracked_properties = {}
|
13
|
+
end
|
18
14
|
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
def clean_volatile_properties!
|
16
|
+
@tracked_properties = @tracked_properties.reject { |_k, v| v.volatile? }
|
17
|
+
end
|
18
|
+
|
19
|
+
def tracked(meth)
|
20
|
+
@tracked_properties[meth] ||= Tracked.new
|
22
21
|
end
|
23
22
|
end
|
24
23
|
end
|
25
|
-
|
@@ -1,38 +1,37 @@
|
|
1
|
-
require
|
1
|
+
require 'simple_ams'
|
2
2
|
|
3
3
|
class SimpleAMS::Options
|
4
|
-
module Concerns
|
5
|
-
|
6
|
-
|
7
|
-
alias :name :value
|
4
|
+
module Concerns::ValueHash
|
5
|
+
attr_reader :value, :options
|
6
|
+
alias name value
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
else
|
17
|
-
@value = _value
|
18
|
-
@options = options || {}
|
19
|
-
end
|
8
|
+
def initialize(value, options = {}, resource:, serializer:)
|
9
|
+
if value.respond_to?(:call)
|
10
|
+
@volatile = true
|
11
|
+
computed_value = value.call(resource, serializer)
|
12
|
+
if computed_value.is_a?(Array) && computed_value.length > 1
|
13
|
+
@value = computed_value[0]
|
14
|
+
@options = (computed_value[-1] || {}).merge(options || {})
|
20
15
|
else
|
21
|
-
@value =
|
16
|
+
@value = computed_value
|
22
17
|
@options = options || {}
|
23
18
|
end
|
19
|
+
else
|
20
|
+
@value = value.is_a?(String) ? value.to_sym : value
|
21
|
+
@options = options || {}
|
24
22
|
end
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
def volatile?
|
31
|
-
return @volatile || false
|
32
|
-
end
|
25
|
+
def raw
|
26
|
+
[value, options]
|
27
|
+
end
|
33
28
|
|
34
|
-
|
35
|
-
|
29
|
+
def volatile?
|
30
|
+
@volatile || false
|
36
31
|
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
attr_writer :value, :options
|
37
36
|
end
|
38
37
|
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require 'simple_ams'
|
2
2
|
|
3
3
|
class SimpleAMS::Options
|
4
4
|
class Generics < Array
|
5
5
|
include SimpleAMS::Options::Concerns::Filterable
|
6
6
|
|
7
7
|
def volatile?
|
8
|
-
|
8
|
+
any?(&:volatile?)
|
9
9
|
end
|
10
10
|
|
11
11
|
class Option
|
@@ -13,5 +13,3 @@ class SimpleAMS::Options
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
16
|
-
|
17
|
-
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'simple_ams'
|
2
2
|
|
3
3
|
class SimpleAMS::Options
|
4
4
|
class Relations < Array
|
@@ -8,27 +8,28 @@ class SimpleAMS::Options
|
|
8
8
|
@relations = relations
|
9
9
|
@includes = includes
|
10
10
|
|
11
|
-
super(relations.map{|rel| Relations::Relation.new(*rel)})
|
11
|
+
super(relations.map { |rel| Relations::Relation.new(*rel) })
|
12
12
|
end
|
13
13
|
|
14
14
|
def available
|
15
15
|
return @available ||= self if includes.nil?
|
16
16
|
return @available ||= [] if includes.empty?
|
17
17
|
|
18
|
-
@available ||= self.select
|
19
|
-
|
20
|
-
|
18
|
+
@available ||= self.select do |relation|
|
19
|
+
includes.include?(relation.name)
|
20
|
+
end
|
21
21
|
end
|
22
22
|
|
23
23
|
class Relation
|
24
24
|
attr_reader :type, :name, :options, :embedded
|
25
|
+
|
25
26
|
def initialize(type, name, options = {}, embedded)
|
26
27
|
@type = type.to_sym
|
27
28
|
@name = name.is_a?(String) ? name.to_sym : name
|
28
29
|
@options = options
|
29
30
|
@embedded = embedded
|
30
31
|
|
31
|
-
@many = type == :has_many
|
32
|
+
@many = type == :has_many
|
32
33
|
end
|
33
34
|
|
34
35
|
alias relation name
|
@@ -46,7 +47,8 @@ class SimpleAMS::Options
|
|
46
47
|
end
|
47
48
|
|
48
49
|
private
|
49
|
-
|
50
|
+
|
51
|
+
attr_writer :type, :name, :options
|
50
52
|
end
|
51
53
|
end
|
52
54
|
end
|
data/lib/simple_ams/renderer.rb
CHANGED
@@ -1,59 +1,61 @@
|
|
1
|
-
require
|
1
|
+
require 'simple_ams'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
3
|
+
class SimpleAMS::Renderer
|
4
|
+
def initialize(resource, options = {})
|
5
|
+
@resource = resource
|
6
|
+
@options = SimpleAMS::Options.new(resource, injected_options: options)
|
7
|
+
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
# resource decorator ?
|
10
|
+
def document
|
11
|
+
@document ||= SimpleAMS::Document.new(options)
|
12
|
+
end
|
13
|
+
|
14
|
+
def name
|
15
|
+
@options.name
|
16
|
+
end
|
17
|
+
|
18
|
+
def as_json
|
19
|
+
options.adapter.klass.new(document, options.adapter.options).as_json
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_json(*_args)
|
23
|
+
as_json.to_json
|
24
|
+
end
|
25
|
+
|
26
|
+
class Collection
|
27
|
+
def initialize(collection, options = {})
|
28
|
+
@collection = collection
|
29
|
+
@options = SimpleAMS::Options.new(
|
30
|
+
collection,
|
31
|
+
injected_options: options.merge(_internal: is_collection)
|
32
|
+
)
|
13
33
|
end
|
14
34
|
|
15
|
-
def
|
16
|
-
@options
|
35
|
+
def folder
|
36
|
+
@folder ||= SimpleAMS::Document::Folder.new(options)
|
17
37
|
end
|
18
38
|
|
19
39
|
def as_json
|
20
|
-
options.adapter.klass.new(
|
40
|
+
options.adapter.klass::Collection.new(folder, options.adapter.options).as_json
|
21
41
|
end
|
22
42
|
|
23
|
-
def to_json
|
43
|
+
def to_json(*_args)
|
24
44
|
as_json.to_json
|
25
45
|
end
|
26
46
|
|
27
|
-
|
28
|
-
def initialize(collection, options = {})
|
29
|
-
@collection = collection
|
30
|
-
@options = SimpleAMS::Options.new(collection, {
|
31
|
-
injected_options: options.merge(_internal: is_collection)
|
32
|
-
})
|
33
|
-
end
|
34
|
-
|
35
|
-
def folder
|
36
|
-
@folder ||= SimpleAMS::Document::Folder.new(options)
|
37
|
-
end
|
38
|
-
|
39
|
-
def as_json
|
40
|
-
options.adapter.klass::Collection.new(folder, options.adapter.options).as_json
|
41
|
-
end
|
42
|
-
|
43
|
-
def to_json
|
44
|
-
as_json.to_json
|
45
|
-
end
|
47
|
+
private
|
46
48
|
|
47
|
-
|
48
|
-
attr_reader :collection, :options
|
49
|
+
attr_reader :collection, :options
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
# rubocop:disable Naming/PredicateName
|
52
|
+
def is_collection
|
53
|
+
{ is_collection: true }
|
53
54
|
end
|
54
|
-
|
55
|
-
private
|
56
|
-
attr_reader :resource, :options
|
55
|
+
# rubocop:enable Naming/PredicateName
|
57
56
|
end
|
58
57
|
|
58
|
+
private
|
59
|
+
|
60
|
+
attr_reader :resource, :options
|
59
61
|
end
|