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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +28 -0
  3. data/.rubocop.yml +56 -0
  4. data/CHANGELOG.md +22 -0
  5. data/Gemfile +2 -2
  6. data/README.md +663 -116
  7. data/Rakefile +3 -3
  8. data/bin/console +3 -3
  9. data/lib/simple_ams.rb +34 -33
  10. data/lib/simple_ams/adapters/ams.rb +26 -32
  11. data/lib/simple_ams/adapters/jsonapi.rb +46 -65
  12. data/lib/simple_ams/document.rb +38 -28
  13. data/lib/simple_ams/document/fields.rb +36 -37
  14. data/lib/simple_ams/document/forms.rb +7 -9
  15. data/lib/simple_ams/document/generics.rb +35 -37
  16. data/lib/simple_ams/document/links.rb +7 -9
  17. data/lib/simple_ams/document/metas.rb +7 -11
  18. data/lib/simple_ams/document/primary_id.rb +14 -17
  19. data/lib/simple_ams/document/relations.rb +99 -109
  20. data/lib/simple_ams/dsl.rb +73 -71
  21. data/lib/simple_ams/methy.rb +2 -2
  22. data/lib/simple_ams/options.rb +268 -266
  23. data/lib/simple_ams/options/adapter.rb +2 -2
  24. data/lib/simple_ams/options/concerns/filterable.rb +29 -34
  25. data/lib/simple_ams/options/concerns/mod.rb +4 -0
  26. data/lib/simple_ams/options/concerns/name_value_hash.rb +25 -26
  27. data/lib/simple_ams/options/concerns/tracked_properties.rb +15 -17
  28. data/lib/simple_ams/options/concerns/value_hash.rb +25 -26
  29. data/lib/simple_ams/options/fields.rb +1 -1
  30. data/lib/simple_ams/options/forms.rb +1 -2
  31. data/lib/simple_ams/options/generics.rb +2 -4
  32. data/lib/simple_ams/options/includes.rb +1 -1
  33. data/lib/simple_ams/options/links.rb +1 -1
  34. data/lib/simple_ams/options/metas.rb +1 -1
  35. data/lib/simple_ams/options/primary_id.rb +1 -1
  36. data/lib/simple_ams/options/relations.rb +9 -7
  37. data/lib/simple_ams/options/type.rb +1 -2
  38. data/lib/simple_ams/renderer.rb +43 -41
  39. data/lib/simple_ams/version.rb +1 -1
  40. data/simple_ams.gemspec +17 -16
  41. metadata +38 -21
  42. data/.travis.yml +0 -15
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "SimpleAMS"
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 "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
data/lib/simple_ams.rb CHANGED
@@ -1,37 +1,38 @@
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"
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 "simple_ams/options/concerns/filterable"
12
- require "simple_ams/options/concerns/name_value_hash"
13
- require "simple_ams/options/concerns/value_hash"
14
- require "simple_ams/options/concerns/tracked_properties"
15
- require "simple_ams/options"
16
- require "simple_ams/options/adapter"
17
- require "simple_ams/options/fields"
18
- require "simple_ams/options/includes"
19
- require "simple_ams/options/generics"
20
- require "simple_ams/options/links"
21
- require "simple_ams/options/metas"
22
- require "simple_ams/options/forms"
23
- require "simple_ams/options/primary_id"
24
- require "simple_ams/options/type"
25
- require "simple_ams/options/relations"
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 "simple_ams/document/primary_id"
28
- require "simple_ams/document/fields"
29
- require "simple_ams/document/relations"
30
- require "simple_ams/document/generics"
31
- require "simple_ams/document/links"
32
- require "simple_ams/document/metas"
33
- require "simple_ams/document/forms"
34
- require "logger"
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(STDOUT)
54
+ @logger = ::Logger.new($stdout)
54
55
  end
55
56
  end
56
57
  end
@@ -1,4 +1,4 @@
1
- require "simple_ams"
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
- return hash
22
+ return { document.name => hash } if options[:root]
23
+
24
+ hash
24
25
  end
25
26
 
26
27
  def fields
