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.
- checksums.yaml +5 -5
- data/.coveralls.yml +1 -0
- data/.github/dependabot.yml +20 -0
- data/.github/workflows/ci.yml +41 -0
- data/.gitignore +5 -1
- data/.rspec +2 -1
- data/.rubocop.yml +85 -2
- data/.rubocop_todo.yml +41 -33
- data/CHANGELOG.md +243 -37
- data/CONTRIBUTING.md +1 -1
- data/Dangerfile +3 -0
- data/Gemfile +11 -7
- data/Guardfile +4 -2
- data/LICENSE +1 -1
- data/README.md +272 -19
- data/Rakefile +9 -11
- data/UPGRADING.md +35 -0
- data/bench/serializing.rb +7 -0
- data/grape-entity.gemspec +13 -8
- data/lib/grape-entity.rb +2 -0
- data/lib/grape_entity/condition/base.rb +37 -0
- data/lib/grape_entity/condition/block_condition.rb +23 -0
- data/lib/grape_entity/condition/hash_condition.rb +27 -0
- data/lib/grape_entity/condition/symbol_condition.rb +23 -0
- data/lib/grape_entity/condition.rb +35 -0
- data/lib/grape_entity/delegator/base.rb +7 -0
- data/lib/grape_entity/delegator/fetchable_object.rb +2 -0
- data/lib/grape_entity/delegator/hash_object.rb +4 -2
- data/lib/grape_entity/delegator/openstruct_object.rb +2 -0
- data/lib/grape_entity/delegator/plain_object.rb +2 -0
- data/lib/grape_entity/delegator.rb +14 -9
- data/lib/grape_entity/deprecated.rb +13 -0
- data/lib/grape_entity/entity.rb +192 -258
- data/lib/grape_entity/exposure/base.rb +138 -0
- data/lib/grape_entity/exposure/block_exposure.rb +31 -0
- data/lib/grape_entity/exposure/delegator_exposure.rb +13 -0
- data/lib/grape_entity/exposure/formatter_block_exposure.rb +27 -0
- data/lib/grape_entity/exposure/formatter_exposure.rb +32 -0
- data/lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb +83 -0
- data/lib/grape_entity/exposure/nesting_exposure/output_builder.rb +66 -0
- data/lib/grape_entity/exposure/nesting_exposure.rb +133 -0
- data/lib/grape_entity/exposure/represent_exposure.rb +50 -0
- data/lib/grape_entity/exposure.rb +105 -0
- data/lib/grape_entity/options.rb +132 -0
- data/lib/grape_entity/version.rb +3 -1
- data/lib/grape_entity.rb +9 -2
- data/spec/grape_entity/entity_spec.rb +903 -184
- data/spec/grape_entity/exposure/nesting_exposure/nested_exposures_spec.rb +58 -0
- data/spec/grape_entity/exposure/represent_exposure_spec.rb +32 -0
- data/spec/grape_entity/exposure_spec.rb +102 -0
- data/spec/grape_entity/hash_spec.rb +91 -0
- data/spec/grape_entity/options_spec.rb +66 -0
- data/spec/spec_helper.rb +21 -2
- metadata +91 -18
- 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
|
data/lib/grape_entity/version.rb
CHANGED
data/lib/grape_entity.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
-
|
2
|
-
|
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'
|