simple_ams 0.2.1 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +665 -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 +47 -64
  12. data/lib/simple_ams/document.rb +38 -37
  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 +267 -265
  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 -17
  41. metadata +30 -27
  42. data/.travis.yml +0 -5
@@ -1,9 +1,9 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
4
  class Adapter
5
5
  include SimpleAMS::Options::Concerns::ValueHash
6
6
 
7
- alias :klass :value
7
+ alias klass value
8
8
  end
9
9
  end
@@ -1,44 +1,39 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
- module Concerns
5
- #works for arrays that can hold either elements or object that respond_to? :name
6
- module Filterable
7
- #for optimizing performance, ask only the first element
8
- #other idea is to create another module just for (Name)ValueHash objects
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
- return self.class.new(
13
- self.select{|m|
14
- if other_is_object
15
- other_filterables.include?(m.name)
16
- else
17
- other_filterables.include?(m)
18
- end
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
- if @self_is_object
30
- self.any?{|s| s.name == member}
31
- else
32
- super
33
- end
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
- def raw
37
- if self[0].respond_to?(:raw)
38
- self.map(&:raw)
39
- else
40
- self.map{|i| i}
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
@@ -0,0 +1,4 @@
1
+ class SimpleAMS::Options
2
+ module Concerns
3
+ end
4
+ end
@@ -1,38 +1,37 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
- module Concerns
5
- module NameValueHash
6
- attr_reader :name, :value, :options
4
+ module Concerns::NameValueHash
5
+ attr_reader :name, :value, :options
7
6
 
8
- def initialize(name, value, options = {}, resource:, serializer:)
9
- @name = name.is_a?(String) ? name.to_sym : name
10
- if value.respond_to?(:call)
11
- @volatile = true
12
- _value = value.call(resource, serializer)
13
- if _value.is_a?(Array) && _value.length > 1
14
- @value = _value[0]
15
- @options = (_value[-1] || {}).merge(options || {})
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 = 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
- def raw
27
- [name, value, options]
28
- end
29
-
30
- def volatile?
31
- return @volatile || false
32
- end
25
+ def raw
26
+ [name, value, options]
27
+ end
33
28
 
34
- private
35
- attr_writer :name, :value, :options
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
- 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
2
+ module Concerns::TrackedProperties
3
+ Tracked = Struct.new(:value) do
4
+ def volatile?
5
+ return @volatile if defined?(@volatile)
10
6
 
11
- def initialize_tracking!
12
- @tracked_properties = {}
7
+ @volatile ||= value.volatile?
13
8
  end
9
+ end
14
10
 
15
- def clean_volatile_properties!
16
- @tracked_properties = @tracked_properties.select{|k, v| !v.volatile?}
17
- end
11
+ def initialize_tracking!
12
+ @tracked_properties = {}
13
+ end
18
14
 
19
- def tracked(meth)
20
- @tracked_properties[meth] ||= Tracked.new
21
- end
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 "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
- module Concerns
5
- module ValueHash
6
- attr_reader :value, :options
7
- alias :name :value
4
+ module Concerns::ValueHash
5
+ attr_reader :value, :options
6
+ alias name value
8
7
 
9
- def initialize(value, options = {}, resource:, serializer:)
10
- if value.respond_to?(:call)
11
- @volatile = true
12
- _value = value.call(resource, serializer)
13
- if _value.is_a?(Array) && _value.length > 1
14
- @value = _value[0]
15
- @options = (_value[-1] || {}).merge(options || {})
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 = value.is_a?(String) ? value.to_sym : 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
- def raw
27
- [value, options]
28
- end
29
-
30
- def volatile?
31
- return @volatile || false
32
- end
25
+ def raw
26
+ [value, options]
27
+ end
33
28
 
34
- private
35
- attr_writer :value, :options
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,4 +1,4 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
4
  class Fields < Array
@@ -1,4 +1,4 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
4
  class Forms < Generics
@@ -9,4 +9,3 @@ class SimpleAMS::Options
9
9
  end
10
10
  end
11
11
  end
12
-
@@ -1,11 +1,11 @@
1
- require "simple_ams"
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
- self.any?(&:volatile?)
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 "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
4
  class Includes < Array
@@ -1,4 +1,4 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
4
  class Links < Generics
@@ -1,4 +1,4 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
4
  class Metas < Generics
@@ -1,4 +1,4 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
4
  class PrimaryId
@@ -1,4 +1,4 @@
1
- require "simple_ams"
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
- |relation| includes.include?(relation.name)
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 ? true : false
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
- attr_writer :type, :name, :options
50
+
51
+ attr_writer :type, :name, :options
50
52
  end
51
53
  end
52
54
  end
@@ -1,8 +1,7 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
3
  class SimpleAMS::Options
4
4
  class Type
5
5
  include SimpleAMS::Options::Concerns::ValueHash
6
6
  end
7
7
  end
8
-
@@ -1,59 +1,61 @@
1
- require "simple_ams"
1
+ require 'simple_ams'
2
2
 
3
- module SimpleAMS
4
- class Renderer
5
- def initialize(resource, options = {})
6
- @resource = resource
7
- @options = SimpleAMS::Options.new(resource, injected_options: options)
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
- #resource decorator ?
11
- def document
12
- @document ||= SimpleAMS::Document.new(options)
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 name
16
- @options.name
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(document, options.adapter.options).as_json
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
- class Collection
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
- private
48
- attr_reader :collection, :options
49
+ attr_reader :collection, :options
49
50
 
50
- def is_collection
51
- { is_collection: true }
52
- end
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