Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style/PreferredHashMethods-20220909233727 #781

Merged
merged 5 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1717,22 +1717,6 @@ Style/PerlBackrefs:
Exclude:
- 'lib/apipie/extractor.rb'

# Offense count: 27
# This cop supports unsafe autocorrection (--autocorrect-all).
# Configuration parameters: EnforcedStyle.
# SupportedStyles: short, verbose
Style/PreferredHashMethods:
Exclude:
- 'lib/apipie/apipie_module.rb'
- 'lib/apipie/application.rb'
- 'lib/apipie/dsl_definition.rb'
- 'lib/apipie/extractor/writer.rb'
- 'lib/apipie/method_description.rb'
- 'lib/apipie/param_description.rb'
- 'lib/apipie/resource_description.rb'
- 'lib/apipie/validator.rb'
- 'spec/lib/param_group_spec.rb'

# Offense count: 5
# This cop supports safe autocorrection (--autocorrect).
Style/Proc:
Expand Down
4 changes: 2 additions & 2 deletions lib/apipie/apipie_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ def self.api_base_url(version = nil)
end

def self.app_info_version_valid?(version)
version && self.configuration.app_info.has_key?(version)
version && self.configuration.app_info.key?(version)
end

def self.api_base_url_version_valid?(version)
version && self.configuration.api_base_url.has_key?(version)
version && self.configuration.api_base_url.key?(version)
end

def self.record(record)
Expand Down
10 changes: 5 additions & 5 deletions lib/apipie/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def add_param_group(controller, name, &block)

def get_param_group(controller, name)
key = "#{controller.name}##{name}"
if @param_groups.has_key?(key)
if @param_groups.key?(key)
return @param_groups[key]
else
raise "param group #{key} not defined"
Expand Down Expand Up @@ -198,7 +198,7 @@ def get_resource_description(resource, version = nil)
version = crumbs.first
end
version ||= Apipie.configuration.default_version
if @resource_descriptions.has_key?(version)
if @resource_descriptions.key?(version)
return @resource_descriptions[version][crumbs.last]
end
else
Expand Down Expand Up @@ -373,7 +373,7 @@ def active_dsl?
def get_resource_name(klass)
if klass.class == String
klass
elsif @controller_to_resource_id.has_key?(klass)
elsif @controller_to_resource_id.key?(klass)
@controller_to_resource_id[klass]
elsif Apipie.configuration.namespaced_resources? && klass.respond_to?(:controller_path)
return nil if klass == ActionController::Base
Expand Down Expand Up @@ -408,9 +408,9 @@ def translate(str, locale)
# Make sure that the version/resource_name/method_name are valid combination
# resource_name and method_name can be nil
def valid_search_args?(version, resource_name, method_name)
return false unless self.resource_descriptions.has_key?(version)
return false unless self.resource_descriptions.key?(version)
if resource_name
return false unless self.resource_descriptions[version].has_key?(resource_name)
return false unless self.resource_descriptions[version].key?(resource_name)
if method_name
resource_description = self.resource_descriptions[version][resource_name]
return false unless resource_description.valid_method_name?(method_name)
Expand Down
6 changes: 3 additions & 3 deletions lib/apipie/dsl_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,14 @@ def _apipie_define_validators(description)
if Apipie.configuration.validate_presence?
method_params.each do |_, param|
# check if required parameters are present
raise ParamMissing.new(param) if param.required && !params.has_key?(param.name)
raise ParamMissing.new(param) if param.required && !params.key?(param.name)
end
end

if Apipie.configuration.validate_value?
method_params.each do |_, param|
# params validations
param.validate(params[:"#{param.name}"]) if params.has_key?(param.name)
param.validate(params[:"#{param.name}"]) if params.key?(param.name)
end
end

