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,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Grape
6
+ class Entity
7
+ class Options
8
+ extend Forwardable
9
+
10
+ attr_reader :opts_hash
11
+
12
+ def_delegators :opts_hash, :dig, :key?, :fetch, :[], :empty?
13
+
14
+ def initialize(opts_hash = {})
15
+ @opts_hash = opts_hash
16
+ @has_only = !opts_hash[:only].nil?
17
+ @has_except = !opts_hash[:except].nil?
18
+ @for_nesting_cache = {}
19
+ @should_return_key_cache = {}
20
+ end
21
+
22
+ def merge(new_opts)
23
+ return self if new_opts.empty?
24
+
25
+ merged = if new_opts.instance_of? Options
26
+ @opts_hash.merge(new_opts.opts_hash)
27
+ else
28
+ @opts_hash.merge(new_opts)
29
+ end
30
+
31
+ Options.new(merged)
32
+ end
33
+
34
+ def reverse_merge(new_opts)
35
+ return self if new_opts.empty?
36
+
37
+ merged = if new_opts.instance_of? Options
38
+ new_opts.opts_hash.merge(@opts_hash)
39
+ else
40
+ new_opts.merge(@opts_hash)
41
+ end
42
+
43
+ Options.new(merged)
44
+ end
45
+
46
+ def ==(other)
47
+ other_hash = other.is_a?(Options) ? other.opts_hash : other
48
+ @opts_hash == other_hash
49
+ end
50
+
51
+ def should_return_key?(key)
52
+ return true unless @has_only || @has_except
53
+
54
+ only = only_fields.nil? ||
55
+ only_fields.key?(key)
56
+ except = except_fields&.key?(key) &&
57
+ except_fields[key] == true
58
+ only && !except
59
+ end
60
+
61
+ def for_nesting(key)
62
+ @for_nesting_cache[key] ||= build_for_nesting(key)
63
+ end
64
+
65
+ def only_fields(for_key = nil)
66
+ return nil unless @has_only
67
+
68
+ @only_fields ||= @opts_hash[:only].each_with_object({}) do |attribute, allowed_fields|
69
+ build_symbolized_hash(attribute, allowed_fields)
70
+ end
71
+
72
+ only_for_given(for_key, @only_fields)
73
+ end
74
+
75
+ def except_fields(for_key = nil)
76
+ return nil unless @has_except
77
+
78
+ @except_fields ||= @opts_hash[:except].each_with_object({}) do |attribute, allowed_fields|
79
+ build_symbolized_hash(attribute, allowed_fields)
80
+ end
81
+
82
+ only_for_given(for_key, @except_fields)
83
+ end
84
+
85
+ def with_attr_path(part)
86
+ return yield unless part
87
+
88
+ stack = (opts_hash[:attr_path] ||= [])
89
+ stack.push part
90
+ result = yield
91
+ stack.pop
92
+ result
93
+ end
94
+
95
+ private
96
+
97
+ def build_for_nesting(key)
98
+ Options.new(
99
+ opts_hash.dup.reject { |current_key| current_key == :collection }.merge(
100
+ root: nil,
101
+ only: only_fields(key),
102
+ except: except_fields(key),
103
+ attr_path: opts_hash[:attr_path]
104
+ )
105
+ )
106
+ end
107
+
108
+ def build_symbolized_hash(attribute, hash)
109
+ case attribute
110
+ when Hash
111
+ attribute.each do |attr, nested_attrs|
112
+ hash[attr.to_sym] = build_symbolized_hash(nested_attrs, {})
113
+ end
114
+ when Array
115
+ return attribute.each { |x| build_symbolized_hash(x, {}) }
116
+ else
117
+ hash[attribute.to_sym] = true
118
+ end
119
+
120
+ hash
121
+ end
122
+
123
+ def only_for_given(key, fields)
124
+ if key && fields[key].is_a?(Array)
125
+ fields[key]
126
+ elsif key.nil?
127
+ fields
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module GrapeEntity
2
- VERSION = '0.4.8'
4
+ VERSION = '0.10.2'
3
5
  end
data/lib/grape_entity.rb CHANGED
@@ -1,5 +1,12 @@
1
- require 'active_support'
2
- require 'active_support/core_ext'
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/version'
4
+ require 'active_support/core_ext/string/inflections'
5
+ require 'active_support/core_ext/hash/reverse_merge'
6
+ require 'active_support/core_ext/object/try'
3
7
  require 'grape_entity/version'
4
8
  require 'grape_entity/entity'
5
9
  require 'grape_entity/delegator'
10
+ require 'grape_entity/exposure'
11
+ require 'grape_entity/options'
12
+ require 'grape_entity/deprecated'