Skip to content

Commit c359488

Browse files
committed
Required tuple
1 parent eb5484c commit c359488

14 files changed

+117
-34
lines changed

.rubocop.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
AllCops:
22
Exclude:
33
- vendor/**/*
4+
TargetRubyVersion: 2.4
45

56
inherit_from: .rubocop_todo.yml
7+
8+
Layout/EmptyLinesAroundArguments:
9+
Enabled: false
10+
11+
Layout/IndentHash:
12+
EnforcedStyle: consistent
13+
14+
Metrics/BlockLength:
15+
Exclude:
16+
- spec/**/*
17+
18+
Metrics/ClassLength:
19+
Max: 300
20+
21+
Metrics/LineLength:
22+
Max: 120
23+
Exclude:
24+
- spec/**/*
25+
26+
Metrics/MethodLength:
27+
Exclude:
28+
- spec/**/*
29+
30+
Naming:
31+
Enabled: false
32+
33+
Style/RegexpLiteral:
34+
Enabled: false

.rubocop_todo.yml

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2017-11-19 22:40:01 +0200 using RuboCop version 0.51.0.
3+
# on 2018-08-21 15:46:20 +0300 using RuboCop version 0.58.2.
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: 2
9+
# Offense count: 3
1010
Metrics/AbcSize:
11-
Max: 34
12-
13-
# Offense count: 8
14-
# Configuration parameters: CountComments, ExcludedMethods.
15-
Metrics/BlockLength:
16-
Max: 132
11+
Max: 39
1712

1813
# Offense count: 2
1914
Metrics/CyclomaticComplexity:
20-
Max: 15
15+
Max: 19
2116

22-
# Offense count: 67
17+
# Offense count: 2
2318
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
2419
# URISchemes: http, https
2520
Metrics/LineLength:
26-
Max: 160
21+
Max: 130
2722

2823
# Offense count: 2
2924
# Configuration parameters: CountComments.
3025
Metrics/MethodLength:
31-
Max: 36
26+
Max: 35
3227

3328
# Offense count: 1
3429
# Configuration parameters: CountKeywordArgs.
@@ -37,14 +32,7 @@ Metrics/ParameterLists:
3732

3833
# Offense count: 2
3934
Metrics/PerceivedComplexity:
40-
Max: 18
41-
42-
# Offense count: 1
43-
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
44-
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
45-
Naming/FileName:
46-
Exclude:
47-
- 'lib/grape-swagger-representable.rb'
35+
Max: 21
4836

4937
# Offense count: 2
5038
Style/Documentation:

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
24

35
# Specify your gem's dependencies in grape-swagger-representable.gemspec
46
gemspec
57

6-
gem 'grape-swagger', github: 'ruby-grape/grape-swagger'
8+
gem 'grape-swagger', path: '../grape-swagger'

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'bundler/gem_tasks'
24
require 'rspec/core/rake_task'
35

bin/pry

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

34
require 'bundler/setup'
45
require 'grape-swagger/representable'

grape-swagger-representable.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
# frozen_string_literal: true
12

2-
lib = File.expand_path('../lib', __FILE__)
3+
lib = File.expand_path('lib', __dir__)
34
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
45
require 'grape-swagger/representable/version'
56

lib/grape-swagger-representable.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
# frozen_string_literal: true
2+
13
require 'grape-swagger/representable'

lib/grape-swagger/representable.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'grape-swagger'
24
require 'representable'
35

lib/grape-swagger/representable/parser.rb

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module GrapeSwagger
24
module Representable
35
class Parser
@@ -75,33 +77,60 @@ def representer_mapping(representer, documentation, property, is_a_collection =
7577
}
7678
end
7779
else
78-
attributes = parse_representer(representer)
79-
attributes = attributes.deep_merge!(parse_representer(nested)) if nested
80+
attributes, required = combine(representer, nested)
8081

8182
if is_a_collection
8283
{
8384
type: :array,
84-
items: {
85+
items: with_required({
8586
type: :object,
8687
properties: attributes
87-
},
88+
}, required),
8889
description: documentation[:desc] || property[:desc] || ''
8990
}
9091
else
91-
{
92+
with_required({
9293
type: :object,
9394
properties: attributes,
9495
description: documentation[:desc] || property[:desc] || ''
95-
}
96+
}, required)
9697
end
9798
end
9899
end
99100

100101
def parse_representer(representer)
101-
representer.map.each_with_object({}) do |value, property|
102+
properties = representer.map.each_with_object({}) do |value, property|
102103
property_name = value[:as].try(:call) || value.name
103104
property[property_name] = parse_representer_property(value)
104105
end
106+
107+
required = representer.map
108+
.select { |value| value[:documentation] && value[:documentation][:required] }
109+
.map { |value| value[:as] || value.name }
110+
111+
[properties, required]
112+
end
113+
114+
def combine(representer, nested)
115+
attributes, required = parse_representer(representer)
116+
return [attributes, required] unless nested
117+
118+
nested_attributes, nested_required = parse_representer(nested) if nested
119+
final_attributes = attributes.deep_merge!(nested_attributes)
120+
121+
overrided = (attributes.keys & nested_attributes.keys)
122+
123+
final_required = (required + nested_required)
124+
.uniq
125+
.select { |k| (overrided.include?(k) && nested_required.include?(k)) || !overrided.include?(k) }
126+
127+
[final_attributes, final_required]
128+
end
129+
130+
def with_required(hash, required)
131+
return hash if required.empty?
132+
hash[:required] = required
133+
hash
105134
end
106135
end
107136
end
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
# frozen_string_literal: true
2+
13
module GrapeSwagger
24
module Representable
3-
VERSION = '0.1.6'.freeze
5+
VERSION = '0.1.6'
46
end
57
end

0 commit comments

Comments
 (0)