Expand All @@ -272,7 +272,7 @@ def _apipie_define_validators(description)
@api_params ||= {}
method_params.each do |_, param|
# params processing
@api_params[param.as] = param.process_value(params[:"#{param.name}"]) if params.has_key?(param.name)
@api_params[param.as] = param.process_value(params[:"#{param.name}"]) if params.key?(param.name)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/apipie/extractor/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def ordered_call(call)
call = call.stringify_keys
ordered_call = OrderedHash.new
%w[title verb path versions query request_data response_data code show_in_doc recorded].each do |k|
next unless call.has_key?(k)
next unless call.key?(k)
ordered_call[k] = case call[k]
when ActiveSupport::HashWithIndifferentAccess
convert_file_value(call[k]).to_hash
Expand Down Expand Up @@ -134,8 +134,8 @@ def merge_old_new_examples
old_examples = self.load_recorded_examples
merged_examples = []
(new_examples.keys + old_examples.keys).uniq.each do |key|
if new_examples.has_key?(key)
if old_examples.has_key?(key)
if new_examples.key?(key)
if old_examples.key?(key)
records = deep_merge_examples(new_examples[key], old_examples[key])
else
records = new_examples[key]
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/method_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def initialize(method, resource, dsl_data)
@params_ordered = ParamDescription.unify(@params_ordered)
@headers = dsl_data[:headers]

@show = if dsl_data.has_key? :show
@show = if dsl_data.key? :show
dsl_data[:show]
else
true
Expand Down
8 changes: 4 additions & 4 deletions lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def initialize(method_description, name, validator, desc_or_options = nil, optio
@request_only = (@options[:only_in] == :request)
raise ArgumentError.new("'#{@options[:only_in]}' is not a valid value for :only_in") if (!@response_only && !@request_only) && @options[:only_in].present?

@show = if @options.has_key? :show
@show = if @options.key? :show
@options[:show]
else
true
Expand Down Expand Up @@ -212,7 +212,7 @@ def self.merge(target_params, source_params)
# action awareness is being inherited from ancestors (in terms of
# nested params)
def action_aware?
if @options.has_key?(:action_aware)
if @options.key?(:action_aware)
return @options[:action_aware]
elsif @parent
@parent.action_aware?
Expand All @@ -237,7 +237,7 @@ def as_action
# crate/update actions.
def action_awareness
if action_aware?
if !@options.has_key?(:allow_nil)
if !@options.key?(:allow_nil)
if @required
@allow_nil = false
else
Expand Down Expand Up @@ -268,7 +268,7 @@ def preformat_text(text)
end

def is_required?
if @options.has_key?(:required)
if @options.key?(:required)
if (@options[:required] == true) || (@options[:required] == false)
@options[:required]
else
Expand Down
2 changes: 1 addition & 1 deletion lib/apipie/resource_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def method_description(method_name)
end

def remove_method_description(method_name)
if @_methods.has_key?(method_name)
if @_methods.key?(method_name)
@_methods.delete(method_name)
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/apipie/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ def validate(value)
if @hash_params
@hash_params.each do |k, p|
if Apipie.configuration.validate_presence?
raise ParamMissing.new(p) if p.required && !value.has_key?(k)
raise ParamMissing.new(p) if p.required && !value.key?(k)
end
if Apipie.configuration.validate_value?
p.validate(value[k]) if value.has_key?(k)
p.validate(value[k]) if value.key?(k)
end
end
end
Expand All @@ -360,7 +360,7 @@ def validate(value)
def process_value(value)
if @hash_params && value
return @hash_params.each_with_object({}) do |(key, param), api_params|
if value.has_key?(key)
if value.key?(key)
api_params[param.as] = param.process_value(value[key])
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/lib/param_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@
end

it "should replace parameter name in a group when it comes from concern" do
expect(Apipie["overridden_concern_resources#update"].params.has_key?(:user)).to eq(true)
expect(Apipie["overridden_concern_resources#update"].params.key?(:user)).to eq(true)
end

it "shouldn't replace parameter name in a group redefined in the controller" do
expect(Apipie["overridden_concern_resources#create"].params.has_key?(:concern)).to eq(true)
expect(Apipie["overridden_concern_resources#create"].params.has_key?(:user)).to eq(false)
expect(Apipie["overridden_concern_resources#create"].params.key?(:concern)).to eq(true)
expect(Apipie["overridden_concern_resources#create"].params.key?(:user)).to eq(false)
end

it "shouldn't replace name of a parameter defined in the controller" do
expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:concern)).to eq(true)
expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:user)).to eq(false)
expect(Apipie["overridden_concern_resources#custom"].params.key?(:concern)).to eq(true)
expect(Apipie["overridden_concern_resources#custom"].params.key?(:user)).to eq(false)
end

end
Expand Down