Skip to content

Commit

Permalink
[rb] fix complexity in options for rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
titusfortner committed Mar 25, 2021
1 parent 1b60931 commit 627c0de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
6 changes: 1 addition & 5 deletions rb/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Metrics/ClassLength:
Metrics/CyclomaticComplexity:
Max: 9
Exclude:
- 'lib/selenium/webdriver/remote/capabilities.rb'
- 'lib/selenium/webdriver/support/color.rb'

Metrics/MethodLength:
Expand All @@ -84,11 +83,8 @@ Metrics/ModuleLength:
Metrics/ParameterLists:
CountKeywordArgs: false

# TODO: Refactor Chrome::Bridge#create_capabilities
Metrics/PerceivedComplexity:
Max: 10
Exclude:
- 'lib/selenium/webdriver/remote/capabilities.rb'
Max: 9

Naming/FileName:
Exclude:
Expand Down
18 changes: 11 additions & 7 deletions rb/lib/selenium/webdriver/common/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,7 @@ def camelize?(_key)

def generate_as_json(value, camelize_keys: true)
if value.is_a?(Hash)
value.each_with_object({}) do |(key, val), hash|
next if val.respond_to?(:empty?) && val.empty?

camelize = camelize_keys ? camelize?(key) : false
key = convert_json_key(key, camelize: camelize)
hash[key] = generate_as_json(val, camelize_keys: camelize)
end
process_json_hash(value, camelize_keys)
elsif value.respond_to?(:as_json)
value.as_json
elsif value.is_a?(Array)
Expand All @@ -152,6 +146,16 @@ def generate_as_json(value, camelize_keys: true)
end
end

def process_json_hash(value, camelize_keys)
value.each_with_object({}) do |(key, val), hash|
next if val.respond_to?(:empty?) && val.empty?

camelize = camelize_keys ? camelize?(key) : false
key = convert_json_key(key, camelize: camelize)
hash[key] = generate_as_json(val, camelize_keys: camelize)
end
end

def convert_json_key(key, camelize: true)
key = key.to_s if key.is_a?(Symbol)
key = camel_case(key) if camelize
Expand Down

0 comments on commit 627c0de

Please sign in to comment.