Skip to content

Commit

Permalink
Automatically correct with Rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
andrykonchin committed Dec 31, 2023
1 parent 5e3bfc1 commit 583347c
Show file tree
Hide file tree
Showing 37 changed files with 148 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if RUN_COVERAGE
track_files '**/*.rb'

if ALL_FORMATTERS
command_name "#{ENV['GITHUB_WORKFLOW']} Job #{ENV['GITHUB_RUN_ID']}:#{ENV['GITHUB_RUN_NUMBER']}"
command_name "#{ENV.fetch('GITHUB_WORKFLOW')} Job #{ENV.fetch('GITHUB_RUN_ID')}:#{ENV.fetch('GITHUB_RUN_NUMBER')}"
else
formatter SimpleCov::Formatter::HTMLFormatter
end
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ gemspec
# All CI jobs must use a discrete Gemfile located at gemfiles/*.gemfile. They will not use this Gemfile
if ENV['CI'].nil?
ruby_version = Gem::Version.new(RUBY_VERSION)
minimum_version = ->(version, engine = 'ruby') { ruby_version >= Gem::Version.new(version) && RUBY_ENGINE == engine }
minimum_version = ->(version, engine = 'ruby') { ruby_version >= Gem::Version.new(version) && engine == RUBY_ENGINE }
committing = minimum_version.call('2.4')
linting = minimum_version.call('2.7')
coverage = minimum_version.call('2.7')
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ For example, to configure AWS access:
Create `config/initializers/aws.rb` as follows:

```ruby
Aws.config.update({
region: 'us-west-2',
Aws.config.update(
region: 'us-west-2',
credentials: Aws::Credentials.new('REPLACE_WITH_ACCESS_KEY_ID', 'REPLACE_WITH_SECRET_ACCESS_KEY'),
})
)
```

Alternatively, if you don't want Aws connection settings to be
Expand Down Expand Up @@ -740,7 +740,7 @@ If Dynamoid keeps `nil` value attributes `eq`/`ne` operators should be
used instead:

```ruby
Address.where('postcode': nil)
Address.where(postcode: nil)
Address.where('postcode.ne': nil)
```

Expand Down Expand Up @@ -914,8 +914,8 @@ If you have a range index, Dynamoid provides a number of additional
other convenience methods to make your life a little easier:

```ruby
User.where("created_at.gt": DateTime.now - 1.day).all
User.where("created_at.lt": DateTime.now - 1.day).all
User.where('created_at.gt': DateTime.now - 1.day).all
User.where('created_at.lt': DateTime.now - 1.day).all
```

It also supports `gte` and `lte`. Turning those into symbols and
Expand Down Expand Up @@ -1295,7 +1295,7 @@ end

In addition, the first test for each model may fail if the relevant models are not included in `included_models`. This can be fixed by adding this line before the `DynamoidReset` module:
```ruby
Dir[File.join(Dynamoid::Config.models_dir, '**/*.rb')].each { |file| require file }
Dir[File.join(Dynamoid::Config.models_dir, '**/*.rb')].sort.each { |file| require file }
```
Note that this will require _all_ models in your models folder - you can also explicitly require only certain models if you would prefer to.

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ desc 'alias test task to spec'
task test: :spec

ruby_version = Gem::Version.new(RUBY_VERSION)
minimum_version = ->(version, engine = 'ruby') { ruby_version >= Gem::Version.new(version) && RUBY_ENGINE == engine }
minimum_version = ->(version, engine = 'ruby') { ruby_version >= Gem::Version.new(version) && engine == RUBY_ENGINE }
linting = minimum_version.call('2.7')
def rubocop_task(warning)
desc 'rubocop task stub'
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/adapter_plugin/aws_sdk_v3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Dynamoid
module AdapterPlugin
# The AwsSdkV3 adapter provides support for the aws-sdk version 2 for ruby.

# Note: Don't use keyword arguments in public methods as far as method
# NOTE: Don't use keyword arguments in public methods as far as method
# calls on adapter are delegated to the plugin.
#
# There are breaking changes in Ruby related to delegating keyword
Expand Down
4 changes: 2 additions & 2 deletions lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def call
gs_indexes = options[:global_secondary_indexes]

