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
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'SimpleAMS'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "SimpleAMS"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start(__FILE__)
|
data/lib/simple_ams.rb
CHANGED
@@ -1,37 +1,38 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
1
|
+
require 'delegate'
|
2
|
+
require 'simple_ams/version'
|
3
|
+
require 'simple_ams/methy'
|
4
|
+
require 'simple_ams/document'
|
5
|
+
require 'simple_ams/dsl'
|
6
|
+
require 'simple_ams/adapters'
|
7
|
+
require 'simple_ams/adapters/ams'
|
8
|
+
require 'simple_ams/adapters/jsonapi'
|
9
|
+
require 'simple_ams/renderer'
|
10
10
|
|
11
|
-
require
|
12
|
-
require
|
13
|
-
require
|
14
|
-
require
|
15
|
-
require
|
16
|
-
require
|
17
|
-
require
|
18
|
-
require
|
19
|
-
require
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
23
|
-
require
|
24
|
-
require
|
25
|
-
require
|
11
|
+
require 'simple_ams/options/concerns/mod'
|
12
|
+
require 'simple_ams/options/concerns/filterable'
|
13
|
+
require 'simple_ams/options/concerns/name_value_hash'
|
14
|
+
require 'simple_ams/options/concerns/value_hash'
|
15
|
+
require 'simple_ams/options/concerns/tracked_properties'
|
16
|
+
require 'simple_ams/options'
|
17
|
+
require 'simple_ams/options/adapter'
|
18
|
+
require 'simple_ams/options/fields'
|
19
|
+
require 'simple_ams/options/includes'
|
20
|
+
require 'simple_ams/options/generics'
|
21
|
+
require 'simple_ams/options/links'
|
22
|
+
require 'simple_ams/options/metas'
|
23
|
+
require 'simple_ams/options/forms'
|
24
|
+
require 'simple_ams/options/primary_id'
|
25
|
+
require 'simple_ams/options/type'
|
26
|
+
require 'simple_ams/options/relations'
|
26
27
|
|
27
|
-
require
|
28
|
-
require
|
29
|
-
require
|
30
|
-
require
|
31
|
-
require
|
32
|
-
require
|
33
|
-
require
|
34
|
-
require
|
28
|
+
require 'simple_ams/document/primary_id'
|
29
|
+
require 'simple_ams/document/fields'
|
30
|
+
require 'simple_ams/document/relations'
|
31
|
+
require 'simple_ams/document/generics'
|
32
|
+
require 'simple_ams/document/links'
|
33
|
+
require 'simple_ams/document/metas'
|
34
|
+
require 'simple_ams/document/forms'
|
35
|
+
require 'logger'
|
35
36
|
|
36
37
|
module SimpleAMS
|
37
38
|
class << self
|
@@ -50,7 +51,7 @@ module SimpleAMS
|
|
50
51
|
attr_accessor :logger
|
51
52
|
|
52
53
|
def initialize
|
53
|
-
@logger = ::Logger.new(
|
54
|
+
@logger = ::Logger.new($stdout)
|
54
55
|
end
|
55
56
|
end
|
56
57
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'simple_ams'
|
2
2
|
|
3
3
|
class SimpleAMS::Adapters::AMS
|
4
4
|
attr_reader :document, :options
|
@@ -19,59 +19,55 @@ class SimpleAMS::Adapters::AMS
|
|
19
19
|
hash.merge!(metas: metas) unless metas.empty?
|
20
20
|
hash.merge!(forms: forms) unless forms.empty?
|
21
21
|
|
22
|
-
return {document.name => hash} if options[:root]
|
23
|
-
|
22
|
+
return { document.name => hash } if options[:root]
|
23
|
+
|
24
|
+
hash
|
24
25
|
end
|
25
26
|
|
26
27
|
def fields
|
27
|
-
@fields ||= document.fields.
|
28
|
+
@fields ||= document.fields.each_with_object({}) do |field, hash|
|
28
29
|
hash[field.key] = field.value
|
29
|
-
|
30
|
-
}
|
30
|
+
end
|
31
31
|
end
|
32
32
|
|
33
33
|
def links
|
34
34
|
return @links ||= {} if document.links.empty?
|
35
35
|
|
36
|
-
@links ||= document.links.
|
36
|
+
@links ||= document.links.each_with_object({}) do |link, hash|
|
37
37
|
hash[link.name] = link.value
|
38
|
-
|
39
|
-
}
|
38
|
+
end
|
40
39
|
end
|
41
40
|
|
42
41
|
def metas
|
43
|
-
@metas ||= document.metas.
|
42
|
+
@metas ||= document.metas.each_with_object({}) do |meta, hash|
|
44
43
|
hash[meta.name] = meta.value
|
45
|
-
|
46
|
-
}
|
44
|
+
end
|
47
45
|
end
|
48
46
|
|
49
47
|
def forms
|
50
|
-
@forms ||= document.forms.
|
48
|
+
@forms ||= document.forms.each_with_object({}) do |form, hash|
|
51
49
|
hash[form.name] = form.value
|
52
|
-
|
53
|
-
}
|
50
|
+
end
|
54
51
|
end
|
55
52
|
|
56
53
|
def relations
|
57
54
|
return @relations = {} if document.relations.available.empty?
|
58
55
|
|
59
|
-
@relations ||= document.relations.available.
|
60
|
-
if relation.folder?
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
56
|
+
@relations ||= document.relations.available.each_with_object({}) do |relation, hash|
|
57
|
+
value = if relation.folder?
|
58
|
+
relation.map { |doc| self.class.new(doc).as_json }
|
59
|
+
else
|
60
|
+
self.class.new(relation).as_json
|
61
|
+
end
|
65
62
|
hash[relation.name] = value
|
66
|
-
|
67
|
-
hash
|
68
|
-
}
|
63
|
+
end
|
69
64
|
end
|
70
65
|
|
71
66
|
class Collection < self
|
72
67
|
attr_reader :folder, :adapter, :options
|
73
68
|
|
74
69
|
def initialize(folder, options = {})
|
70
|
+
super
|
75
71
|
@folder = folder
|
76
72
|
@adapter = folder.adapter.value
|
77
73
|
@options = options
|
@@ -90,23 +86,21 @@ class SimpleAMS::Adapters::AMS
|
|
90
86
|
end
|
91
87
|
|
92
88
|
def documents
|
93
|
-
|
89
|
+
folder.map do |document|
|
94
90
|
adapter.new(document).as_json
|
95
|
-
|
91
|
+
end || []
|
96
92
|
end
|
97
93
|
|
98
94
|
def metas
|
99
|
-
@metas ||= folder.metas.
|
95
|
+
@metas ||= folder.metas.each_with_object({}) do |meta, hash|
|
100
96
|
hash[meta.name] = meta.value
|
101
|
-
|
102
|
-
}
|
97
|
+
end
|
103
98
|
end
|
104
99
|
|
105
100
|
def links
|
106
|
-
@links ||= folder.links.
|
101
|
+
@links ||= folder.links.each_with_object({}) do |link, hash|
|
107
102
|
hash[link.name] = link.value
|
108
|
-
|
109
|
-
}
|
103
|
+
end
|
110
104
|
end
|
111
105
|
end
|
112
106
|
end
|
@@ -1,12 +1,13 @@
|
|
1
|
-
require
|
1
|
+
require 'simple_ams'
|
2
2
|
|
3
3
|
class SimpleAMS::Adapters::JSONAPI
|
4
4
|
DEFAULT_OPTIONS = {
|
5
5
|
skip_id_in_attributes: false,
|
6
6
|
key_transform: nil
|
7
|
-
}
|
7
|
+
}.freeze
|
8
8
|
|
9
9
|
attr_reader :document, :options
|
10
|
+
|
10
11
|
def initialize(document, options = {})
|
11
12
|
@document = document
|
12
13
|
@options = DEFAULT_OPTIONS.merge(options)
|
@@ -14,21 +15,21 @@ class SimpleAMS::Adapters::JSONAPI
|
|
14
15
|
|
15
16
|
def as_json
|
16
17
|
hash = {
|
17
|
-
data: data
|
18
|
+
data: data
|
18
19
|
}
|
19
20
|
|
20
21
|
hash.merge!(links: links) unless links.empty?
|
21
22
|
hash.merge!(metas: metas) unless metas.empty?
|
22
23
|
hash.merge!(included: included) unless included.empty?
|
23
24
|
|
24
|
-
|
25
|
+
hash
|
25
26
|
end
|
26
27
|
|
27
28
|
def data
|
28
29
|
data = {
|
29
30
|
transform_key(document.primary_id.name) => document.primary_id.value.to_s,
|
30
31
|
type: document.type.value,
|
31
|
-
attributes: fields
|
32
|
+
attributes: fields
|
32
33
|
}
|
33
34
|
|
34
35
|
data.merge!(relationships: relationships) unless relationships.empty?
|
@@ -37,12 +38,9 @@ class SimpleAMS::Adapters::JSONAPI
|
|
37
38
|
end
|
38
39
|
|
39
40
|
def fields
|
40
|
-
@fields ||= document.fields.
|
41
|
-
unless options[:skip_id_in_attributes] && field_is_primary_id?(field)
|
42
|
-
|
43
|
-
end
|
44
|
-
hash
|
45
|
-
}
|
41
|
+
@fields ||= document.fields.each_with_object({}) do |field, hash|
|
42
|
+
hash[transform_key(field.key)] = field.value unless options[:skip_id_in_attributes] && field_is_primary_id?(field)
|
43
|
+
end
|
46
44
|
end
|
47
45
|
|
48
46
|
def transform_key(key)
|
@@ -50,7 +48,7 @@ class SimpleAMS::Adapters::JSONAPI
|
|
50
48
|
when :camel
|
51
49
|
key.to_s.split('_').map(&capitalize).join
|
52
50
|
when :kebab
|
53
|
-
key.to_s.gsub('_','-')
|
51
|
+
key.to_s.gsub('_', '-')
|
54
52
|
when :snake
|
55
53
|
key
|
56
54
|
else
|
@@ -61,36 +59,29 @@ class SimpleAMS::Adapters::JSONAPI
|
|
61
59
|
def relationships
|
62
60
|
return @relationships ||= {} if document.relations.empty?
|
63
61
|
|
64
|
-
@relationships ||= document.relations.
|
65
|
-
_hash = {}
|
66
|
-
|
62
|
+
@relationships ||= document.relations.each_with_object({}) do |relation, hash|
|
67
63
|
embedded_relation_data = embedded_relation_data_for(relation)
|
68
|
-
unless embedded_relation_data.empty?
|
69
|
-
_hash.merge!(data: embedded_relation_data_for(relation))
|
70
|
-
end
|
64
|
+
hash.merge!(data: embedded_relation_data_for(relation)) unless embedded_relation_data.empty?
|
71
65
|
|
72
66
|
embedded_relation_links = embedded_relation_links_for(relation)
|
73
|
-
unless embedded_relation_links.empty?
|
74
|
-
_hash.merge!(links: embedded_relation_links_for(relation))
|
75
|
-
end
|
67
|
+
hash.merge!(links: embedded_relation_links_for(relation)) unless embedded_relation_links.empty?
|
76
68
|
|
77
|
-
hash.merge!(relation.name =>
|
78
|
-
|
79
|
-
}
|
69
|
+
hash.merge!(relation.name => hash) unless hash.empty?
|
70
|
+
end
|
80
71
|
end
|
81
72
|
|
82
73
|
def embedded_relation_data_for(relation)
|
83
74
|
return {} if relation.embedded.generics[:skip_data]&.value
|
84
75
|
|
85
76
|
if relation.folder?
|
86
|
-
|
77
|
+
relation.each.map do |doc|
|
87
78
|
{
|
88
79
|
doc.primary_id.name => doc.primary_id.value.to_s,
|
89
80
|
type: doc.type.name
|
90
81
|
}
|
91
|
-
|
82
|
+
end
|
92
83
|
else
|
93
|
-
|
84
|
+
{
|
94
85
|
relation.primary_id.name => relation.primary_id.value.to_s,
|
95
86
|
type: relation.type.name
|
96
87
|
}
|
@@ -100,50 +91,43 @@ class SimpleAMS::Adapters::JSONAPI
|
|
100
91
|
def embedded_relation_links_for(relation)
|
101
92
|
return {} if relation.embedded.links.empty?
|
102
93
|
|
103
|
-
relation.embedded.links.
|
94
|
+
relation.embedded.links.each_with_object({}) do |link, hash|
|
104
95
|
hash[link.name] = link.value
|
105
|
-
|
106
|
-
}
|
96
|
+
end
|
107
97
|
end
|
108
98
|
|
109
|
-
|
110
99
|
def links
|
111
100
|
return @links ||= {} if document.links.empty?
|
112
101
|
|
113
|
-
@links ||= document.links.
|
102
|
+
@links ||= document.links.each_with_object({}) do |link, hash|
|
114
103
|
hash[link.name] = link.value
|
115
|
-
|
116
|
-
}
|
104
|
+
end
|
117
105
|
end
|
118
106
|
|
119
107
|
def metas
|
120
|
-
@metas ||= document.metas.
|
108
|
+
@metas ||= document.metas.each_with_object({}) do |meta, hash|
|
121
109
|
hash[meta.name] = meta.value
|
122
|
-
|
123
|
-
}
|
110
|
+
end
|
124
111
|
end
|
125
112
|
|
126
113
|
def forms
|
127
|
-
@forms ||= document.forms.
|
114
|
+
@forms ||= document.forms.each_with_object({}) do |form, hash|
|
128
115
|
hash[form.name] = form.value
|
129
|
-
|
130
|
-
}
|
116
|
+
end
|
131
117
|
end
|
132
118
|
|
133
119
|
def included
|
134
120
|
return @included ||= [] if document.relations.available.empty?
|
135
121
|
|
136
|
-
@included ||= document.relations.available.
|
122
|
+
@included ||= document.relations.available.each_with_object([]) do |relation, array|
|
137
123
|
next array if relation.embedded.generics[:skip_data]&.value
|
138
124
|
|
139
|
-
if relation.folder?
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
array
|
146
|
-
}.flatten
|
125
|
+
array << if relation.folder?
|
126
|
+
relation.map { |doc| self.class.new(doc, options).as_json[:data] }
|
127
|
+
else
|
128
|
+
self.class.new(relation, options).as_json[:data]
|
129
|
+
end
|
130
|
+
end.flatten
|
147
131
|
end
|
148
132
|
|
149
133
|
class Collection < self
|
@@ -163,16 +147,16 @@ class SimpleAMS::Adapters::JSONAPI
|
|
163
147
|
hash.merge!(links: links) unless links.empty?
|
164
148
|
hash.merge!(included: included) unless included.empty?
|
165
149
|
|
166
|
-
|
150
|
+
hash
|
167
151
|
end
|
168
152
|
|
169
153
|
def documents
|
170
154
|
@included = []
|
171
|
-
|
172
|
-
|
173
|
-
@included <<
|
174
|
-
|
175
|
-
|
155
|
+
folder.map do |document|
|
156
|
+
doc = adapter.new(document, options).as_json
|
157
|
+
@included << doc[:included]
|
158
|
+
doc[:data]
|
159
|
+
end || []
|
176
160
|
end
|
177
161
|
|
178
162
|
def included
|
@@ -180,24 +164,21 @@ class SimpleAMS::Adapters::JSONAPI
|
|
180
164
|
end
|
181
165
|
|
182
166
|
def metas
|
183
|
-
@metas ||= folder.metas.
|
167
|
+
@metas ||= folder.metas.each_with_object({}) do |meta, hash|
|
184
168
|
hash[transform_key(meta.name)] = meta.value
|
185
|
-
|
186
|
-
}
|
169
|
+
end
|
187
170
|
end
|
188
171
|
|
189
172
|
def links
|
190
|
-
@links ||= folder.links.
|
173
|
+
@links ||= folder.links.each_with_object({}) do |link, hash|
|
191
174
|
hash[transform_key(link.name)] = link.value
|
192
|
-
|
193
|
-
}
|
175
|
+
end
|
194
176
|
end
|
195
177
|
end
|
196
178
|
|
197
179
|
private
|
198
|
-
def field_is_primary_id?(field)
|
199
|
-
field.key == document.primary_id.name
|
200
|
-
end
|
201
180
|
|
181
|
+
def field_is_primary_id?(field)
|
182
|
+
field.key == document.primary_id.name
|
183
|
+
end
|
202
184
|
end
|
203
|
-
|