Skip to content

Commit ef6026a

Browse files
authored
Merge branch 'master' into master
2 parents c2ce4c9 + 53b2502 commit ef6026a

20 files changed

+154
-100
lines changed

.rubocop.yml

+33-22
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,48 @@
1+
inherit_from: .rubocop_todo.yml
2+
13
AllCops:
4+
Exclude:
5+
- vendor/**/*
6+
- example/**/*
27
TargetRubyVersion: 2.4
3-
Include:
4-
- Dangerfile
58

9+
Layout/EmptyLinesAroundArguments:
10+
Enabled: false
11+
12+
Layout/IndentFirstHashElement:
13+
EnforcedStyle: consistent
14+
15+
Metrics/AbcSize:
16+
Max: 25
17+
18+
Metrics/BlockLength:
619
Exclude:
7-
- vendor/**/*
8-
- bin/**/*
9-
- Guardfile
20+
- spec/**/*
1021

11-
inherit_from: .rubocop_todo.yml
22+
Metrics/CyclomaticComplexity:
23+
Max: 10
1224

13-
Gemspec/RequiredRubyVersion:
14-
Enabled: false
25+
Metrics/ClassLength:
26+
Max: 300
1527

16-
Naming/FileName:
28+
Metrics/LineLength:
29+
Max: 120
1730
Exclude:
18-
- 'Gemfile'
19-
- 'Rakefile'
20-
- 'grape-entity.gemspec'
21-
- 'lib/grape-entity.rb'
31+
- spec/**/*
2232

23-
Style/Documentation:
24-
Enabled: false
33+
Metrics/MethodLength:
34+
Max: 26
35+
Exclude:
36+
- spec/**/*
2537

26-
Style/MultilineIfModifier:
27-
Enabled: false
38+
Metrics/PerceivedComplexity:
39+
Max: 11
2840

29-
Style/RaiseArgs:
41+
Naming:
3042
Enabled: false
3143

32-
Lint/BooleanSymbol:
33-
Exclude:
34-
- 'spec/grape_entity/exposure_spec.rb'
44+
Style/Documentation:
45+
Enabled: false
3546

36-
Lint/UnneededDisable:
47+
Style/RegexpLiteral:
3748
Enabled: false

.rubocop_todo.yml

+12-43
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,34 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2018-03-28 21:26:18 +0200 using RuboCop version 0.54.0.
3+
# on 2019-03-15 00:00:57 +0100 using RuboCop version 0.65.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
10-
Lint/AmbiguousBlockAssociation:
9+
# Offense count: 6
10+
Lint/BooleanSymbol:
1111
Exclude:
12-
- 'spec/grape_entity/exposure/represent_exposure_spec.rb'
13-
14-
# Offense count: 5
15-
Metrics/AbcSize:
16-
Max: 25
12+
- 'spec/grape_entity/exposure_spec.rb'
1713

18-
# Offense count: 39
19-
# Configuration parameters: CountComments, ExcludedMethods.
20-
Metrics/BlockLength:
21-
Max: 1632
2214

23-
# Offense count: 1
24-
# Configuration parameters: CountComments.
25-
Metrics/ClassLength:
26-
Max: 205
27-
28-
# Offense count: 1
29-
Metrics/CyclomaticComplexity:
30-
Max: 10
31-
32-
# Offense count: 6
33-
# Configuration parameters: CountComments.
34-
Metrics/MethodLength:
35-
Max: 26
15+
# Offense count: 4
16+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
17+
# URISchemes: http, https
18+
Metrics/LineLength:
19+
Max: 146
3620

3721
# Offense count: 1
38-
Metrics/PerceivedComplexity:
39-
Max: 11
40-
41-
# Offense count: 3
42-
# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
43-
# AllowedNames: io, id, to
44-
Naming/UncommunicativeMethodParamName:
22+
# Configuration parameters: EnforcedStyle.
23+
# SupportedStyles: inline, group
24+
Style/AccessModifierDeclarations:
4525
Exclude:
4626
- 'spec/grape_entity/entity_spec.rb'
4727