key_schema = {
hash_key_schema: { key => (options[:hash_key_type] || :string) },
hash_key_schema: { key => options[:hash_key_type] || :string },
range_key_schema: options[:range_key]
}
attribute_definitions = build_all_attribute_definitions(
Expand Down Expand Up @@ -69,7 +69,7 @@ def call
end
end
resp = client.create_table(client_opts)
options[:sync] = true if !options.key?(:sync) && ls_indexes.present? || gs_indexes.present?
options[:sync] = true if (!options.key?(:sync) && ls_indexes.present?) || gs_indexes.present?

if options[:sync]
status = PARSE_TABLE_STATUS.call(resp, :table_description)
Expand Down
3 changes: 1 addition & 2 deletions lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ def set(values)
else
# delete explicitly attributes if assigned nil value and configured
# to not store nil values
values_to_update = values_sanitized.select { |_, v| !v.nil? }
values_to_update = values_sanitized.reject { |_, v| v.nil? }
values_to_delete = values_sanitized.select { |_, v| v.nil? }

@updates.merge!(values_to_update)
@deletions.merge!(values_to_delete)
end

end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def association(type, name, options = {})
@associations[:"#{name}_ids"] ||= Dynamoid::Associations.const_get(type.to_s.camelcase).new(self, name, options)
end

define_method("#{name}=".to_sym) do |objects|
define_method(:"#{name}=") do |objects|
@associations[:"#{name}_ids"] ||= Dynamoid::Associations.const_get(type.to_s.camelcase).new(self, name, options)
@associations[:"#{name}_ids"].setter(objects)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/criteria/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def field_condition(key, value_before_type_casting)
raise Dynamoid::Errors::Error, "Unsupported operator #{operator} in #{key}"
end

condition = \
condition =
case operator
# NULL/NOT_NULL operators don't have parameters
# So { null: true } means NULL check and { null: false } means NOT_NULL one
Expand Down
4 changes: 2 additions & 2 deletions lib/dynamoid/criteria/nonexistent_fields_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def warning_message
fields_list = @nonexistent_fields.map { |s| "`#{s}`" }.join(', ')
count = @nonexistent_fields.size

'where conditions contain nonexistent' \
" field #{'name'.pluralize(count)} #{fields_list}"
'where conditions contain nonexistent ' \
"field #{'name'.pluralize(count)} #{fields_list}"
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def attribute_will_change!(name)

begin
value = read_attribute(name)
value = value.duplicable? ? value.clone : value
value = value.clone if value.duplicable?
rescue TypeError, NoMethodError
end

Expand Down
4 changes: 2 additions & 2 deletions lib/dynamoid/dumping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def process(value)
# datetime -> integer/string
class DateTimeDumper < Base
def process(value)
!value.nil? ? format_datetime(value, @options) : nil
value.nil? ? nil : format_datetime(value, @options)
end

private
Expand All @@ -237,7 +237,7 @@ def format_datetime(value, options)
# date -> integer/string
class DateDumper < Base
def process(value)
!value.nil? ? format_date(value, @options) : nil
value.nil? ? nil : format_date(value, @options)
end

private
Expand Down
12 changes: 5 additions & 7 deletions lib/dynamoid/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,8 @@ def timestamps_enabled?

# @private
def generated_methods
@generated_methods ||= begin
Module.new.tap do |mod|
include(mod)
end
@generated_methods ||= Module.new.tap do |mod|
include(mod)
end
end
end
Expand Down Expand Up @@ -362,7 +360,7 @@ def set_expires_field
seconds = options[:after]

if self[name].blank?
send("#{name}=", Time.now.to_i + seconds)
send(:"#{name}=", Time.now.to_i + seconds)
end
end
end
Expand All @@ -374,12 +372,12 @@ def set_inheritance_field

type = self.class.inheritance_field
if self.class.attributes[type] && send(type).nil?
send("#{type}=", self.class.sti_name)
send(:"#{type}=", self.class.sti_name)
end
end

def attribute_is_present_on_model?(attribute_name)
setter = "#{attribute_name}=".to_sym
setter = :"#{attribute_name}="
respond_to?(setter)
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/dynamoid/fields/declare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def generate_instance_methods

@source.generated_methods.module_eval do
define_method(name) { read_attribute(name) }
define_method("#{name}?") do
define_method(:"#{name}?") do
value = read_attribute(name)
case value
when true then true
Expand All @@ -57,8 +57,8 @@ def generate_instance_methods
!value.nil?
end
end
define_method("#{name}=") { |value| write_attribute(name, value) }
define_method("#{name}_before_type_cast") { read_attribute_before_type_cast(name) }
define_method(:"#{name}=") { |value| write_attribute(name, value) }
define_method(:"#{name}_before_type_cast") { read_attribute_before_type_cast(name) }
end
end

Expand All @@ -70,9 +70,9 @@ def generate_instance_methods_for_alias

@source.generated_methods.module_eval do
alias_method alias_name, name
alias_method "#{alias_name}=", "#{name}="
alias_method "#{alias_name}?", "#{name}?"
alias_method "#{alias_name}_before_type_cast", "#{name}_before_type_cast"
alias_method :"#{alias_name}=", :"#{name}="
alias_method :"#{alias_name}?", :"#{name}?"
alias_method :"#{alias_name}_before_type_cast", :"#{name}_before_type_cast"
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/finders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def method_missing(method, *args)
chain = Dynamoid::Criteria::Chain.new(self)
chain = chain.where({}.tap { |h| attributes.each_with_index { |attr, index| h[attr.to_sym] = args[index] } })

if finder =~ /all/
if finder.include?('all')
chain.all
else
chain.first
Expand Down
13 changes: 6 additions & 7 deletions lib/dynamoid/indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ def local_secondary_index(options = {})
index_range_key = options[:range_key]

unless index_range_key.present?
raise Dynamoid::Errors::InvalidIndex, 'A local secondary index '\
'requires a :range_key to be specified'
raise Dynamoid::Errors::InvalidIndex, 'A local secondary index ' \
'requires a :range_key to be specified'
end

if primary_range_key.present? && index_range_key == primary_range_key
raise Dynamoid::Errors::InvalidIndex, 'A local secondary index'\
' must use a different :range_key than the primary key'
raise Dynamoid::Errors::InvalidIndex, 'A local secondary index ' \
'must use a different :range_key than the primary key'
end

index_opts = options.merge(
Expand All @@ -159,8 +159,7 @@ def local_secondary_index(options = {})
# @param range [scalar] the range key used to declare an index (optional)
# @return [Dynamoid::Indexes::Index, nil] index object or nil if it isn't found
def find_index(hash, range = nil)
index = indexes[index_key(hash, range)]
index
indexes[index_key(hash, range)]
end

# Returns an index by its name
Expand Down Expand Up @@ -317,7 +316,7 @@ def validate_index_key(key_param, key_val)

key_dynamodb_type = dynamodb_type(key_field_attributes[:type], key_field_attributes)
if PERMITTED_KEY_DYNAMODB_TYPES.include?(key_dynamodb_type)
send("#{key_param}_schema=", { key_val => key_dynamodb_type })
send(:"#{key_param}_schema=", { key_val => key_dynamodb_type })
else
errors.add(key_param, "Index :#{key_param} is not a valid key type")
end
Expand Down
4 changes: 2 additions & 2 deletions lib/dynamoid/loadable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Loadable

def load(attrs)
attrs.each do |key, value|
send("#{key}=", value) if respond_to?("#{key}=")
send(:"#{key}=", value) if respond_to?(:"#{key}=")
end

self
Expand All @@ -27,7 +27,7 @@ def reload

self.attributes = self.class.find(hash_key, **options).attributes

@associations.values.each(&:reset)
@associations.each_value(&:reset)
@new_record = false

self
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ def delete

Dynamoid.adapter.delete(self.class.table_name, hash_key, options)

self.class.associations.each do |name, _options|
self.class.associations.each_key do |name|
send(name).disassociate_source
end

Expand Down
24 changes: 10 additions & 14 deletions lib/dynamoid/persistence/save.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@ def conditions_for_write
end

# Add an optimistic locking check if the lock_version column exists
if @model.class.attributes[:lock_version]
# Uses the original lock_version value from Dirty API
# in case user changed 'lock_version' manually
if @model.changes[:lock_version][0]
conditions[:if] ||= {}
conditions[:if][:lock_version] = @model.changes[:lock_version][0]
end
# Uses the original lock_version value from Dirty API
# in case user changed 'lock_version' manually
if @model.class.attributes[:lock_version] && (@model.changes[:lock_version][0])
conditions[:if] ||= {}
conditions[:if][:lock_version] = @model.changes[:lock_version][0]
end

conditions
Expand All @@ -93,13 +91,11 @@ def options_to_update_item
conditions[:if][@model.class.hash_key] = @model.hash_key

# Add an optimistic locking check if the lock_version column exists
if @model.class.attributes[:lock_version]
# Uses the original lock_version value from Dirty API
# in case user changed 'lock_version' manually
if @model.changes[:lock_version][0]
conditions[:if] ||= {}
conditions[:if][:lock_version] = @model.changes[:lock_version][0]
end
# Uses the original lock_version value from Dirty API
# in case user changed 'lock_version' manually
if @model.class.attributes[:lock_version] && (@model.changes[:lock_version][0])
conditions[:if] ||= {}
conditions[:if][:lock_version] = @model.changes[:lock_version][0]
end

options[:conditions] = conditions
Expand Down
2 changes: 1 addition & 1 deletion lib/dynamoid/persistence/update_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module UpdateValidations
def self.validate_attributes_exist(model_class, attributes)
model_attributes = model_class.attributes.keys

attributes.each do |attr_name, _|
attributes.each_key do |attr_name|
unless model_attributes.include?(attr_name)
raise Dynamoid::Errors::UnknownAttribute, "Attribute #{attr_name} does not exist in #{model_class}"
end
Expand Down
Loading

0 comments on commit 583347c

Please sign in to comment.