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,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,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
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|