48-
# Offense count: 1
49-
Style/EvalWithLocation:
50-
Exclude:
51-
- 'lib/grape_entity/exposure/nesting_exposure/nested_exposures.rb'
52-
5328
# Offense count: 1
5429
# Cop supports --auto-correct.
5530
# Configuration parameters: IgnoredMethods.
5631
# IgnoredMethods: respond_to, define_method
5732
Style/SymbolProc:
5833
Exclude:
5934
- 'spec/grape_entity/entity_spec.rb'
60-
61-
# Offense count: 272
62-
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
63-
# URISchemes: http, https
64-
Metrics/LineLength:
65-
Max: 146

.travis.yml

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
sudo: false
2-
31
language: ruby
42

53
before_install:
6-
- gem update --system
74
- gem install bundler
85

96
after_success:
10-
# - coveralls
117
- bundle exec danger
128

139
rvm:
1410
- 2.4.5
15-
- 2.5.3
16-
- 2.6.0
11+
- 2.5.4
12+
- 2.6.2
13+
- ruby-head
14+
- jruby-head
1715

1816
matrix:
1917
fast_finish: true
2018

21-
include:
22-
- rvm: 2.3.8
23-
- rvm: ruby-head
24-
- rvm: jruby-head
25-
2619
allow_failures:
27-
- rvm: 2.3.8
2820
- rvm: ruby-head
2921
- rvm: jruby-head

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
#### Features
44

