grape-entity 0.4.8 → 0.10.2

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 (55) hide show
  1. checksums.yaml +5 -5
  2. data/.coveralls.yml +1 -0
  3. data/.github/dependabot.yml +20 -0
  4. data/.github/workflows/ci.yml +41 -0
  5. data/.gitignore +5 -1
  6. data/.rspec +2 -1
  7. data/.rubocop.yml +85 -2
  8. data/.rubocop_todo.yml +41 -33
  9. data/CHANGELOG.md +243 -37
  10. data/CONTRIBUTING.md +1 -1
  11. data/Dangerfile +3 -0
  12. data/Gemfile +11 -7
  13. data/Guardfile +4 -2
  14. data/LICENSE +1 -1
  15. data/README.md +272 -19
  16. data/Rakefile +9 -11
  17. data/UPGRADING.md +35 -0
  18. data/bench/serializing.rb +7 -0
  19. data/grape-entity.gemspec +13 -8
  20. data/lib/grape-entity.rb +2 -0
  21. data/lib/grape_entity/condition/base.rb +37 -0
  22. data/lib/grape_entity/condition/block_condition.rb +23 -0
  23. data/lib/grape_entity/condition/hash_condition.rb +27 -0
  24. data/lib/grape_entity/condition/symbol_condition.rb +23 -0
  25. data/lib/grape_entity/condition.rb +35 -0
  26. data/lib/grape_entity/delegator/base.rb +7 -0
  27. data/lib/grape_entity/delegator/fetchable_object.rb +2 -0
  28. data/lib/grape_entity/delegator/hash_object.rb +4 -2
  29. data/lib/grape_entity/delegator/openstruct_object.rb +2 -0
  30. data/lib/grape_entity/delegator/plain_object.rb +2 -0
  31. data/lib/grape_entity/delegator.rb +14 -9
  32. data/lib/grape_entity/deprecated.rb +13 -0
  33. data/lib/grape_entity/entity.rb +192 -258
  34. data/lib/grape_entity/exposure/base.rb +138 -0
  35. data/lib/grape_entity/exposure/block_exposure.rb +31 -0
  36. data/lib/grape_entity/exposure/delegator_exposure.rb +13 -0
  37. data/lib/grape_entity/exposure/formatter_block_exposure.rb +27 -0
  38. data/lib/grape_entity/exposure/formatter_exposure.rb +32 -0
  39. data/lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb +83 -0
  40. data/lib/grape_entity/exposure/nesting_exposure/output_builder.rb +66 -0
  41. data/lib/grape_entity/exposure/nesting_exposure.rb +133 -0
  42. data/lib/grape_entity/exposure/represent_exposure.rb +50 -0
  43. data/lib/grape_entity/exposure.rb +105 -0
  44. data/lib/grape_entity/options.rb +132 -0
  45. data/lib/grape_entity/version.rb +3 -1
  46. data/lib/grape_entity.rb +9 -2
  47. data/spec/grape_entity/entity_spec.rb +903 -184
  48. data/spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb +58 -0
  49. data/spec/grape_entity/exposure/represent_exposure_spec.rb +32 -0
  50. data/spec/grape_entity/exposure_spec.rb +102 -0
  51. data/spec/grape_entity/hash_spec.rb +91 -0
  52. data/spec/grape_entity/options_spec.rb +66 -0
  53. data/spec/spec_helper.rb +21 -2
  54. metadata +91 -18
  55. data/.travis.yml +0 -19
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grape_entity/condition/base'
4
+ require 'grape_entity/condition/block_condition'
5
+ require 'grape_entity/condition/hash_condition'
6
+ require 'grape_entity/condition/symbol_condition'
7
+
8
+ module Grape
9
+ class Entity
10
+ module Condition
11
+ class << self
12
+ def new_if(arg)
13
+ condition(false, arg)
14
+ end
15
+
16
+ def new_unless(arg)
17
+ condition(true, arg)
18
+ end
19
+
20
+ private
21
+
22
+ def condition(inverse, arg)
23
+ condition_klass =
24
+ case arg
25
+ when Hash then HashCondition
26
+ when Proc then BlockCondition
27
+ when Symbol then SymbolCondition
28
+ end
29
+
30
+ condition_klass.new(inverse, arg)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Grape
2
4
  class Entity
3
5
  module Delegator
@@ -11,6 +13,11 @@ module Grape
11
13
  def delegatable?(_attribute)
12
14
  true
13
15
  end
16
+
17
+ def accepts_options?
18
+ # Why not `arity > 1`? It might be negative https://ruby-doc.org/core-2.6.6/Method.html#method-i-arity
19
+ method(:delegate).arity != 1
20
+ end
14
21
  end
15
22
  end
16
23
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Grape
2
4
  class Entity
3
5
  module Delegator
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Grape
2
4
  class Entity
3
5
  module Delegator
4
6
  class HashObject < Base
5
- def delegate(attribute)
6
- object[attribute]
7
+ def delegate(attribute, hash_access: :to_sym)
8
+ object[attribute.send(hash_access)]
7
9
  end
8
10
  end
9
11
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Grape
2
4
  class Entity
3
5
  module Delegator
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Grape
2
4
  class Entity
3
5
  module Delegator
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'grape_entity/delegator/base'
2
4
  require 'grape_entity/delegator/hash_object'
3
5
  require 'grape_entity/delegator/openstruct_object'
@@ -8,15 +10,18 @@ module Grape
8
10
  class Entity
9
11
  module Delegator
10
12
  def self.new(object)
11
- if object.is_a?(Hash)
12
- HashObject.new object
13
- elsif defined?(OpenStruct) && object.is_a?(OpenStruct)
14
- OpenStructObject.new object
15
- elsif object.respond_to? :fetch, true
16
- FetchableObject.new object
17
- else
18
- PlainObject.new object
19
- end
13
+ delegator_klass =
14
+ if object.is_a?(Hash)
15
+ HashObject
16
+ elsif defined?(OpenStruct) && object.is_a?(OpenStruct)
17
+ OpenStructObject
18
+ elsif object.respond_to?(:fetch, true)
19
+ FetchableObject
20
+ else
21
+ PlainObject
22
+ end
23
+
24
+ delegator_klass.new(object)
20
25
  end
21
26
  end
22
27
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Grape
4
+ class Entity
5
+ class Deprecated < StandardError
6
+ def initialize(msg, spec)
7
+ message = "DEPRECATED #{spec}: #{msg}"
8
+
9
+ super(message)
10
+ end
11
+ end
12
+ end
13
+ end