27
- @fields ||= document.fields.inject({}){ |hash, field|
28
+ @fields ||= document.fields.each_with_object({}) do |field, hash|
28
29
  hash[field.key] = field.value
29
- hash
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.inject({}){ |hash, link|
36
+ @links ||= document.links.each_with_object({}) do |link, hash|
37
37
  hash[link.name] = link.value
38
- hash
39
- }
38
+ end
40
39
  end
41
40
 
42
41
  def metas
43
- @metas ||= document.metas.inject({}){ |hash, meta|
42
+ @metas ||= document.metas.each_with_object({}) do |meta, hash|
44
43
  hash[meta.name] = meta.value
45
- hash
46
- }
44
+ end
47
45
  end
48
46
 
49
47
  def forms
50
- @forms ||= document.forms.inject({}){ |hash, form|
48
+ @forms ||= document.forms.each_with_object({}) do |form, hash|
51
49
  hash[form.name] = form.value
52
- hash
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.inject({}){ |hash, relation|
60
- if relation.folder?
61
- value = relation.map{|doc| self.class.new(doc).as_json}
62
- else
63
- value = self.class.new(relation).as_json
64
- end
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
- return folder.map{|document|
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.inject({}){ |hash, meta|
95
+ @metas ||= folder.metas.each_with_object({}) do |meta, hash|
100
96
  hash[meta.name] = meta.value
101
- hash
102
- }
97
+ end
103
98
  end
104
99
 
105
100
  def links
106
- @links ||= folder.links.inject({}){ |hash, link|
101
+ @links ||= folder.links.each_with_object({}) do |link, hash|
107
102
  hash[link.name] = link.value
108
- hash
109
- }
103
+ end
110
104
  end
111
105
  end
112
106
  end
@@ -1,12 +1,13 @@
1
- require "simple_ams"
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
- return hash
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.inject({}){ |hash, field|
41
- unless options[:skip_id_in_attributes] && field_is_primary_id?(field)
42
- hash[transform_key(field.key)] = field.value
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.inject({}){ |hash, relation|
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 => _hash) unless _hash.empty?
78
- hash
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
- value = relation.each.map{|doc|
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
- value = {
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.inject({}){ |hash, link|
94
+ relation.embedded.links.each_with_object({}) do |link, hash|
104
95
  hash[link.name] = link.value
105
- hash
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.inject({}){ |hash, link|
102
+ @links ||= document.links.each_with_object({}) do |link, hash|
114
103
  hash[link.name] = link.value
115
- hash
116
- }
104
+ end
117
105
  end
118
106
 
119
107
  def metas
120
- @metas ||= document.metas.inject({}){ |hash, meta|
108
+ @metas ||= document.metas.each_with_object({}) do |meta, hash|
121
109
  hash[meta.name] = meta.value
122
- hash
123
- }
110
+ end
124
111
  end
125
112
 
126
113
  def forms
127
- @forms ||= document.forms.inject({}){ |hash, form|
114
+ @forms ||= document.forms.each_with_object({}) do |form, hash|
128
115
  hash[form.name] = form.value
129
- hash
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.inject([]){ |array, relation|
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
- array << relation.map{|doc| self.class.new(doc, options).as_json[:data]}
141
- else
142
- array << self.class.new(relation, options).as_json[:data]
143
- end
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
- return hash
150
+ hash
167
151
  end
168
152
 
169
153
  def documents
170
154
  @included = []
171
- return folder.map{|document|
172
- _doc = adapter.new(document, options).as_json
173
- @included << _doc[:included]
174
- _doc[:data]
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.inject({}){ |hash, meta|
167
+ @metas ||= folder.metas.each_with_object({}) do |meta, hash|
184
168
  hash[transform_key(meta.name)] = meta.value
185
- hash
186
- }
169
+ end
187
170
  end
188
171
 
189
172
  def links
190
- @links ||= folder.links.inject({}){ |hash, link|
173
+ @links ||= folder.links.each_with_object({}) do |link, hash|
191
174
  hash[transform_key(link.name)] = link.value
192
- hash
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
-