Skip to content

Commit

Permalink
Don't use interpolation if it's not required.
Browse files Browse the repository at this point in the history
Follow up to #11074, but for the core backend ruby files

Interpolation syntax can be error prone so don't use it when you can just
convert to string.

Prefer `variable.to_s` over `"#{variable}"`

Also, some places we use Array#join which returns a String already:
`"#{rate_detail.errors.full_messages.join(', ')}"`
Becomes:

`rate_detail.errors.full_messages.join(', ')`

Also, some places already created a message string but then wanted to interpolate
it for good measure:

```diff
msg = "Authentication failed for userid #{username}, unable to find user object in #{self.class.proper_name}"
-            _log.warn("#{msg}")
+            _log.warn(msg)
```

rubocop -a --only "Lint/StringConversionInInterpolation,Style/UnneededInterpolation" ...


(transferred from ManageIQ/manageiq@62e4980)
  • Loading branch information
jrafanie committed Sep 7, 2016
1 parent 93eb1d4 commit 382311a
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
10 changes: 5 additions & 5 deletions lib/miq_automation_engine/engine/miq_ae_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def self.deliver(*args)

if ws.nil? || ws.root.nil?
message = "Error delivering #{options[:attrs].inspect} for object [#{object_name}] with state [#{state}] to Automate: Empty Workspace"
_log.error("#{message}")
_log.error(message)
return nil
end

Expand All @@ -138,12 +138,12 @@ def self.deliver(*args)
options[:ae_state_previous] = YAML.dump(ws.current_state_info) unless ws.current_state_info.empty?

message = "Requeuing #{options.inspect} for object [#{object_name}] with state [#{options[:state]}] to Automate for delivery in [#{ae_retry_interval}] seconds"
_log.info("#{message}")
_log.info(message)
deliver_queue(options, :deliver_on => deliver_on)
else
if ae_result.casecmp('error').zero?
message = "Error delivering #{options[:attrs].inspect} for object [#{object_name}] with state [#{state}] to Automate: #{ws.root['ae_message']}"
_log.error("#{message}")
_log.error(message)
end
MiqAeEvent.process_result(ae_result, automate_attrs) if options[:instance_name].to_s.casecmp('EVENT').zero?
end
Expand All @@ -152,7 +152,7 @@ def self.deliver(*args)
return ws
rescue MiqAeException::Error => err
message = "Error delivering #{automate_attrs.inspect} for object [#{object_name}] with state [#{state}] to Automate: #{err.message}"
_log.error("#{message}")
_log.error(message)
ensure
vmdb_object.after_ae_delivery(ae_result.to_s.downcase) if vmdb_object.respond_to?(:after_ae_delivery)
end
Expand Down Expand Up @@ -212,7 +212,7 @@ def self.create_automation_attribute_name(object)

def self.create_automation_attribute_key(object, attr_name = nil)
klass_name = create_automation_attribute_class_name(object)
return "#{klass_name}" if automation_attribute_is_array?(klass_name)
return klass_name.to_s if automation_attribute_is_array?(klass_name)
attr_name ||= create_automation_attribute_name(object)
"#{klass_name}::#{attr_name}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/miq_automation_engine/models/miq_ae_method_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(filename = nil)
def define_instance_variables
@ae_method_obj['object']['attributes'].each do |k, v|
instance_variable_set("@#{k}", v)
singleton_class.class_eval { attr_accessor "#{k}" }
singleton_class.class_eval { attr_accessor k.to_s }
send("#{k}=", v)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def self.service_now_eccq_insert(server, username, password, agent, queue, topic
$log.error hserr.backtrace.join("\n")
raise
rescue => err
_log.error "#{err}"
_log.error err.to_s
$log.error err.backtrace.join("\n")
raise
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def set_dvs(portgroup, switch = portgroup)
end

def set_vlan(vlan)
set_option(:vlan, ["#{vlan}", "#{vlan}"])
set_option(:vlan, [vlan.to_s, vlan.to_s])
end

def get_folder_paths
Expand Down
2 changes: 1 addition & 1 deletion spec/miq_ae_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def value_match(value, xml_value)
end

it "#process_args_as_attributes with a hash with an object reference" do
result = @miq_obj.process_args_as_attributes("VmOrTemplate::vm" => "#{@vm.id}")
result = @miq_obj.process_args_as_attributes("VmOrTemplate::vm" => @vm.id.to_s)
expect(result["vm_id"]).to eq(@vm.id.to_s)
expect(result["vm"]).to be_kind_of(MiqAeMethodService::MiqAeServiceVmOrTemplate)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module MiqAeServiceManageIQ_Providers_CloudManager_ProvisionSpec
end

%w(template clone_to_vm).each do |provision_type|
it "#{provision_type}" do
it provision_type.to_s do
@miq_provision.update_attributes(:provision_type => provision_type)
expect(ae_svc_prov.target_type).to eq('vm')
end
Expand Down

0 comments on commit 382311a

Please sign in to comment.