diff options
author | Markus Roberts <Markus@reality.com> | 2010-07-09 18:06:06 -0700 |
---|---|---|
committer | Markus Roberts <Markus@reality.com> | 2010-07-09 18:06:06 -0700 |
commit | 81e283b28cdd91d259e3b60687aee7ea66e9d05d (patch) | |
tree | e3c7b6e4b41cc219f75a3ae7d1294652ead6f268 /lib/puppet/rails | |
parent | e8cf06336b64491a2dd7538a06651e0caaf6a48d (diff) | |
download | puppet-81e283b28cdd91d259e3b60687aee7ea66e9d05d.tar.gz puppet-81e283b28cdd91d259e3b60687aee7ea66e9d05d.tar.xz puppet-81e283b28cdd91d259e3b60687aee7ea66e9d05d.zip |
Code smell: Line modifiers are preferred to one-line blocks.
* Replaced 6 occurances of (while .*?) *do$ with
The do is unneeded in the block header form and causes problems
with the block-to-one-line transformation.
3 Examples:
The code:
while line = f.gets do
becomes:
while line = f.gets
The code:
while line = shadow.gets do
becomes:
while line = shadow.gets
The code:
while wrapper = zeros.pop do
becomes:
while wrapper = zeros.pop
* Replaced 19 occurances of ((if|unless) .*?) *then$ with
The then is unneeded in the block header form and causes problems
with the block-to-one-line transformation.
3 Examples:
The code:
if f = test_files_for(failed).find { |f| failed_trace =~ Regexp.new(f) } then
becomes:
if f = test_files_for(failed).find { |f| failed_trace =~ Regexp.new(f) }
The code:
unless defined?(@spec_command) then
becomes:
unless defined?(@spec_command)
The code:
if c == ?\n then
becomes:
if c == ?\n
* Replaced 758 occurances of
((?:if|unless|while|until) .*)
(.*)
end
with
The one-line form is preferable provided:
* The condition is not used to assign a variable
* The body line is not already modified
* The resulting line is not too long
3 Examples:
The code:
if Puppet.features.libshadow?
has_feature :manages_passwords
end
becomes:
has_feature :manages_passwords if Puppet.features.libshadow?
The code:
unless (defined?(@current_pool) and @current_pool)
@current_pool = process_zpool_data(get_pool_data)
end
becomes:
@current_pool = process_zpool_data(get_pool_data) unless (defined?(@current_pool) and @current_pool)
The code:
if Puppet[:trace]
puts detail.backtrace
end
becomes:
puts detail.backtrace if Puppet[:trace]
Diffstat (limited to 'lib/puppet/rails')
-rw-r--r-- | lib/puppet/rails/benchmark.rb | 12 | ||||
-rw-r--r-- | lib/puppet/rails/database/001_add_created_at_to_all_tables.rb | 8 | ||||
-rw-r--r-- | lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb | 8 | ||||
-rw-r--r-- | lib/puppet/rails/database/003_add_environment_to_host.rb | 8 | ||||
-rw-r--r-- | lib/puppet/rails/database/schema.rb | 4 | ||||
-rw-r--r-- | lib/puppet/rails/resource.rb | 12 |
6 files changed, 13 insertions, 39 deletions
diff --git a/lib/puppet/rails/benchmark.rb b/lib/puppet/rails/benchmark.rb index 72ffa7443..35e6fdcef 100644 --- a/lib/puppet/rails/benchmark.rb +++ b/lib/puppet/rails/benchmark.rb @@ -16,9 +16,7 @@ module Puppet::Rails::Benchmark end def debug_benchmark(message) - unless Puppet::Rails::TIME_DEBUG - return yield - end + return yield unless Puppet::Rails::TIME_DEBUG railsmark(message) { yield } end @@ -28,9 +26,7 @@ module Puppet::Rails::Benchmark # These are always low-level debugging so we only # print them if time_debug is enabled. def accumulate_benchmark(message, label) - unless time_debug? - return yield - end + return yield unless time_debug? $benchmarks[:accumulated][message] ||= Hash.new(0) $benchmarks[:accumulated][message][label] += Benchmark.realtime { yield } @@ -40,9 +36,7 @@ module Puppet::Rails::Benchmark def log_accumulated_marks(message) return unless time_debug? - if $benchmarks[:accumulated].empty? or $benchmarks[:accumulated][message].nil? or $benchmarks[:accumulated][message].empty? - return - end + return if $benchmarks[:accumulated].empty? or $benchmarks[:accumulated][message].nil? or $benchmarks[:accumulated][message].empty? $benchmarks[:accumulated][message].each do |label, value| Puppet.debug(message + ("(#{label})") + (" in %0.2f seconds" % value)) diff --git a/lib/puppet/rails/database/001_add_created_at_to_all_tables.rb b/lib/puppet/rails/database/001_add_created_at_to_all_tables.rb index 138f9f151..65432a6b7 100644 --- a/lib/puppet/rails/database/001_add_created_at_to_all_tables.rb +++ b/lib/puppet/rails/database/001_add_created_at_to_all_tables.rb @@ -1,17 +1,13 @@ class AddCreatedAtToAllTables < ActiveRecord::Migration def self.up ActiveRecord::Base.connection.tables.each do |t| - unless ActiveRecord::Base.connection.columns(t).collect {|c| c.name}.include?("created_at") - add_column t.to_s, :created_at, :datetime - end + add_column t.to_s, :created_at, :datetime unless ActiveRecord::Base.connection.columns(t).collect {|c| c.name}.include?("created_at") end end def self.down ActiveRecord::Base.connection.tables.each do |t| - unless ActiveRecord::Base.connection.columns(t).collect {|c| c.name}.include?("created_at") - remove_column t.to_s, :created_at - end + remove_column t.to_s, :created_at unless ActiveRecord::Base.connection.columns(t).collect {|c| c.name}.include?("created_at") end end end diff --git a/lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb b/lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb index 050ca7f43..c1e60db14 100644 --- a/lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb +++ b/lib/puppet/rails/database/002_remove_duplicated_index_on_all_tables.rb @@ -1,17 +1,13 @@ class RemoveDuplicatedIndexOnAllTables < ActiveRecord::Migration def self.up ActiveRecord::Base.connection.tables.each do |t| - if ActiveRecord::Base.connection.indexes(t).collect {|c| c.columns}.include?("id") - remove_index t.to_s, :id - end + remove_index t.to_s, :id if ActiveRecord::Base.connection.indexes(t).collect {|c| c.columns}.include?("id") end end def self.down ActiveRecord::Base.connection.tables.each do |t| - unless ActiveRecord::Base.connection.indexes(t).collect {|c| c.columns}.include?("id") - add_index t.to_s, :id, :integer => true - end + add_index t.to_s, :id, :integer => true unless ActiveRecord::Base.connection.indexes(t).collect {|c| c.columns}.include?("id") end end end diff --git a/lib/puppet/rails/database/003_add_environment_to_host.rb b/lib/puppet/rails/database/003_add_environment_to_host.rb index 4ab2fbc63..95f036d11 100644 --- a/lib/puppet/rails/database/003_add_environment_to_host.rb +++ b/lib/puppet/rails/database/003_add_environment_to_host.rb @@ -1,13 +1,9 @@ class AddEnvironmentToHost < ActiveRecord::Migration def self.up - unless ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") - add_column :hosts, :environment, :string - end + add_column :hosts, :environment, :string unless ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") end def self.down - if ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") - remove_column :hosts, :environment - end + remove_column :hosts, :environment if ActiveRecord::Base.connection.columns(:hosts).collect {|c| c.name}.include?("environment") end end diff --git a/lib/puppet/rails/database/schema.rb b/lib/puppet/rails/database/schema.rb index a2477f2e6..8cb7350eb 100644 --- a/lib/puppet/rails/database/schema.rb +++ b/lib/puppet/rails/database/schema.rb @@ -52,9 +52,7 @@ class Puppet::Rails::Schema end # Oracle automatically creates a primary key index - if Puppet[:dbadapter] != "oracle_enhanced" - add_index :puppet_tags, :id, :integer => true - end + add_index :puppet_tags, :id, :integer => true if Puppet[:dbadapter] != "oracle_enhanced" create_table :hosts do |t| t.column :name, :string, :null => false diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb index f2c41c17e..0a9172a42 100644 --- a/lib/puppet/rails/resource.rb +++ b/lib/puppet/rails/resource.rb @@ -102,15 +102,11 @@ class Puppet::Rails::Resource < ActiveRecord::Base def merge_attributes(resource) args = self.class.rails_resource_initial_args(resource) args.each do |param, value| - unless resource[param] == value - self[param] = value - end + self[param] = value unless resource[param] == value end # Handle file specially - if (resource.file and (!resource.file or self.file != resource.file)) - self.file = resource.file - end + self.file = resource.file if (resource.file and (!resource.file or self.file != resource.file)) end def merge_parameters(resource) @@ -138,9 +134,7 @@ class Puppet::Rails::Resource < ActiveRecord::Base db_params.each do |name, value_hashes| values = value_hashes.collect { |v| v['value'] } - unless value_compare(catalog_params[name], values) - value_hashes.each { |v| deletions << v['id'] } - end + value_hashes.each { |v| deletions << v['id'] } unless value_compare(catalog_params[name], values) end # Perform our deletions. |