55
* Your contribution here.
6+
* [#319](https://github.com/ruby-grape/grape-entity/pull/319): Support hashes with string keys - [@mhenrixon](https://github.com/mhenrixon).
67
* [#300](https://github.com/ruby-grape/grape-entity/pull/300): Loosens activesupport to 3 - [@ericschultz](https://github.com/ericschultz).
78

89
#### Fixes
910

1011
* Your contribution here.
11-
* [#258](https://github.com/ruby-grape/grape-entity/pull/307): Allow exposures to call methods defined in modules included in an entity [@robertoz-01](https://github.com/robertoz-01).
12+
* [#307](https://github.com/ruby-grape/grape-entity/pull/307): Allow exposures to call methods defined in modules included in an entity [@robertoz-01](https://github.com/robertoz-01).
1213

1314
### 0.7.1 (2018-01-30)
1415

1516
#### Features
1617

17-
* [#292](https://github.com/ruby-grape/grape-entity/pull/297): Introduce `override` option for expose (fixes [#286](https://github.com/ruby-grape/grape-entity/issues/296)) - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
18+
* [#297](https://github.com/ruby-grape/grape-entity/pull/297): Introduce `override` option for expose (fixes [#286](https://github.com/ruby-grape/grape-entity/issues/296)) - [@DmitryTsepelev](https://github.com/DmitryTsepelev).
1819

1920
### 0.7.0 (2018-01-25)
2021

Gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source 'http://rubygems.org'
55
gemspec
66

77
group :development, :test do
8-
gem 'rubocop', '~> 0.61', require: false
8+
gem 'rubocop', '~> 0.65', require: false
99
end
1010

1111
group :test do

Guardfile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
# frozen_string_literal: true
2+
13
# A sample Guardfile
24
# More info at https://github.com/guard/guard#readme
35

46
guard 'rspec', version: 2 do
57
watch(%r{^spec/.+_spec\.rb$})
6-
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
8+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
79
watch(%r{^spec/support/shared_versioning_examples.rb$}) { |_m| 'spec/' }
8-
watch('spec/spec_helper.rb') { 'spec/' }
10+
watch('spec/spec_helper.rb') { 'spec/' }
911
end
1012

1113
guard 'bundler' do

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ class ExampleEntity < Grape::Entity
220220
end
221221
```
222222

223-
You have always access to the presented instance with `object`
223+
You always have access to the presented instance (`object`) and the top-level
224+
entity options (`options`).
224225

225226
```ruby
226227
class ExampleEntity < Grape::Entity
@@ -229,7 +230,7 @@ class ExampleEntity < Grape::Entity
229230
private
230231

231232
def formatted_value
232-
"+ X #{object.value}"
233+
"+ X #{object.value} #{options[:y]}"
233234
end
234235
end
235236
```

grape-entity.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
1414
s.description = 'Extracted from Grape, A Ruby framework for rapid API development with great conventions.'
1515
s.license = 'MIT'
1616

17-
s.required_ruby_version = '>= 2.3'
17+
s.required_ruby_version = '>= 2.4'
1818

1919
s.rubyforge_project = 'grape-entity'
2020

lib/grape_entity/delegator/fetchable_object.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Grape
44
class Entity
55
module Delegator
66
class FetchableObject < Base
7-
def delegate(attribute)
7+
def delegate(attribute, **)
88
object.fetch attribute
99
end
1010
end

lib/grape_entity/delegator/hash_object.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# frozen_string_literal: true
22

3+
require 'pry'
4+
35
module Grape
46
class Entity
57
module Delegator
68
class HashObject < Base
7-
def delegate(attribute)
8-
object[attribute]
9+
def delegate(attribute, hash_access: :to_sym)
10+
object[attribute.send(hash_access)]
911
end
1012
end
1113
end

lib/grape_entity/delegator/openstruct_object.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Grape
44
class Entity
55
module Delegator
66
class OpenStructObject < Base
7-
def delegate(attribute)
7+
def delegate(attribute, **)
88
object.send attribute
99
end
1010
end

lib/grape_entity/delegator/plain_object.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Grape
44
class Entity
55
module Delegator
66
class PlainObject < Base
7-
def delegate(attribute)
7+
def delegate(attribute, **)
88
object.send attribute
99
end
1010

lib/grape_entity/entity.rb

+39-3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,22 @@ def formatters
114114
end
115115

116116
attr_writer :formatters
117+
118+
def hash_access
119+
@hash_access ||= :to_sym
120+
end
121+
122+
def hash_access=(value)
123+
@hash_access =
124+
case value
125+
when :to_s, :str, :string
126+
:to_s
127+
when :to_sym, :sym, :symbol
128+
:to_sym
129+
else
130+
:to_sym
131+
end
132+
end
117133
end
118134

119135
@formatters = {}
@@ -168,10 +184,13 @@ def self.inherited(subclass)
168184
# @option options :documentation Define documenation for an exposed
169185
# field, typically the value is a hash with two fields, type and desc.
170186
# @option options :merge This option allows you to merge an exposed field to the root
187+
#
188+
# rubocop:disable Metrics/LineLength
171189
def self.expose(*args, &block)
172190
options = merge_options(args.last.is_a?(Hash) ? args.pop : {})
173191

174192
if args.size > 1
193+
175194
raise ArgumentError, 'You may not use the :as option on multi-attribute exposures.' if options[:as]
176195
raise ArgumentError, 'You may not use the :expose_nil on multi-attribute exposures.' if options.key?(:expose_nil)
177196
raise ArgumentError, 'You may not use block-setting on multi-attribute exposures.' if block_given?
@@ -191,6 +210,7 @@ def self.expose(*args, &block)
191210
@nesting_stack ||= []
192211
args.each { |attribute| build_exposure_for_attribute(attribute, @nesting_stack, options, block) }
193212
end
213+
# rubocop:enable Metrics/LineLength
194214

195215
def self.build_exposure_for_attribute(attribute, nesting_stack, options, block)
196216
exposure_list = nesting_stack.empty? ? root_exposures : nesting_stack.last.nested_exposures
@@ -291,6 +311,7 @@ def self.documentation
291311
#
292312
def self.format_with(name, &block)
293313
raise ArgumentError, 'You must pass a block for formatters' unless block_given?
314+
294315
formatters[name.to_sym] = block
295316
end
296317

@@ -454,8 +475,8 @@ def inspect
454475

455476
def initialize(object, options = {})
456477
@object = object
457-
@delegator = Delegator.new(object)
458478
@options = options.is_a?(Options) ? options : Options.new(options)
479+
@delegator = Delegator.new(object)
459480
end
460481

461482
def root_exposures
@@ -509,7 +530,7 @@ def delegate_attribute(attribute)
509530
if is_defined_in_entity?(attribute)
510531
send(attribute)
511532
else
512-
delegator.delegate(attribute)
533+
delegator.delegate(attribute, hash_access: self.class.hash_access)
513534
end
514535
end
515536

@@ -534,7 +555,22 @@ def to_xml(options = {})
534555

535556
# All supported options.
536557
OPTIONS = %i[
537-
rewrite as if unless using with proc documentation format_with safe attr_path if_extras unless_extras merge expose_nil override
558+
rewrite
559+
as
560+
if
561+
unless
562+
using
563+
with
564+
proc
565+
documentation
566+
format_with
567+
safe
568+
attr_path
569+
if_extras
570+
unless_extras
571+
merge
572+
expose_nil
573+
override
538574
].to_set.freeze
539575

540576
# Merges the given options with current block options.

0 commit comments

Comments
 (0)