From 80202e321c4247f8c59274834b21f30d84cc4ff1 Mon Sep 17 00:00:00 2001 From: dblock Date: Tue, 17 Nov 2015 14:30:16 -0500 Subject: [PATCH 1/2] Integrated RuboCop as part of the build. --- .rubocop.yml | 5 ++ .rubocop_todo.yml | 87 +++++++++++++++++++ CONTRIBUTING.md | 1 + Gemfile | 10 ++- Guardfile | 4 +- Rakefile | 14 ++- lib/virtus.rb | 9 +- lib/virtus/attribute.rb | 13 ++- lib/virtus/attribute/accessor.rb | 4 - lib/virtus/attribute/boolean.rb | 2 - lib/virtus/attribute/builder.rb | 22 ++--- lib/virtus/attribute/coercer.rb | 4 - lib/virtus/attribute/coercible.rb | 4 - lib/virtus/attribute/collection.rb | 11 +-- lib/virtus/attribute/default_value.rb | 2 - .../attribute/default_value/from_callable.rb | 3 - .../attribute/default_value/from_clonable.rb | 6 +- .../attribute/default_value/from_symbol.rb | 3 - lib/virtus/attribute/embedded_value.rb | 15 +--- lib/virtus/attribute/hash.rb | 18 ++-- lib/virtus/attribute/lazy_default.rb | 4 - lib/virtus/attribute/nullify_blank.rb | 4 - lib/virtus/attribute/strict.rb | 6 +- lib/virtus/attribute_set.rb | 6 +- lib/virtus/builder.rb | 13 +-- lib/virtus/builder/hook_context.rb | 6 +- lib/virtus/class_inclusions.rb | 5 -- lib/virtus/class_methods.rb | 4 +- lib/virtus/coercer.rb | 7 +- lib/virtus/configuration.rb | 17 ++-- lib/virtus/const_missing_extensions.rb | 6 +- lib/virtus/extensions.rb | 10 +-- lib/virtus/instance_methods.rb | 9 +- lib/virtus/model.rb | 9 -- lib/virtus/module_extensions.rb | 2 - lib/virtus/support/equalizer.rb | 11 +-- lib/virtus/support/options.rb | 7 +- lib/virtus/support/type_lookup.rb | 11 +-- lib/virtus/value_object.rb | 14 +-- spec/integration/attributes_attribute_spec.rb | 9 +- spec/integration/building_module_spec.rb | 24 ++--- .../collection_member_coercion_spec.rb | 28 +++--- spec/integration/custom_attributes_spec.rb | 2 - .../custom_collection_attributes_spec.rb | 8 +- spec/integration/default_values_spec.rb | 15 ++-- spec/integration/defining_attributes_spec.rb | 16 ++-- spec/integration/embedded_value_spec.rb | 15 ++-- spec/integration/extending_objects_spec.rb | 4 +- .../hash_attributes_coercion_spec.rb | 10 +-- spec/integration/inheritance_spec.rb | 2 +- spec/integration/injectible_coercers_spec.rb | 13 ++- .../mass_assignment_with_accessors_spec.rb | 9 +- spec/integration/overriding_virtus_spec.rb | 9 +- spec/integration/required_attributes_spec.rb | 8 +- .../struct_as_embedded_value_spec.rb | 2 +- spec/integration/using_modules_spec.rb | 6 +- ...lue_object_with_custom_constructor_spec.rb | 8 +- spec/integration/virtus/value_object_spec.rb | 8 +- spec/shared/constants_helpers.rb | 1 - spec/shared/freeze_method_behavior.rb | 2 +- spec/spec_helper.rb | 6 +- .../virtus/attribute/boolean/coerce_spec.rb | 2 +- .../attribute/class_methods/build_spec.rb | 14 +-- spec/unit/virtus/attribute/coerce_spec.rb | 6 +- .../attribute/coercible_predicate_spec.rb | 2 +- .../attribute/collection/coerce_spec.rb | 14 +-- spec/unit/virtus/attribute/comparison_spec.rb | 8 +- .../attribute/custom_collection_spec.rb | 2 +- spec/unit/virtus/attribute/defined_spec.rb | 2 +- .../attribute/embedded_value/coerce_spec.rb | 6 +- spec/unit/virtus/attribute/get_spec.rb | 4 +- .../hash/class_methods/build_spec.rb | 2 +- .../unit/virtus/attribute/hash/coerce_spec.rb | 26 +++--- .../virtus/attribute/lazy_predicate_spec.rb | 2 +- spec/unit/virtus/attribute/rename_spec.rb | 4 +- .../attribute/required_predicate_spec.rb | 2 +- .../attribute/set_default_value_spec.rb | 39 +++++++-- spec/unit/virtus/attribute/set_spec.rb | 2 +- .../attribute/value_coerced_predicate_spec.rb | 4 +- spec/unit/virtus/attribute_set/append_spec.rb | 32 +++---- .../define_reader_method_spec.rb | 14 +-- .../define_writer_method_spec.rb | 14 +-- spec/unit/virtus/attribute_set/each_spec.rb | 24 ++--- .../attribute_set/element_reference_spec.rb | 6 +- .../virtus/attribute_set/element_set_spec.rb | 38 ++++---- spec/unit/virtus/attribute_set/merge_spec.rb | 14 +-- spec/unit/virtus/attribute_set/reset_spec.rb | 24 ++--- spec/unit/virtus/attribute_spec.rb | 16 ++-- spec/unit/virtus/attributes_reader_spec.rb | 17 ++-- spec/unit/virtus/attributes_writer_spec.rb | 13 ++- .../virtus/class_methods/finalize_spec.rb | 4 +- spec/unit/virtus/class_methods/new_spec.rb | 15 ++-- spec/unit/virtus/element_reader_spec.rb | 8 +- spec/unit/virtus/element_writer_spec.rb | 8 +- spec/unit/virtus/freeze_spec.rb | 18 ++-- spec/unit/virtus/model_spec.rb | 20 ++--- spec/unit/virtus/module_spec.rb | 21 +++-- .../virtus/set_default_attributes_spec.rb | 12 +-- spec/unit/virtus/value_object_spec.rb | 36 ++++---- virtus.gemspec | 14 +-- 100 files changed, 546 insertions(+), 556 deletions(-) create mode 100644 .rubocop.yml create mode 100644 .rubocop_todo.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..04b22169 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,5 @@ +AllCops: + Exclude: + - vendor/**/* + +inherit_from: .rubocop_todo.yml diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 00000000..2c453a3c --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,87 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2015-11-17 14:29:53 -0500 using RuboCop version 0.35.1. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +# Configuration parameters: AllowSafeAssignment. +Lint/AssignmentInCondition: + Exclude: + - 'spec/spec_helper.rb' + +# Offense count: 3 +Metrics/AbcSize: + Max: 24 + +# Offense count: 4 +Metrics/CyclomaticComplexity: + Max: 9 + +# Offense count: 98 +# Configuration parameters: AllowURI, URISchemes. +Metrics/LineLength: + Max: 140 + +# Offense count: 6 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 20 + +# Offense count: 4 +Metrics/PerceivedComplexity: + Max: 12 + +# Offense count: 2 +Style/AccessorMethodName: + Exclude: + - 'lib/virtus/attribute/accessor.rb' + - 'lib/virtus/support/options.rb' + +# Offense count: 2 +Style/CaseEquality: + Exclude: + - 'lib/virtus.rb' + - 'lib/virtus/module_extensions.rb' + +# Offense count: 15 +# Configuration parameters: Exclude. +Style/Documentation: + Exclude: + - 'spec/**/*' + - 'test/**/*' + - 'lib/virtus.rb' + - 'lib/virtus/attribute/lazy_default.rb' + - 'lib/virtus/class_inclusions.rb' + - 'lib/virtus/const_missing_extensions.rb' + - 'lib/virtus/extensions.rb' + - 'lib/virtus/instance_methods.rb' + - 'lib/virtus/model.rb' + - 'lib/virtus/value_object.rb' + - 'lib/virtus/version.rb' + +# Offense count: 3 +# Configuration parameters: MinBodyLength. +Style/GuardClause: + Exclude: + - 'lib/virtus/class_methods.rb' + - 'lib/virtus/instance_methods.rb' + - 'lib/virtus/support/type_lookup.rb' + +# Offense count: 1 +Style/ModuleFunction: + Exclude: + - 'spec/shared/constants_helpers.rb' + +# Offense count: 1 +Style/MultilineBlockChain: + Exclude: + - 'lib/virtus/module_extensions.rb' + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SupportedStyles. +Style/SpaceInsideHashLiteralBraces: + Enabled: false diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 463b0e72..3c4bf2dd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -7,6 +7,7 @@ Virtus recently hit it's 1.0 release (2013-10-16). The focus now is on bug-fixes * Fork the project. * Make your feature addition or bug fix. * Add tests for it. This is important so I don't break it in a future version unintentionally. +* Run `rubocop -a` to fix any Ruby style issues and re-run `rubocop --auto-gen-config` for any acceptable exceptions. * Commit, do not mess with Rakefile or version (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) * Send me a pull request. Bonus points for topic branches. diff --git a/Gemfile b/Gemfile index d33512bc..7977f48a 100644 --- a/Gemfile +++ b/Gemfile @@ -6,14 +6,18 @@ gem 'bogus', '~> 0.1' gem 'inflecto', '~> 0.0.2' gem 'rspec', '~> 3.1' -gem "codeclimate-test-reporter", group: :test, require: false +group :development, :test do + gem 'rubocop', '0.35.1' +end + +group :test do + gem 'codeclimate-test-reporter', require: false +end group :tools do gem 'guard' gem 'guard-rspec' - gem 'rubocop' - platform :mri do gem 'mutant' gem 'mutant-rspec' diff --git a/Guardfile b/Guardfile index 79dc623d..069b825d 100644 --- a/Guardfile +++ b/Guardfile @@ -1,5 +1,5 @@ guard :rspec, spec_paths: 'spec/unit' do - #run all specs if configuration is modified + # run all specs if configuration is modified watch('Guardfile') { 'spec' } watch('Gemfile.lock') { 'spec' } watch('spec/spec_helper.rb') { 'spec' } @@ -15,5 +15,5 @@ guard :rspec, spec_paths: 'spec/unit' do # run a spec if it is modified watch(%r{\Aspec/(?:unit|integration)/.+_spec\.rb\z}) - notification :tmux, :display_message => true + notification :tmux, display_message: true end diff --git a/Rakefile b/Rakefile index 179aabe1..6b809222 100644 --- a/Rakefile +++ b/Rakefile @@ -1,15 +1,11 @@ -require "rspec/core/rake_task" +require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) task default: [:spec] -begin - require "rubocop/rake_task" +require 'rubocop/rake_task' +Rake::Task[:default].enhance [:rubocop] - Rake::Task[:default].enhance [:rubocop] - - RuboCop::RakeTask.new do |task| - task.options << "--display-cop-names" - end -rescue LoadError +RuboCop::RakeTask.new do |task| + task.options << '--display-cop-names' end diff --git a/lib/virtus.rb b/lib/virtus.rb index 1b83ab0b..19530f7d 100644 --- a/lib/virtus.rb +++ b/lib/virtus.rb @@ -2,10 +2,9 @@ # Base module which adds Attribute API to your classes module Virtus - # Provides args for const_get and const_defined? to make them behave # consistently across different versions of ruby - EXTRA_CONST_ARGS = (RUBY_VERSION < '1.9' ? [] : [ false ]).freeze + EXTRA_CONST_ARGS = (RUBY_VERSION < '1.9' ? [] : [false]).freeze # Represents an undefined parameter used by auto-generated option methods Undefined = Object.new.freeze @@ -14,7 +13,8 @@ class CoercionError < StandardError attr_reader :output, :attribute def initialize(output, attribute) - @output, @attribute = output, attribute + @output = output + @attribute = attribute super(build_message) end @@ -120,7 +120,7 @@ def self.coerce # @return [Configuration] # # @api public - def self.config(&block) + def self.config(&_block) yield configuration if block_given? configuration end @@ -237,7 +237,6 @@ def self.finalize def self.warn(msg) Kernel.warn(msg) end - end # module Virtus require 'descendants_tracker' diff --git a/lib/virtus/attribute.rb b/lib/virtus/attribute.rb index 0654d4d1..24837e47 100644 --- a/lib/virtus/attribute.rb +++ b/lib/virtus/attribute.rb @@ -1,5 +1,4 @@ module Virtus - # Attribute objects handle coercion and provide interface to hook into an # attribute set instance that's included into a class or object # @@ -112,7 +111,7 @@ def coerce(input) # # @api public def rename(name) - self.class.build(type, options.merge(:name => name)) + self.class.build(type, options.merge(name: name)) end # Return if the given value was coerced @@ -140,7 +139,7 @@ def value_coerced?(value) # # @api public def coercible? - kind_of?(Coercible) + is_a?(Coercible) end # Return if the attribute has lazy default value evaluation @@ -157,7 +156,7 @@ def coercible? # # @api public def lazy? - kind_of?(LazyDefault) + is_a?(LazyDefault) end # Return if the attribute is in the strict coercion mode @@ -174,7 +173,7 @@ def lazy? # # @api public def strict? - kind_of?(Strict) + is_a?(Strict) end # Return if the attribute is in the nullify blank coercion mode @@ -191,7 +190,7 @@ def strict? # # @api public def nullify_blank? - kind_of?(NullifyBlank) + is_a?(NullifyBlank) end # Return if the attribute is accepts nil values as valid coercion output @@ -239,7 +238,5 @@ def finalize freeze self end - end # class Attribute - end # module Virtus diff --git a/lib/virtus/attribute/accessor.rb b/lib/virtus/attribute/accessor.rb index 7a51f03d..8b628088 100644 --- a/lib/virtus/attribute/accessor.rb +++ b/lib/virtus/attribute/accessor.rb @@ -1,6 +1,5 @@ module Virtus class Attribute - # Accessor extension provides methods to read and write attributes # # @example @@ -13,7 +12,6 @@ class Attribute # attribute.get(object) # => 'jane@doe.com' # module Accessor - # Return name of this accessor attribute # # @return [Symbol] @@ -96,8 +94,6 @@ def public_reader? def public_writer? options[:writer] == :public end - end # Accessor - end # Attribute end # Virtus diff --git a/lib/virtus/attribute/boolean.rb b/lib/virtus/attribute/boolean.rb index c3b72efb..dbe3a4a2 100644 --- a/lib/virtus/attribute/boolean.rb +++ b/lib/virtus/attribute/boolean.rb @@ -1,6 +1,5 @@ module Virtus class Attribute - # Boolean attribute allows true or false values to be set # Additionally it adds boolean reader method, like "admin?" # @@ -49,7 +48,6 @@ def define_accessor_methods(attribute_set) super attribute_set.define_reader_method(self, "#{name}?", options[:reader]) end - end # class Boolean end # class Attribute end # module Virtus diff --git a/lib/virtus/attribute/builder.rb b/lib/virtus/attribute/builder.rb index 9ac6faa5..86878839 100644 --- a/lib/virtus/attribute/builder.rb +++ b/lib/virtus/attribute/builder.rb @@ -1,5 +1,4 @@ module Virtus - # Attribute placeholder used when type constant is passed as a string or symbol # # @private @@ -8,7 +7,8 @@ class PendingAttribute # @api private def initialize(type, options) - @type, @options = type.to_s, options + @type = type.to_s + @options = options @name = options[:name] end @@ -30,13 +30,12 @@ def determine_type if defined?(Inflecto) Inflecto.constantize(type) else - raise NotImplementedError, 'Virtus needs inflecto gem to constantize namespaced constant names' + fail NotImplementedError, 'Virtus needs inflecto gem to constantize namespaced constant names' end else Object.const_get(type) end end - end # PendingAttribute # Extracts the actual type primitive from input type @@ -64,13 +63,13 @@ def initialize_primitive if type.instance_of?(String) || type.instance_of?(Symbol) if !type.to_s.include?('::') && Object.const_defined?(type) Object.const_get(type) - elsif not Attribute::Builder.determine_type(type) + elsif !Attribute::Builder.determine_type(type) @pending = true type else type end - elsif not type.is_a?(Class) + elsif !type.is_a?(Class) type.class else type @@ -79,7 +78,6 @@ def initialize_primitive end class Attribute - # Builder is used to set up an attribute instance based on input type and options # # @private @@ -141,19 +139,19 @@ def initialize_type # @api private def initialize_options(options) - @options = klass.options.merge(:coerce => Virtus.coerce).update(options) + @options = klass.options.merge(coerce: Virtus.coerce).update(options) klass.merge_options!(type, @options) determine_visibility end # @api private def initialize_default_value - options.update(:default_value => DefaultValue.build(options[:default])) + options.update(default_value: DefaultValue.build(options[:default])) end # @api private def initialize_coercer - options.update(:coercer => determine_coercer) + options.update(coercer: determine_coercer) end # @api private @@ -179,10 +177,8 @@ def determine_visibility default_accessor = options.fetch(:accessor) reader_visibility = options.fetch(:reader, default_accessor) writer_visibility = options.fetch(:writer, default_accessor) - options.update(:reader => reader_visibility, :writer => writer_visibility) + options.update(reader: reader_visibility, writer: writer_visibility) end - end # class Builder - end # class Attribute end # module Virtus diff --git a/lib/virtus/attribute/coercer.rb b/lib/virtus/attribute/coercer.rb index a37fc0dd..46e7f03f 100644 --- a/lib/virtus/attribute/coercer.rb +++ b/lib/virtus/attribute/coercer.rb @@ -1,11 +1,9 @@ module Virtus class Attribute - # Coercer accessor wrapper # # @api private class Coercer < Virtus::Coercer - # @api private attr_reader :method, :coercers @@ -38,8 +36,6 @@ def call(value) def success?(primitive, value) coercers[primitive].coerced?(value) end - end # class Coercer - end # class Attribute end # module Virtus diff --git a/lib/virtus/attribute/coercible.rb b/lib/virtus/attribute/coercible.rb index 2b966d6e..950e11ed 100644 --- a/lib/virtus/attribute/coercible.rb +++ b/lib/virtus/attribute/coercible.rb @@ -1,10 +1,8 @@ module Virtus class Attribute - # Attribute extension providing coercion when setting an attribute value # module Coercible - # Coerce value before setting # # @see Accessor#set @@ -13,8 +11,6 @@ module Coercible def set(instance, value) super(instance, coerce(value)) end - end # Coercible - end # Attribute end # Virtus diff --git a/lib/virtus/attribute/collection.rb b/lib/virtus/attribute/collection.rb index 1b3a20b6..42ca0880 100644 --- a/lib/virtus/attribute/collection.rb +++ b/lib/virtus/attribute/collection.rb @@ -1,12 +1,11 @@ module Virtus class Attribute - # Collection attribute handles enumerable-like types # # Handles coercing members to the designated member type. # class Collection < Attribute - default Proc.new { |_, attribute| attribute.primitive.new } + default proc { |_, attribute| attribute.primitive.new } # @api private attr_reader :member_type @@ -22,10 +21,10 @@ def self.infer(type, primitive) if EmbeddedValue.handles?(member) || pending?(member) Type.new(primitive, member) else - klass.new { + klass.new do primitive primitive member_type Axiom::Types.infer(member) - } + end end end @@ -42,7 +41,7 @@ def self.infer_member_type(type) member_type = if type.count > 1 - raise NotImplementedError, "build SumType from list of types (#{type})" + fail NotImplementedError, "build SumType from list of types (#{type})" else type.first end @@ -91,8 +90,6 @@ def finalize def finalized? super && member_type.finalized? end - end # class Collection - end # class Attribute end # module Virtus diff --git a/lib/virtus/attribute/default_value.rb b/lib/virtus/attribute/default_value.rb index eca7350f..3e4f5ff8 100644 --- a/lib/virtus/attribute/default_value.rb +++ b/lib/virtus/attribute/default_value.rb @@ -1,6 +1,5 @@ module Virtus class Attribute - # Class representing the default value option # # @api private @@ -45,7 +44,6 @@ def initialize(value) def call(*) value end - end # class DefaultValue end # class Attribute end # module Virtus diff --git a/lib/virtus/attribute/default_value/from_callable.rb b/lib/virtus/attribute/default_value/from_callable.rb index cd3f8f52..cbb54e52 100644 --- a/lib/virtus/attribute/default_value/from_callable.rb +++ b/lib/virtus/attribute/default_value/from_callable.rb @@ -1,12 +1,10 @@ module Virtus class Attribute class DefaultValue - # Represents default value evaluated via a callable object # # @api private class FromCallable < DefaultValue - # Return if the class can handle the value # # @param [Object] value @@ -28,7 +26,6 @@ def self.handle?(value) def call(*args) @value.call(*args) end - end # class FromCallable end # class DefaultValue end # class Attribute diff --git a/lib/virtus/attribute/default_value/from_clonable.rb b/lib/virtus/attribute/default_value/from_clonable.rb index ffb6f177..1747a8f4 100644 --- a/lib/virtus/attribute/default_value/from_clonable.rb +++ b/lib/virtus/attribute/default_value/from_clonable.rb @@ -1,13 +1,12 @@ module Virtus class Attribute class DefaultValue - # Represents default value evaluated via a clonable object # # @api private class FromClonable < DefaultValue SINGLETON_CLASSES = [ - ::NilClass, ::TrueClass, ::FalseClass, ::Numeric, ::Symbol ].freeze + ::NilClass, ::TrueClass, ::FalseClass, ::Numeric, ::Symbol].freeze # Return if the class can handle the value # @@ -17,7 +16,7 @@ class FromClonable < DefaultValue # # @api private def self.handle?(value) - SINGLETON_CLASSES.none? { |klass| value.kind_of?(klass) } + SINGLETON_CLASSES.none? { |klass| value.is_a?(klass) } end # Evaluates the value via value#clone @@ -28,7 +27,6 @@ def self.handle?(value) def call(*) @value.clone end - end # class FromClonable end # class DefaultValue end # class Attribute diff --git a/lib/virtus/attribute/default_value/from_symbol.rb b/lib/virtus/attribute/default_value/from_symbol.rb index ef0e7dcf..b48428c6 100644 --- a/lib/virtus/attribute/default_value/from_symbol.rb +++ b/lib/virtus/attribute/default_value/from_symbol.rb @@ -1,12 +1,10 @@ module Virtus class Attribute class DefaultValue - # Represents default value evaluated via a symbol # # @api private class FromSymbol < DefaultValue - # Return if the class can handle the value # # @param [Object] value @@ -28,7 +26,6 @@ def self.handle?(value) def call(instance, _) instance.respond_to?(@value, true) ? instance.send(@value) : @value end - end # class FromSymbol end # class DefaultValue end # class Attribute diff --git a/lib/virtus/attribute/embedded_value.rb b/lib/virtus/attribute/embedded_value.rb index cca93799..d5bcc124 100644 --- a/lib/virtus/attribute/embedded_value.rb +++ b/lib/virtus/attribute/embedded_value.rb @@ -1,6 +1,5 @@ module Virtus class Attribute - # EmbeddedValue handles virtus-like objects, OpenStruct and Struct # class EmbeddedValue < Attribute @@ -11,16 +10,14 @@ class EmbeddedValue < Attribute # # @private class FromStruct < Virtus::Coercer - # @api public def call(input) - if input.kind_of?(primitive) + if input.is_a?(primitive) input - elsif not input.nil? + elsif !input.nil? primitive.new(*input) end end - end # FromStruct # Builds OpenStruct-like instance with attributes passed to the constructor @@ -28,16 +25,14 @@ def call(input) # # @private class FromOpenStruct < Virtus::Coercer - # @api public def call(input) - if input.kind_of?(primitive) + if input.is_a?(primitive) input - elsif not input.nil? + elsif !input.nil? primitive.new(input) end end - end # FromOpenStruct # @api private @@ -60,8 +55,6 @@ def self.build_coercer(type, _options) FromStruct.new(type) end end - end # class EmbeddedValue - end # class Attribute end # module Virtus diff --git a/lib/virtus/attribute/hash.rb b/lib/virtus/attribute/hash.rb index 7979c8e0..cf8f0dfd 100644 --- a/lib/virtus/attribute/hash.rb +++ b/lib/virtus/attribute/hash.rb @@ -1,11 +1,10 @@ module Virtus class Attribute - # Handles attributes with Hash type # class Hash < Attribute primitive ::Hash - default primitive.new + default primitive.new # @api private attr_reader :key_type, :value_type @@ -49,12 +48,13 @@ def self.determine_type(type) # @api private def self.infer_key_and_value_types(type) - return {} unless type.kind_of?(::Hash) + return {} unless type.is_a?(::Hash) if type.size > 1 - raise ArgumentError, "more than one [key => value] pair in `#{type}`" + fail ArgumentError, "more than one [key => value] pair in `#{type}`" else - key_type, value_type = type.keys.first, type.values.first + key_type = type.keys.first + value_type = type.values.first key_primitive = if key_type.is_a?(Class) && key_type < Attribute && key_type.primitive @@ -70,7 +70,7 @@ def self.infer_key_and_value_types(type) value_type end - { :key_type => key_primitive, :value_type => value_primitive} + { key_type: key_primitive, value_type: value_primitive } end end @@ -92,8 +92,8 @@ def self.build_type(definition) # @api private def self.merge_options!(type, options) - options[:key_type] ||= Attribute.build(type.key_type, :strict => options[:strict]) - options[:value_type] ||= Attribute.build(type.value_type, :strict => options[:strict]) + options[:key_type] ||= Attribute.build(type.key_type, strict: options[:strict]) + options[:value_type] ||= Attribute.build(type.value_type, strict: options[:strict]) end # Coerce members @@ -123,8 +123,6 @@ def finalize def finalized? super && key_type.finalized? && value_type.finalized? end - end # class Hash - end # class Attribute end # module Virtus diff --git a/lib/virtus/attribute/lazy_default.rb b/lib/virtus/attribute/lazy_default.rb index 6363640b..973e145b 100644 --- a/lib/virtus/attribute/lazy_default.rb +++ b/lib/virtus/attribute/lazy_default.rb @@ -1,8 +1,6 @@ module Virtus class Attribute - module LazyDefault - # @api public def get(instance) if instance.instance_variable_defined?(instance_variable_name) @@ -11,8 +9,6 @@ def get(instance) set_default_value(instance) end end - end # LazyDefault - end # Attribute end # Virtus diff --git a/lib/virtus/attribute/nullify_blank.rb b/lib/virtus/attribute/nullify_blank.rb index e43cf3b2..78c2ab46 100644 --- a/lib/virtus/attribute/nullify_blank.rb +++ b/lib/virtus/attribute/nullify_blank.rb @@ -1,10 +1,8 @@ module Virtus class Attribute - # Attribute extension which nullifies blank attributes when coercion failed # module NullifyBlank - # @see [Attribute#coerce] # # @api public @@ -17,8 +15,6 @@ def coerce(input) output end end - end # NullifyBlank - end # Attribute end # Virtus diff --git a/lib/virtus/attribute/strict.rb b/lib/virtus/attribute/strict.rb index 87fc40f6..a6d00e0d 100644 --- a/lib/virtus/attribute/strict.rb +++ b/lib/virtus/attribute/strict.rb @@ -1,10 +1,8 @@ module Virtus class Attribute - # Attribute extension which raises CoercionError when coercion failed # module Strict - # @see [Attribute#coerce] # # @raises [CoercionError] when coercer failed @@ -16,11 +14,9 @@ def coerce(*) if value_coerced?(output) || !required? && output.nil? output else - raise CoercionError.new(output, self) + fail CoercionError.new(output, self) end end - end # Strict - end # Attribute end # Virtus diff --git a/lib/virtus/attribute_set.rb b/lib/virtus/attribute_set.rb index 66055a84..239e52fb 100644 --- a/lib/virtus/attribute_set.rb +++ b/lib/virtus/attribute_set.rb @@ -1,5 +1,4 @@ module Virtus - # A set of Attribute objects class AttributeSet < Module include Enumerable @@ -43,7 +42,7 @@ def initialize(parent = nil, attributes = []) # @api public def each return to_enum unless block_given? - @index.each { |name, attribute| yield attribute if name.kind_of?(Symbol) } + @index.each { |name, attribute| yield attribute if name.is_a?(Symbol) } self end @@ -193,7 +192,7 @@ def set_defaults(object, filter = method(:skip_default?)) # # @api private def coerce(attributes) - ::Hash.try_convert(attributes) or raise( + ::Hash.try_convert(attributes) || fail( NoMethodError, "Expected #{attributes.inspect} to respond to #to_hash" ) end @@ -235,6 +234,5 @@ def merge_attributes(attributes) def update_index(name, attribute) @index[name] = @index[name.to_s.freeze] = attribute end - end # class AttributeSet end # module Virtus diff --git a/lib/virtus/builder.rb b/lib/virtus/builder.rb index 8216414a..1b46a1c4 100644 --- a/lib/virtus/builder.rb +++ b/lib/virtus/builder.rb @@ -1,5 +1,4 @@ module Virtus - # Class to build a Virtus module with it's own config # # This allows for individual Virtus modules to be included in @@ -8,7 +7,6 @@ module Virtus # # @private class Builder - # Return module # # @return [Module] @@ -42,7 +40,8 @@ def self.pending # # @api private def initialize(conf, mod = Module.new) - @config, @mod = conf, mod + @config = conf + @mod = mod add_included_hook add_extended_hook end @@ -89,7 +88,6 @@ def add_extended_hook def with_hook_context yield(HookContext.new(self, config)) end - end # class Builder # @private @@ -98,7 +96,6 @@ class ModelBuilder < Builder # @private class ModuleBuilder < Builder - private # @api private @@ -112,12 +109,10 @@ def add_included_hook end end end - end # ModuleBuilder # @private class ValueObjectBuilder < Builder - # @api private def extensions super << ValueObject::AllowedWriterMethods << ValueObject::InstanceMethods @@ -125,9 +120,7 @@ def extensions # @api private def options - super.merge(:writer => :private) + super.merge(writer: :private) end - end # ValueObjectBuilder - end # module Virtus diff --git a/lib/virtus/builder/hook_context.rb b/lib/virtus/builder/hook_context.rb index 4166e3e0..ad704703 100644 --- a/lib/virtus/builder/hook_context.rb +++ b/lib/virtus/builder/hook_context.rb @@ -1,6 +1,5 @@ module Virtus class Builder - # Context used for building "included" and "extended" hooks # # @private @@ -9,7 +8,8 @@ class HookContext # @api private def initialize(builder, config) - @builder, @config = builder, config + @builder = builder + @config = config initialize_attribute_method end @@ -44,8 +44,6 @@ def initialize_attribute_method super(name, type, method_options.merge(options)) end end - end # HookContext - end # Builder end # Virtus diff --git a/lib/virtus/class_inclusions.rb b/lib/virtus/class_inclusions.rb index aab1afd0..4c826e37 100644 --- a/lib/virtus/class_inclusions.rb +++ b/lib/virtus/class_inclusions.rb @@ -1,8 +1,6 @@ module Virtus - # Class-level extensions module ClassInclusions - # Extends a descendant with class and instance methods # # @param [Class] descendant @@ -21,7 +19,6 @@ def self.included(descendant) private_class_method :included module Methods - # Return a list of allowed writer method names # # @return [Set] @@ -41,8 +38,6 @@ def allowed_writer_methods def attribute_set self.class.attribute_set end - end # Methods - end # module ClassInclusions end # module Virtus diff --git a/lib/virtus/class_methods.rb b/lib/virtus/class_methods.rb index 7f5930a9..7c8b4bf0 100644 --- a/lib/virtus/class_methods.rb +++ b/lib/virtus/class_methods.rb @@ -1,5 +1,4 @@ module Virtus - # Class methods that are added when you include Virtus module ClassMethods include Extensions::Methods @@ -82,9 +81,8 @@ def allowed_methods # @api private def assert_valid_name(name) if instance_methods.include?(:attributes) && name.to_sym == :attributes - raise ArgumentError, "#{name.inspect} is not allowed as an attribute name" + fail ArgumentError, "#{name.inspect} is not allowed as an attribute name" end end - end # module ClassMethods end # module Virtus diff --git a/lib/virtus/coercer.rb b/lib/virtus/coercer.rb index 676d5537..2e399e94 100644 --- a/lib/virtus/coercer.rb +++ b/lib/virtus/coercer.rb @@ -1,5 +1,4 @@ module Virtus - # Abstract coercer class # class Coercer @@ -21,7 +20,7 @@ def initialize(type) # @return [Object] coerced input # # @api public - def call(input) + def call(_input) NotImplementedError.new("#{self.class}#call must be implemented") end @@ -33,9 +32,7 @@ def call(input) # # @api public def success?(primitive, input) - input.kind_of?(primitive) + input.is_a?(primitive) end - end # Coercer - end # Virtus diff --git a/lib/virtus/configuration.rb b/lib/virtus/configuration.rb index 7e91b300..95858a0a 100644 --- a/lib/virtus/configuration.rb +++ b/lib/virtus/configuration.rb @@ -1,8 +1,6 @@ module Virtus - # A Configuration instance class Configuration - # Access the finalize setting for this instance attr_accessor :finalize @@ -29,7 +27,7 @@ class Configuration # @return [undefined] # # @api private - def initialize(options={}) + def initialize(options = {}) @finalize = options.fetch(:finalize, true) @coerce = options.fetch(:coerce, true) @strict = options.fetch(:strict, false) @@ -60,13 +58,12 @@ def coercer(&block) # @api private def to_h - { :coerce => coerce, - :finalize => finalize, - :strict => strict, - :nullify_blank => nullify_blank, - :required => required, - :configured_coercer => coercer }.freeze + { coerce: coerce, + finalize: finalize, + strict: strict, + nullify_blank: nullify_blank, + required: required, + configured_coercer: coercer }.freeze end - end # class Configuration end # module Virtus diff --git a/lib/virtus/const_missing_extensions.rb b/lib/virtus/const_missing_extensions.rb index fe374525..c545e6c3 100644 --- a/lib/virtus/const_missing_extensions.rb +++ b/lib/virtus/const_missing_extensions.rb @@ -1,6 +1,5 @@ module Virtus module ConstMissingExtensions - # Hooks into const missing process to determine types of attributes # # @param [String] name @@ -9,10 +8,9 @@ module ConstMissingExtensions # # @api private def const_missing(name) - Attribute::Builder.determine_type(name) or - Axiom::Types.const_defined?(name) && Axiom::Types.const_get(name) or + Attribute::Builder.determine_type(name) || + Axiom::Types.const_defined?(name) && Axiom::Types.const_get(name) || super end - end end diff --git a/lib/virtus/extensions.rb b/lib/virtus/extensions.rb index 031fb8ca..a91e751a 100644 --- a/lib/virtus/extensions.rb +++ b/lib/virtus/extensions.rb @@ -1,9 +1,8 @@ module Virtus - # Extensions common for both classes and instances module Extensions WRITER_METHOD_REGEXP = /=\z/.freeze - INVALID_WRITER_METHODS = %w[ == != === []= attributes= ].to_set.freeze + INVALID_WRITER_METHODS = %w( == != === []= attributes= ).to_set.freeze RESERVED_NAMES = [:attributes].to_set.freeze # A hook called when an object is extended with Virtus @@ -24,7 +23,6 @@ def self.extended(object) private_class_method :extended module Methods - # @api private def self.extended(descendant) super @@ -61,14 +59,14 @@ def self.extended(descendant) # @api public def attribute(name, type = nil, options = {}) assert_valid_name(name) - attribute_set << Attribute.build(type, options.merge(:name => name)) + attribute_set << Attribute.build(type, options.merge(name: name)) self end # @see Virtus.default_value # # @api public - def values(&block) + def values(&_block) private :attributes= if instance_methods.include?(:attributes=) yield include(::Equalizer.new(*attribute_set.map(&:name))) @@ -98,8 +96,6 @@ def allowed_writer_methods def attribute_set @attribute_set end - end # Methods - end # module Extensions end # module Virtus diff --git a/lib/virtus/instance_methods.rb b/lib/virtus/instance_methods.rb index 4448bf66..4c716291 100644 --- a/lib/virtus/instance_methods.rb +++ b/lib/virtus/instance_methods.rb @@ -1,10 +1,7 @@ module Virtus - # Instance methods that are added when you include Virtus module InstanceMethods - module Constructor - # Set attributes during initialization of an object # # @param [#to_hash] attributes @@ -17,11 +14,9 @@ def initialize(attributes = nil) attribute_set.set(self, attributes) if attributes set_default_attributes end - end # Constructor module MassAssignment - # Returns a hash of all publicly accessible attributes # # @example @@ -70,7 +65,6 @@ def attributes def attributes=(attributes) attribute_set.set(self, attributes) end - end # MassAssignment # Returns a value of the attribute with the given name @@ -210,9 +204,8 @@ def allowed_methods # @api private def assert_valid_name(name) if respond_to?(:attributes) && name.to_sym == :attributes || name.to_sym == :attribute_set - raise ArgumentError, "#{name.inspect} is not allowed as an attribute name" + fail ArgumentError, "#{name.inspect} is not allowed as an attribute name" end end - end # module InstanceMethods end # module Virtus diff --git a/lib/virtus/model.rb b/lib/virtus/model.rb index 0eaf06a9..c3f1ac87 100644 --- a/lib/virtus/model.rb +++ b/lib/virtus/model.rb @@ -1,7 +1,5 @@ module Virtus - module Model - # @api private def self.included(descendant) super @@ -15,7 +13,6 @@ def self.extended(descendant) end module Core - # @api private def self.included(descendant) super @@ -32,22 +29,18 @@ def self.extended(descendant) descendant.extend(InstanceMethods) end private_class_method :extended - end # Core module Constructor - # @api private def self.included(descendant) super descendant.send(:include, InstanceMethods::Constructor) end private_class_method :included - end # Constructor module MassAssignment - # @api private def self.included(descendant) super @@ -61,8 +54,6 @@ def self.extended(descendant) descendant.extend(InstanceMethods::MassAssignment) end private_class_method :extended - end # MassAssignment - end # Model end # Virtus diff --git a/lib/virtus/module_extensions.rb b/lib/virtus/module_extensions.rb index ece4f2bf..bf4d81f2 100644 --- a/lib/virtus/module_extensions.rb +++ b/lib/virtus/module_extensions.rb @@ -1,5 +1,4 @@ module Virtus - # Virtus module that can define attributes for later inclusion # # @private @@ -83,6 +82,5 @@ def define_attributes(object) object.attribute(*attribute_args) end end - end # module ModuleExtensions end # module Virtus diff --git a/lib/virtus/support/equalizer.rb b/lib/virtus/support/equalizer.rb index 9784853f..bd2f0349 100644 --- a/lib/virtus/support/equalizer.rb +++ b/lib/virtus/support/equalizer.rb @@ -1,8 +1,6 @@ module Virtus - # Define equality, equivalence and inspection methods class Equalizer < Module - # Initialize an Equalizer with the given keys # # Will use the keys with which it is initialized to define #cmp?, @@ -32,7 +30,7 @@ def <<(key) self end - private + private # Define the equalizer methods based on #keys # @@ -75,7 +73,8 @@ def define_hash_method # # @api private def define_inspect_method - name, keys = @name, @keys + name = @name + keys = @keys define_method(:inspect) do "#<#{name}#{keys.map { |key| " #{key}=#{send(key).inspect}" }.join}>" end @@ -92,7 +91,6 @@ def include_comparison_methods # The comparison methods module Methods - # Compare the object with other object for equality # # @example @@ -120,9 +118,8 @@ def eql?(other) # # @api public def ==(other) - other.kind_of?(self.class) && cmp?(__method__, other) + other.is_a?(self.class) && cmp?(__method__, other) end - end # module Methods end # class Equalizer end # module Virtus diff --git a/lib/virtus/support/options.rb b/lib/virtus/support/options.rb index 2a845abc..638d65b8 100644 --- a/lib/virtus/support/options.rb +++ b/lib/virtus/support/options.rb @@ -1,8 +1,6 @@ module Virtus - # A module that adds class and instance level options module Options - # Returns default options hash for a given attribute class # # @example @@ -51,7 +49,7 @@ def accept_options(*new_options) self end - protected + protected # Adds a reader/writer method for the give option name # @@ -95,7 +93,7 @@ def add_accepted_options(new_options) self end - private + private # Adds descendant to descendants array and inherits default options # @@ -108,6 +106,5 @@ def inherited(descendant) super descendant.add_accepted_options(accepted_options).set_options(options) end - end # module Options end # module Virtus diff --git a/lib/virtus/support/type_lookup.rb b/lib/virtus/support/type_lookup.rb index a6ce2e39..ed1e3280 100644 --- a/lib/virtus/support/type_lookup.rb +++ b/lib/virtus/support/type_lookup.rb @@ -1,8 +1,6 @@ module Virtus - # A module that adds type lookup to a class module TypeLookup - TYPE_FORMAT = /\A[A-Z]\w*\z/.freeze # Set cache ivar on the model @@ -41,10 +39,10 @@ def determine_type(class_or_name) # # @api private def primitive - raise NotImplementedError, "#{self}.primitive must be implemented" + fail NotImplementedError, "#{self}.primitive must be implemented" end - private + private # @api private def determine_type_and_cache(class_or_name) @@ -84,7 +82,7 @@ def determine_type_from_primitive(primitive) descendants.select(&:primitive).reverse_each do |descendant| descendant_primitive = descendant.primitive next unless primitive <= descendant_primitive - type = descendant if type.nil? or type.primitive > descendant_primitive + type = descendant if type.nil? || type.primitive > descendant_primitive end type end @@ -100,10 +98,9 @@ def determine_type_from_primitive(primitive) # # @api private def determine_type_from_string(string) - if string =~ TYPE_FORMAT and const_defined?(string, *EXTRA_CONST_ARGS) + if string =~ TYPE_FORMAT && const_defined?(string, *EXTRA_CONST_ARGS) const_get(string, *EXTRA_CONST_ARGS) end end - end # module TypeLookup end # module Virtus diff --git a/lib/virtus/value_object.rb b/lib/virtus/value_object.rb index 61c9b8fa..81d0255a 100644 --- a/lib/virtus/value_object.rb +++ b/lib/virtus/value_object.rb @@ -1,5 +1,4 @@ module Virtus - # Include this Module for Value Object semantics # # The idea is that instances should be immutable and compared based on state @@ -18,7 +17,6 @@ module Virtus # hash = { location => :foo } # hash[same_location] #=> :foo module ValueObject - # Callback to configure including Class as a Value Object # # Including Class will include Virtus and have additional @@ -45,7 +43,6 @@ def self.included(base) private_class_method :included module InstanceMethods - # ValueObjects are immutable and can't be cloned # # They always represent the same value @@ -60,7 +57,7 @@ module InstanceMethods def clone self end - alias dup clone + alias_method :dup, :clone # Create a new ValueObject by combining the passed attribute hash with # the instances attributes. @@ -77,7 +74,6 @@ def clone def with(attribute_updates) self.class.new(attribute_set.get(self).merge(attribute_updates)) end - end module AllowedWriterMethods @@ -90,14 +86,13 @@ def allowed_writer_methods @allowed_writer_methods ||= begin allowed_writer_methods = super - allowed_writer_methods += attribute_set.map{|attr| "#{attr.name}="} + allowed_writer_methods += attribute_set.map { |attr| "#{attr.name}=" } allowed_writer_methods.to_set.freeze end end end module ClassMethods - # Define an attribute on the receiver # # The Attribute will have private writer methods (eg., immutable instances) @@ -118,7 +113,7 @@ module ClassMethods # @api public def attribute(name, type, options = {}) equalizer << name - super name, type, options.merge(:writer => :private) + super name, type, options.merge(writer: :private) end # Define and include a module that provides Value Object semantics @@ -142,9 +137,6 @@ def equalizer equalizer end end - end # module ClassMethods - end # module ValueObject - end # module Virtus diff --git a/spec/integration/attributes_attribute_spec.rb b/spec/integration/attributes_attribute_spec.rb index 3c89e371..f20a6f62 100644 --- a/spec/integration/attributes_attribute_spec.rb +++ b/spec/integration/attributes_attribute_spec.rb @@ -1,8 +1,7 @@ require 'spec_helper' describe "Adding attribute called 'attributes'" do - - context "when mass assignment is disabled" do + context 'when mass assignment is disabled' do before do module Examples class User @@ -13,11 +12,11 @@ class User end end - it "allows model to use `attributes` attribute" do + it 'allows model to use `attributes` attribute' do user = Examples::User.new expect(user.attributes).to eq(nil) - user.attributes = "attributes string" - expect(user.attributes).to eq("attributes string") + user.attributes = 'attributes string' + expect(user.attributes).to eq('attributes string') end it "doesn't accept `attributes` key in initializer" do diff --git a/spec/integration/building_module_spec.rb b/spec/integration/building_module_spec.rb index ed492a21..eae43277 100644 --- a/spec/integration/building_module_spec.rb +++ b/spec/integration/building_module_spec.rb @@ -3,25 +3,25 @@ describe 'I can create a Virtus module' do before do module Examples - NoncoercingModule = Virtus.model { |config| + NoncoercingModule = Virtus.model do |config| config.coerce = false - } + end - CoercingModule = Virtus.model { |config| + CoercingModule = Virtus.model do |config| config.coerce = true config.coercer do |coercer| coercer.string.boolean_map = { 'yup' => true, 'nope' => false } end - } + end - StrictModule = Virtus.model { |config| + StrictModule = Virtus.model do |config| config.strict = true - } + end - BlankModule = Virtus.model { |config| + BlankModule = Virtus.model do |config| config.nullify_blank = true - } + end class NoncoercedUser include NoncoercingModule @@ -41,27 +41,27 @@ class StrictModel include StrictModule attribute :stuff, Hash - attribute :happy, Boolean, :strict => false + attribute :happy, Boolean, strict: false end class BlankModel include BlankModule attribute :stuff, Hash - attribute :happy, Boolean, :nullify_blank => false + attribute :happy, Boolean, nullify_blank: false end end end specify 'including a custom module with coercion disabled' do - user = Examples::NoncoercedUser.new(:name => :Giorgio, :happy => 'yes') + user = Examples::NoncoercedUser.new(name: :Giorgio, happy: 'yes') expect(user.name).to be(:Giorgio) expect(user.happy).to eql('yes') end specify 'including a custom module with coercion enabled' do - user = Examples::CoercedUser.new(:name => 'Paul', :happy => 'nope') + user = Examples::CoercedUser.new(name: 'Paul', happy: 'nope') expect(user.name).to eql('Paul') expect(user.happy).to be(false) diff --git a/spec/integration/collection_member_coercion_spec.rb b/spec/integration/collection_member_coercion_spec.rb index f6ef068a..71f512d5 100644 --- a/spec/integration/collection_member_coercion_spec.rb +++ b/spec/integration/collection_member_coercion_spec.rb @@ -31,18 +31,22 @@ class User it { is_expected.to respond_to(:addresses=) } let(:instance) do - described_class.new(:phone_numbers => phone_numbers_attributes, - :addresses => addresses_attributes) + described_class.new(phone_numbers: phone_numbers_attributes, + addresses: addresses_attributes) end - let(:phone_numbers_attributes) { [ - { :number => '212-555-1212' }, - { :number => '919-444-3265' }, - ] } + let(:phone_numbers_attributes) do + [ + { number: '212-555-1212' }, + { number: '919-444-3265' } + ] + end - let(:addresses_attributes) { [ - { :address => '1234 Any St.', :locality => 'Anytown', :region => "DC", :postal_code => "21234" }, - ] } + let(:addresses_attributes) do + [ + { address: '1234 Any St.', locality: 'Anytown', region: 'DC', postal_code: '21234' } + ] + end describe '#phone_numbers' do describe 'first entry' do @@ -80,17 +84,17 @@ class User describe '#locality' do subject { super().locality } - it { is_expected.to eql('Anytown') } + it { is_expected.to eql('Anytown') } end describe '#region' do subject { super().region } - it { is_expected.to eql('DC') } + it { is_expected.to eql('DC') } end describe '#postal_code' do subject { super().postal_code } - it { is_expected.to eql('21234') } + it { is_expected.to eql('21234') } end end end diff --git a/spec/integration/custom_attributes_spec.rb b/spec/integration/custom_attributes_spec.rb index 3473d024..bd79060b 100644 --- a/spec/integration/custom_attributes_spec.rb +++ b/spec/integration/custom_attributes_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' describe 'custom attributes' do - before do module Examples class NoisyString < Virtus::Attribute @@ -38,5 +37,4 @@ class User subject.scream = 'welcome' expect(subject.scream).to eq('WELCOME') end - end diff --git a/spec/integration/custom_collection_attributes_spec.rb b/spec/integration/custom_collection_attributes_spec.rb index f267aa5d..3cb8458a 100644 --- a/spec/integration/custom_collection_attributes_spec.rb +++ b/spec/integration/custom_collection_attributes_spec.rb @@ -54,12 +54,12 @@ class Library end it 'coerces an array of attribute hashes' do - library.books = [{ :title => 'Foo' }] + library.books = [{ title: 'Foo' }] expect(books).to be_kind_of(Examples::BookCollection) end it 'coerces its members' do - library.books = [{ :title => 'Foo' }] + library.books = [{ title: 'Foo' }] expect(books.count).to eq(1) expect(books.first).to be_kind_of(Examples::Book) end @@ -79,7 +79,7 @@ def books_should_be_an_empty_collection context 'with an enumerable' do require 'forwardable' - let(:book_collection_class) { + let(:book_collection_class) do Class.new do extend Forwardable include Enumerable @@ -94,7 +94,7 @@ def self.[](*args) new(*args) end end - } + end it_behaves_like 'a collection' end diff --git a/spec/integration/default_values_spec.rb b/spec/integration/default_values_spec.rb index d9d9b1e8..639ffd85 100644 --- a/spec/integration/default_values_spec.rb +++ b/spec/integration/default_values_spec.rb @@ -1,10 +1,8 @@ require 'spec_helper' -describe "default values" do - +describe 'default values' do before do module Examples - class Reference include Virtus::ValueObject @@ -15,11 +13,11 @@ class Page include Virtus attribute :title, String - attribute :slug, String, :default => lambda { |post, attribute| post.title.downcase.gsub(' ', '-') }, :lazy => true - attribute :view_count, Integer, :default => 0 - attribute :published, Boolean, :default => false, :accessor => :private - attribute :editor_title, String, :default => :default_editor_title, :lazy => true - attribute :reference, String, :default => Reference.new + attribute :slug, String, default: ->(post, _attribute) { post.title.downcase.tr(' ', '-') }, lazy: true + attribute :view_count, Integer, default: 0 + attribute :published, Boolean, default: false, accessor: :private + attribute :editor_title, String, default: :default_editor_title, lazy: true + attribute :reference, String, default: Reference.new attribute :revisions, Array attribute :index, Hash attribute :authors, Set @@ -28,7 +26,6 @@ def default_editor_title published? ? title : "UNPUBLISHED: #{title}" end end - end end diff --git a/spec/integration/defining_attributes_spec.rb b/spec/integration/defining_attributes_spec.rb index d0d711e5..e9026dc7 100644 --- a/spec/integration/defining_attributes_spec.rb +++ b/spec/integration/defining_attributes_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' -describe "virtus attribute definitions" do - +describe 'virtus attribute definitions' do before do module Examples class Person @@ -14,7 +13,6 @@ class Person end class Manager < Person - end end end @@ -29,7 +27,7 @@ class Manager < Person end specify 'the constructor accepts a hash for mass-assignment' do - john = Examples::Person.new(:name => 'John', :age => 13) + john = Examples::Person.new(name: 'John', age: 13) expect(john.name).to eq('John') expect(john.age).to eq(13) end @@ -41,7 +39,7 @@ class Manager < Person end context 'with attributes' do - let(:attributes) { {:name => 'Jane', :age => 45, :doctor => true, :salary => 4500} } + let(:attributes) { { name: 'Jane', age: 45, doctor: true, salary: 4500 } } specify "#attributes returns the object's attributes as a hash" do expect(person.attributes).to eq(attributes) @@ -58,21 +56,21 @@ class Manager < Person context 'inheritance' do specify 'inherits all the attributes from the base class' do - fred = Examples::Manager.new(:name => 'Fred', :age => 29) + fred = Examples::Manager.new(name: 'Fred', age: 29) expect(fred.name).to eq('Fred') expect(fred.age).to eq(29) end specify 'lets you add attributes to the base class at runtime' do - frank = Examples::Manager.new(:name => 'Frank') + frank = Examples::Manager.new(name: 'Frank') Examples::Person.attribute :just_added, String frank.just_added = 'it works!' expect(frank.just_added).to eq('it works!') end specify 'lets you add attributes to the subclass at runtime' do - person_jack = Examples::Person.new(:name => 'Jack') - manager_frank = Examples::Manager.new(:name => 'Frank') + person_jack = Examples::Person.new(name: 'Jack') + manager_frank = Examples::Manager.new(name: 'Frank') Examples::Manager.attribute :just_added, String diff --git a/spec/integration/embedded_value_spec.rb b/spec/integration/embedded_value_spec.rb index f33b3477..67eaafca 100644 --- a/spec/integration/embedded_value_spec.rb +++ b/spec/integration/embedded_value_spec.rb @@ -26,17 +26,17 @@ class User end end - subject { Examples::User.new(:name => 'the guy', - :address => address_attributes) } + subject do + Examples::User.new(name: 'the guy', + address: address_attributes) + end let(:address_attributes) do - { :street => 'Street 1/2', :zipcode => '12345', :city => { :name => 'NYC' } } + { street: 'Street 1/2', zipcode: '12345', city: { name: 'NYC' } } end specify '#attributes returns instances of the embedded values' do - expect(subject.attributes).to eq({ - :name => 'the guy', - :address => subject.address - }) + expect(subject.attributes).to eq(name: 'the guy', + address: subject.address) end specify 'allows you to pass a hash for the embedded value' do @@ -46,5 +46,4 @@ class User expect(user.address.zipcode).to eq('12345') expect(user.address.city.name).to eq('NYC') end - end diff --git a/spec/integration/extending_objects_spec.rb b/spec/integration/extending_objects_spec.rb index f6ca9785..1dfb465c 100644 --- a/spec/integration/extending_objects_spec.rb +++ b/spec/integration/extending_objects_spec.rb @@ -10,7 +10,7 @@ class Admin; end end specify 'defining attributes on an object' do - attributes = { :name => 'John', :age => 29 } + attributes = { name: 'John', age: 29 } admin = Examples::Admin.new admin.extend(Virtus) @@ -26,7 +26,7 @@ class Admin; end expect(admin.attributes).to eql(attributes) - new_attributes = { :name => 'Jane', :age => 28 } + new_attributes = { name: 'Jane', age: 28 } admin.attributes = new_attributes expect(admin.name).to eql('Jane') diff --git a/spec/integration/hash_attributes_coercion_spec.rb b/spec/integration/hash_attributes_coercion_spec.rb index c225daa4..38d6f395 100644 --- a/spec/integration/hash_attributes_coercion_spec.rb +++ b/spec/integration/hash_attributes_coercion_spec.rb @@ -1,19 +1,17 @@ require 'spec_helper' - class Package include Virtus attribute :dimensions, Hash[Symbol => Float] - attribute :meta_info , Hash[String => String] + attribute :meta_info, Hash[String => String] end - describe Package do let(:instance) do described_class.new( - :dimensions => { 'width' => "2.2", :height => 2, "length" => 4.5 }, - :meta_info => { 'from' => :Me , :to => 'You' } + dimensions: { 'width' => '2.2', :height => 2, 'length' => 4.5 }, + meta_info: { 'from' => :Me, :to => 'You' } ) end @@ -31,7 +29,7 @@ class Package it { is_expected.to have_key :length } it 'should be coerced to [Symbol => Float] format' do - expect(dimensions[:width]).to be_eql(2.2) + expect(dimensions[:width]).to be_eql(2.2) expect(dimensions[:height]).to be_eql(2.0) expect(dimensions[:length]).to be_eql(4.5) end diff --git a/spec/integration/inheritance_spec.rb b/spec/integration/inheritance_spec.rb index 068466af..dfcd7acb 100644 --- a/spec/integration/inheritance_spec.rb +++ b/spec/integration/inheritance_spec.rb @@ -25,7 +25,7 @@ class Second < Base end it 'sets correct attributes on the descendant classes' do - first = Examples::First.new(:id => 1, :description => 'hello world') + first = Examples::First.new(id: 1, description: 'hello world') expect(first.id).to be(1) expect(first.name).to eql('Named: 1') diff --git a/spec/integration/injectible_coercers_spec.rb b/spec/integration/injectible_coercers_spec.rb index bf891733..53f7e159 100644 --- a/spec/integration/injectible_coercers_spec.rb +++ b/spec/integration/injectible_coercers_spec.rb @@ -7,12 +7,12 @@ class EmailAddress include Virtus.value_object values do - attribute :address, String, :coercer => lambda { |add| add.downcase } + attribute :address, String, coercer: ->(add) { add.downcase } end def self.coerce(input) if input.is_a?(String) - new(:address => input) + new(address: input) else new(input) end @@ -23,7 +23,7 @@ class User include Virtus.model attribute :email, EmailAddress, - :coercer => lambda { |input| Examples::EmailAddress.coerce(input) } + coercer: ->(input) { Examples::EmailAddress.coerce(input) } end end end @@ -33,16 +33,15 @@ class User Examples.send(:remove_const, :User) end - let(:doe) { Examples::EmailAddress.new(:address => 'john.doe@example.com') } + let(:doe) { Examples::EmailAddress.new(address: 'john.doe@example.com') } it 'accepts an email hash' do - user = Examples::User.new :email => { :address => 'John.Doe@Example.Com' } + user = Examples::User.new email: { address: 'John.Doe@Example.Com' } expect(user.email).to eq(doe) end it 'coerces an embedded string' do - user = Examples::User.new :email => 'John.Doe@Example.Com' + user = Examples::User.new email: 'John.Doe@Example.Com' expect(user.email).to eq(doe) end - end diff --git a/spec/integration/mass_assignment_with_accessors_spec.rb b/spec/integration/mass_assignment_with_accessors_spec.rb index 72e1124e..2c614427 100644 --- a/spec/integration/mass_assignment_with_accessors_spec.rb +++ b/spec/integration/mass_assignment_with_accessors_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' -describe "mass assignment with accessors" do - +describe 'mass assignment with accessors' do before do module Examples class Product @@ -16,7 +15,7 @@ def categories=(categories) self.subcategory = categories.last end - private + private def _id=(value) self.id = value @@ -25,7 +24,7 @@ def _id=(value) end end - subject { Examples::Product.new(:categories => ['Office', 'Printers'], :_id => 100) } + subject { Examples::Product.new(categories: %w(Office Printers), _id: 100) } specify 'works uppon instantiation' do expect(subject.category).to eq('Office') @@ -33,7 +32,7 @@ def _id=(value) end specify 'can be set with #attributes=' do - subject.attributes = {:categories => ['Home', 'Furniture']} + subject.attributes = { categories: %w(Home Furniture) } expect(subject.category).to eq('Home') expect(subject.subcategory).to eq('Furniture') end diff --git a/spec/integration/overriding_virtus_spec.rb b/spec/integration/overriding_virtus_spec.rb index d02b8f3d..845710a6 100644 --- a/spec/integration/overriding_virtus_spec.rb +++ b/spec/integration/overriding_virtus_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' describe 'overriding virtus behavior' do - before do module Examples class Article @@ -14,7 +13,7 @@ def title end def title=(name) - super unless self.title == "can't be changed" + super unless title == "can't be changed" end end end @@ -26,19 +25,19 @@ def title=(name) end specify 'super can be used to access the getter defined by virtus' do - expect(Examples::Article.new(:title => 'example article').title).to eq('example article') + expect(Examples::Article.new(title: 'example article').title).to eq('example article') end end describe 'overriding an attribute setter' do specify 'calls the defined setter' do - article = Examples::Article.new(:title => "can't be changed") + article = Examples::Article.new(title: "can't be changed") article.title = 'this will never be assigned' expect(article.title).to eq("can't be changed") end specify 'super can be used to access the setter defined by virtus' do - article = Examples::Article.new(:title => 'example article') + article = Examples::Article.new(title: 'example article') article.title = 'my new title' expect(article.title).to eq('my new title') end diff --git a/spec/integration/required_attributes_spec.rb b/spec/integration/required_attributes_spec.rb index 76b1fa9e..bf295252 100644 --- a/spec/integration/required_attributes_spec.rb +++ b/spec/integration/required_attributes_spec.rb @@ -4,20 +4,20 @@ before do module Examples class User - include Virtus.model(:strict => true) + include Virtus.model(strict: true) attribute :name, String - attribute :age, Integer, :required => false + attribute :age, Integer, required: false end end end it 'raises coercion error when required attribute is nil' do - expect { Examples::User.new(:name => nil) }.to raise_error(Virtus::CoercionError, "Failed to coerce attribute `name' from nil into String") + expect { Examples::User.new(name: nil) }.to raise_error(Virtus::CoercionError, "Failed to coerce attribute `name' from nil into String") end it 'does not raise coercion error when not required attribute is nil' do - user = Examples::User.new(:name => 'Jane', :age => nil) + user = Examples::User.new(name: 'Jane', age: nil) expect(user.name).to eql('Jane') expect(user.age).to be(nil) diff --git a/spec/integration/struct_as_embedded_value_spec.rb b/spec/integration/struct_as_embedded_value_spec.rb index f83465a5..5d7b04b5 100644 --- a/spec/integration/struct_as_embedded_value_spec.rb +++ b/spec/integration/struct_as_embedded_value_spec.rb @@ -15,7 +15,7 @@ class Rectangle end subject do - Examples::Rectangle.new(:top_left => [ 3, 5 ], :bottom_right => [ 8, 7 ]) + Examples::Rectangle.new(top_left: [3, 5], bottom_right: [8, 7]) end specify 'initialize a struct object with correct attributes' do diff --git a/spec/integration/using_modules_spec.rb b/spec/integration/using_modules_spec.rb index 0abb59f3..2886b161 100644 --- a/spec/integration/using_modules_spec.rb +++ b/spec/integration/using_modules_spec.rb @@ -17,7 +17,7 @@ module Name module Age include Common - attribute :age, Integer + attribute :age, Integer end class User @@ -39,7 +39,7 @@ class Moderator; end expect(Examples::Admin.attribute_set[:name]).to be_instance_of(Virtus::Attribute) expect(Examples::Admin.attribute_set[:age]).to be_instance_of(Virtus::Attribute) - user = Examples::Admin.new(:name => 'Piotr', :age => 29) + user = Examples::Admin.new(name: 'Piotr', age: 29) expect(user.name).to eql('Piotr') expect(user.age).to eql(29) end @@ -48,7 +48,7 @@ class Moderator; end moderator = Examples::Moderator.new moderator.extend(Examples::Name, Examples::Age) - moderator.attributes = { :name => 'John', :age => 21 } + moderator.attributes = { name: 'John', age: 21 } expect(moderator.name).to eql('John') expect(moderator.age).to eql(21) end diff --git a/spec/integration/value_object_with_custom_constructor_spec.rb b/spec/integration/value_object_with_custom_constructor_spec.rb index 0be0a883..7650363e 100644 --- a/spec/integration/value_object_with_custom_constructor_spec.rb +++ b/spec/integration/value_object_with_custom_constructor_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe "Defining a ValueObject with a custom constructor" do +describe 'Defining a ValueObject with a custom constructor' do before do module Examples class Point @@ -10,7 +10,7 @@ class Point attribute :y, Integer def initialize(attributes) - if attributes.kind_of?(Array) + if attributes.is_a?(Array) self.x = attributes.first self.y = attributes.last else @@ -29,10 +29,10 @@ class Rectangle end subject do - Examples::Rectangle.new(:top_left => [ 3, 4 ], :bottom_right => [ 5, 8 ]) + Examples::Rectangle.new(top_left: [3, 4], bottom_right: [5, 8]) end - specify "initialize a value object attribute with correct attributes" do + specify 'initialize a value object attribute with correct attributes' do expect(subject.top_left.x).to be(3) expect(subject.top_left.y).to be(4) diff --git a/spec/integration/virtus/value_object_spec.rb b/spec/integration/virtus/value_object_spec.rb index 100de421..13dd1cf9 100644 --- a/spec/integration/virtus/value_object_spec.rb +++ b/spec/integration/virtus/value_object_spec.rb @@ -14,19 +14,19 @@ def self.name end end - let(:attribute_values) { { :latitude => 10.0, :longitude => 20.0 } } + let(:attribute_values) { { latitude: 10.0, longitude: 20.0 } } let(:instance_with_equal_state) { class_under_test.new(attribute_values) } let(:instance_with_different_state) do - class_under_test.new(:latitude => attribute_values[:latitude]) + class_under_test.new(latitude: attribute_values[:latitude]) end subject { class_under_test.new(attribute_values) } describe 'initialization' do it 'sets the attribute values provided to Class.new' do - expect(class_under_test.new(:latitude => 10000.001).latitude).to eq(10000.001) + expect(class_under_test.new(latitude: 10_000.001).latitude).to eq(10_000.001) expect(subject.latitude).to eql(attribute_values[:latitude]) end end @@ -39,7 +39,7 @@ def self.name it 'writer methods are set to private' do private_methods = class_under_test.private_instance_methods - private_methods.map! { |m| m.to_s } + private_methods.map!(&:to_s) expect(private_methods).to include('latitude=', 'longitude=', 'attributes=') end diff --git a/spec/shared/constants_helpers.rb b/spec/shared/constants_helpers.rb index 0e30ade1..17cebad1 100644 --- a/spec/shared/constants_helpers.rb +++ b/spec/shared/constants_helpers.rb @@ -1,5 +1,4 @@ module ConstantsHelpers - extend self # helper to remove constants after test-runs. diff --git a/spec/shared/freeze_method_behavior.rb b/spec/shared/freeze_method_behavior.rb index 6b50f745..c475dabf 100644 --- a/spec/shared/freeze_method_behavior.rb +++ b/spec/shared/freeze_method_behavior.rb @@ -25,7 +25,7 @@ it 'prevents future modifications' do subject - expectation = raise_error(expected_exception_class,expected_exception_message) + expectation = raise_error(expected_exception_class, expected_exception_message) expect { object.instance_variable_set(:@foo, :bar) }.to(expectation) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7bd9c881..9c0ca846 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,5 @@ -if RUBY_ENGINE == "rbx" - require "codeclimate-test-reporter" +if RUBY_ENGINE == 'rbx' + require 'codeclimate-test-reporter' CodeClimate::TestReporter.start end @@ -22,7 +22,7 @@ def self.warn(*) RSpec.configure do |config| # Remove anonymous- and example- Attribute classes from Attribute descendants config.after :all do - stack = [ Virtus::Attribute ] + stack = [Virtus::Attribute] while klass = stack.pop klass.descendants.delete_if do |descendant| descendant.name.nil? || descendant.name.empty? || descendant.name.start_with?('Examples::') diff --git a/spec/unit/virtus/attribute/boolean/coerce_spec.rb b/spec/unit/virtus/attribute/boolean/coerce_spec.rb index 1cbc3dfd..1868276c 100644 --- a/spec/unit/virtus/attribute/boolean/coerce_spec.rb +++ b/spec/unit/virtus/attribute/boolean/coerce_spec.rb @@ -21,7 +21,7 @@ end context 'when strict is turned on' do - let(:options) { { :strict => true } } + let(:options) { { strict: true } } context 'with a coercible input' do let(:input) { 1 } diff --git a/spec/unit/virtus/attribute/class_methods/build_spec.rb b/spec/unit/virtus/attribute/class_methods/build_spec.rb index b9427ed2..d6d18522 100644 --- a/spec/unit/virtus/attribute/class_methods/build_spec.rb +++ b/spec/unit/virtus/attribute/class_methods/build_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Virtus::Attribute, '.build' do - subject { described_class.build(type, options.merge(:name => name)) } + subject { described_class.build(type, options.merge(name: name)) } let(:name) { :test } let(:type) { String } @@ -37,7 +37,7 @@ end context 'when coercion is turned off in options' do - let(:options) { { :coerce => false } } + let(:options) { { coerce: false } } it_behaves_like 'a valid attribute instance' @@ -45,7 +45,7 @@ end context 'when options specify reader visibility' do - let(:options) { { :reader => :private } } + let(:options) { { reader: :private } } it_behaves_like 'a valid attribute instance' @@ -54,7 +54,7 @@ end context 'when options specify writer visibility' do - let(:options) { { :writer => :private } } + let(:options) { { writer: :private } } it_behaves_like 'a valid attribute instance' @@ -63,7 +63,7 @@ end context 'when options specify lazy accessor' do - let(:options) { { :lazy => true } } + let(:options) { { lazy: true } } it_behaves_like 'a valid attribute instance' @@ -71,7 +71,7 @@ end context 'when options specify strict mode' do - let(:options) { { :strict => true } } + let(:options) { { strict: true } } it_behaves_like 'a valid attribute instance' @@ -79,7 +79,7 @@ end context 'when options specify nullify blank mode' do - let(:options) { { :nullify_blank => true } } + let(:options) { { nullify_blank: true } } it_behaves_like 'a valid attribute instance' diff --git a/spec/unit/virtus/attribute/coerce_spec.rb b/spec/unit/virtus/attribute/coerce_spec.rb index a9d11906..74bc33b0 100644 --- a/spec/unit/virtus/attribute/coerce_spec.rb +++ b/spec/unit/virtus/attribute/coerce_spec.rb @@ -5,10 +5,10 @@ fake(:coercer) { Virtus::Attribute::Coercer } - let(:object) { + let(:object) do described_class.build(String, - :coercer => coercer, :strict => strict, :required => required, :nullify_blank => nullify_blank) - } + coercer: coercer, strict: strict, required: required, nullify_blank: nullify_blank) + end let(:required) { true } let(:nullify_blank) { false } diff --git a/spec/unit/virtus/attribute/coercible_predicate_spec.rb b/spec/unit/virtus/attribute/coercible_predicate_spec.rb index 8e328211..30aff31f 100644 --- a/spec/unit/virtus/attribute/coercible_predicate_spec.rb +++ b/spec/unit/virtus/attribute/coercible_predicate_spec.rb @@ -4,7 +4,7 @@ subject { object.coercible? } let(:object) { described_class.build(String, options) } - let(:options) { Hash[:coerce => coerce] } + let(:options) { Hash[coerce: coerce] } context 'when :coerce is set to true' do let(:coerce) { true } diff --git a/spec/unit/virtus/attribute/collection/coerce_spec.rb b/spec/unit/virtus/attribute/collection/coerce_spec.rb index e4ad256a..fff9effe 100644 --- a/spec/unit/virtus/attribute/collection/coerce_spec.rb +++ b/spec/unit/virtus/attribute/collection/coerce_spec.rb @@ -9,11 +9,11 @@ fake(:member_type) { Virtus::Attribute } let(:member_primitive) { Integer } - let(:input) { ['1', '2'] } + let(:input) { %w(1 2) } - let(:object) { - described_class.build(Array[member_primitive], :coercer => coercer, :member_type => member_type) - } + let(:object) do + described_class.build(Array[member_primitive], coercer: coercer, member_type: member_type) + end it 'uses coercer to coerce members' do mock(coercer).call(input) { input } @@ -41,7 +41,7 @@ context 'when member type is a hash with key/value coercion' do let(:member_primitive) { Hash[String => Integer] } let(:member_attribute) { Virtus::Attribute.build(member_primitive) } - let(:input) { [{:one => '1'}, {:two => '2'}] } + let(:input) { [{ one: '1' }, { two: '2' }] } let(:output) { [member_attribute.coerce(input.first), member_attribute.coerce(input.last)] } let(:object) { described_class.build(Array[member_primitive]) } @@ -59,11 +59,11 @@ let(:member_primitive) { Integer } - let(:object) { + let(:object) do described_class.build( Array[member_primitive], coercer: coercer, member_type: member_type ) - } + end it 'returns nil' do mock(coercer).call(input) { input } diff --git a/spec/unit/virtus/attribute/comparison_spec.rb b/spec/unit/virtus/attribute/comparison_spec.rb index 796cd209..79fde95f 100644 --- a/spec/unit/virtus/attribute/comparison_spec.rb +++ b/spec/unit/virtus/attribute/comparison_spec.rb @@ -1,20 +1,20 @@ require 'spec_helper' describe Virtus::Attribute, '#== (defined by including Virtus::Equalizer)' do - let(:attribute) { described_class.build(String, :name => :name) } + let(:attribute) { described_class.build(String, name: :name) } it 'returns true when attributes have same type and options' do - equal_attribute = described_class.build(String, :name => :name) + equal_attribute = described_class.build(String, name: :name) expect(attribute == equal_attribute).to be_truthy end it 'returns false when attributes have different type' do - different_attribute = described_class.build(Integer, :name => :name) + different_attribute = described_class.build(Integer, name: :name) expect(attribute == different_attribute).to be_falsey end it 'returns false when attributes have different options' do - different_attribute = described_class.build(Integer, :name => :name_two) + different_attribute = described_class.build(Integer, name: :name_two) expect(attribute == different_attribute).to be_falsey end end diff --git a/spec/unit/virtus/attribute/custom_collection_spec.rb b/spec/unit/virtus/attribute/custom_collection_spec.rb index 423d2b50..2d616307 100644 --- a/spec/unit/virtus/attribute/custom_collection_spec.rb +++ b/spec/unit/virtus/attribute/custom_collection_spec.rb @@ -3,7 +3,7 @@ describe Virtus::Attribute::Collection, 'custom subclass' do subject { attribute_class.build(primitive) } - let(:primitive) { Class.new { include Enumerable } } + let(:primitive) { Class.new { include Enumerable } } after do described_class.descendants.delete(attribute_class) diff --git a/spec/unit/virtus/attribute/defined_spec.rb b/spec/unit/virtus/attribute/defined_spec.rb index 03f142d5..e1fc4361 100644 --- a/spec/unit/virtus/attribute/defined_spec.rb +++ b/spec/unit/virtus/attribute/defined_spec.rb @@ -3,7 +3,7 @@ describe Virtus::Attribute, '#defined?' do subject { object.defined?(instance) } - let(:object) { described_class.build(String, :name => name) } + let(:object) { described_class.build(String, name: name) } let(:model) { Class.new { attr_accessor :test } } let(:name) { :test } diff --git a/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb b/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb index 34670690..6595fa62 100644 --- a/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb +++ b/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb @@ -7,7 +7,7 @@ let(:options) { {} } context 'when primitive is OpenStruct' do - let(:model) { OpenStruct } + let(:model) { OpenStruct } context 'when input is an attribute hash' do let(:input) { Hash[name: 'Piotr', age: 30] } @@ -39,7 +39,7 @@ end context 'when primitive is Struct' do - let(:model) { Struct.new(:name, :age) } + let(:model) { Struct.new(:name, :age) } context 'when input is an attribute hash' do let(:input) { ['Piotr', 30] } @@ -72,7 +72,7 @@ context 'when :strict mode is enabled' do let(:model) { Struct.new(:name) } - let(:options) { { :strict => true } } + let(:options) { { strict: true } } context 'when input is coercible' do let(:input) { ['Piotr'] } diff --git a/spec/unit/virtus/attribute/get_spec.rb b/spec/unit/virtus/attribute/get_spec.rb index 474efbb9..76362d13 100644 --- a/spec/unit/virtus/attribute/get_spec.rb +++ b/spec/unit/virtus/attribute/get_spec.rb @@ -3,7 +3,7 @@ describe Virtus::Attribute, '#get' do subject { object.get(instance) } - let(:object) { described_class.build(String, options.update(:name => name)) } + let(:object) { described_class.build(String, options.update(name: name)) } let(:model) { Class.new { attr_accessor :test } } let(:name) { :test } @@ -20,7 +20,7 @@ end context 'with :lazy is set to true' do - let(:options) { { :lazy => true, :default => value } } + let(:options) { { lazy: true, default: value } } it { is_expected.to eql(value) } diff --git a/spec/unit/virtus/attribute/hash/class_methods/build_spec.rb b/spec/unit/virtus/attribute/hash/class_methods/build_spec.rb index 99427500..4156c5b9 100644 --- a/spec/unit/virtus/attribute/hash/class_methods/build_spec.rb +++ b/spec/unit/virtus/attribute/hash/class_methods/build_spec.rb @@ -96,7 +96,7 @@ context 'when strict mode is used' do let(:type) { Hash[String => Integer] } - let(:options) { { :strict => true } } + let(:options) { { strict: true } } it 'sets the strict mode for key/value types' do expect(subject.key_type).to be_strict diff --git a/spec/unit/virtus/attribute/hash/coerce_spec.rb b/spec/unit/virtus/attribute/hash/coerce_spec.rb index 502cea4f..ae60bcbe 100644 --- a/spec/unit/virtus/attribute/hash/coerce_spec.rb +++ b/spec/unit/virtus/attribute/hash/coerce_spec.rb @@ -7,17 +7,23 @@ fake(:key_type) { Virtus::Attribute } fake(:value_type) { Virtus::Attribute } - let(:object) { + let(:object) do described_class.build(Hash[key_primitive => value_primitive], options) - } + end let(:options) { {} } context 'when input is coercible to hash' do - let(:input) { Class.new { def to_hash; { :hello => 'World' }; end }.new } + let(:input) do + Class.new do + def to_hash + { hello: 'World' } + end + end.new + end let(:object) { described_class.build(Hash) } - it { is_expected.to eq(:hello => 'World') } + it { is_expected.to eq(hello: 'World') } end context 'when input is not coercible to hash' do @@ -29,9 +35,9 @@ context 'when input is a hash' do context 'when key/value types are primitives' do - let(:options) { - { :coercer => coercer, :key_type => key_type, :value_type => value_type } - } + let(:options) do + { coercer: coercer, key_type: key_type, value_type: value_type } + end let(:key_primitive) { String } let(:value_primitive) { Integer } @@ -63,8 +69,8 @@ let(:key_primitive) { OpenStruct } let(:value_primitive) { Struct.new(:id) } - let(:input) { Hash[{:name => 'Test'} => [1]] } - let(:output) { Hash[key_primitive.new(:name => 'Test') => value_primitive.new(1)] } + let(:input) { Hash[{ name: 'Test' } => [1]] } + let(:output) { Hash[key_primitive.new(name: 'Test') => value_primitive.new(1)] } it 'coerces keys and values' do # FIXME: expect(subject).to eq(output) crashes in rspec @@ -81,7 +87,7 @@ let(:key_attribute) { Virtus::Attribute.build(key_primitive) } let(:value_attribute) { Virtus::Attribute.build(value_primitive) } - let(:input) { Hash[[1, 2], {:one => '1', :two => '2'}] } + let(:input) { Hash[[1, 2], { one: '1', two: '2' }] } let(:output) { Hash[key_attribute.coerce(input.keys.first) => value_attribute.coerce(input.values.first)] } it 'coerces keys and values' do diff --git a/spec/unit/virtus/attribute/lazy_predicate_spec.rb b/spec/unit/virtus/attribute/lazy_predicate_spec.rb index 8806c0c9..97ea3fb7 100644 --- a/spec/unit/virtus/attribute/lazy_predicate_spec.rb +++ b/spec/unit/virtus/attribute/lazy_predicate_spec.rb @@ -4,7 +4,7 @@ subject { object.lazy? } let(:object) { described_class.build(String, options) } - let(:options) { Hash[:lazy => lazy] } + let(:options) { Hash[lazy: lazy] } context 'when :lazy is set to true' do let(:lazy) { true } diff --git a/spec/unit/virtus/attribute/rename_spec.rb b/spec/unit/virtus/attribute/rename_spec.rb index f314cdbc..9c7a6867 100644 --- a/spec/unit/virtus/attribute/rename_spec.rb +++ b/spec/unit/virtus/attribute/rename_spec.rb @@ -3,8 +3,8 @@ describe Virtus::Attribute, '#rename' do subject { object.rename(:bar) } - let(:object) { described_class.build(String, :name => :foo, :strict => true) } - let(:other) { described_class.build(String, :name => :bar, :strict => true) } + let(:object) { described_class.build(String, name: :foo, strict: true) } + let(:other) { described_class.build(String, name: :bar, strict: true) } describe '#name' do subject { super().name } diff --git a/spec/unit/virtus/attribute/required_predicate_spec.rb b/spec/unit/virtus/attribute/required_predicate_spec.rb index 57dd1d20..cc5a5da3 100644 --- a/spec/unit/virtus/attribute/required_predicate_spec.rb +++ b/spec/unit/virtus/attribute/required_predicate_spec.rb @@ -3,7 +3,7 @@ describe Virtus::Attribute, '#required?' do subject { object.required? } - let(:object) { described_class.build(String, :required => required) } + let(:object) { described_class.build(String, required: required) } context 'when required option is true' do let(:required) { true } diff --git a/spec/unit/virtus/attribute/set_default_value_spec.rb b/spec/unit/virtus/attribute/set_default_value_spec.rb index 9151b846..c9e3fd88 100644 --- a/spec/unit/virtus/attribute/set_default_value_spec.rb +++ b/spec/unit/virtus/attribute/set_default_value_spec.rb @@ -1,9 +1,18 @@ require 'spec_helper' describe Virtus::Attribute, '#set_default_value' do - let(:object) { described_class.build(String, options.merge(:name => name, :default => default)) } + let(:object) { described_class.build(String, options.merge(name: name, default: default)) } + + let(:model) do + Class.new do + def name + 'model' + end + + attr_reader :test + end + end - let(:model) { Class.new { def name; 'model'; end; attr_reader :test } } let(:name) { :test } let(:instance) { model.new } let(:options) { {} } @@ -29,7 +38,7 @@ context 'with a non-clonable object' do subject { instance } - let(:object) { described_class.build('Boolean', options.merge(:name => name, :default => default)) } + let(:object) { described_class.build('Boolean', options.merge(name: name, default: default)) } let(:default) { true } describe '#test' do @@ -62,7 +71,7 @@ context 'with a callable' do subject { instance } - let(:default) { lambda { |model, attribute| "#{model.name}-#{attribute.name}" } } + let(:default) { ->(model, attribute) { "#{model.name}-#{attribute.name}" } } describe '#test' do subject { super().test } @@ -77,7 +86,15 @@ let(:default) { :set_test } context 'when method is public' do - let(:model) { Class.new { attr_reader :test; def set_test; @test = 'hello world'; end } } + let(:model) do + Class.new do + attr_reader :test + + def set_test + @test = 'hello world' + end + end + end describe '#test' do subject { super().test } @@ -86,7 +103,17 @@ end context 'when method is private' do - let(:model) { Class.new { attr_reader :test; private; def set_test; @test = 'hello world'; end } } + let(:model) do + Class.new do + attr_reader :test + + private + + def set_test + @test = 'hello world' + end + end + end describe '#test' do subject { super().test } diff --git a/spec/unit/virtus/attribute/set_spec.rb b/spec/unit/virtus/attribute/set_spec.rb index cff898b1..e40b7ee5 100644 --- a/spec/unit/virtus/attribute/set_spec.rb +++ b/spec/unit/virtus/attribute/set_spec.rb @@ -3,7 +3,7 @@ describe Virtus::Attribute, '#set' do subject { object.set(instance, value) } - let(:object) { described_class.build(String, options.merge(:name => name)) } + let(:object) { described_class.build(String, options.merge(name: name)) } let(:model) { Class.new { attr_reader :test } } let(:name) { :test } diff --git a/spec/unit/virtus/attribute/value_coerced_predicate_spec.rb b/spec/unit/virtus/attribute/value_coerced_predicate_spec.rb index 3cd8c649..2b173e2c 100644 --- a/spec/unit/virtus/attribute/value_coerced_predicate_spec.rb +++ b/spec/unit/virtus/attribute/value_coerced_predicate_spec.rb @@ -6,13 +6,13 @@ let(:object) { described_class.build(String) } context 'when input is coerced' do - let(:input) { '1' } + let(:input) { '1' } it { is_expected.to be(true) } end context 'when input is not coerced' do - let(:input) { 1 } + let(:input) { 1 } it { is_expected.to be(false) } end diff --git a/spec/unit/virtus/attribute_set/append_spec.rb b/spec/unit/virtus/attribute_set/append_spec.rb index 577b07ed..47e833a3 100644 --- a/spec/unit/virtus/attribute_set/append_spec.rb +++ b/spec/unit/virtus/attribute_set/append_spec.rb @@ -9,39 +9,39 @@ let(:name) { :name } context 'with a new attribute' do - let(:attribute) { Virtus::Attribute.build(String, :name => name) } + let(:attribute) { Virtus::Attribute.build(String, name: name) } it { is_expected.to equal(object) } it 'adds an attribute' do - expect { subject }.to change { object.to_a }. - from(attributes). - to([ attribute ]) + expect { subject }.to change { object.to_a } + .from(attributes) + .to([attribute]) end it 'indexes the new attribute under its #name property' do - expect { subject }.to change { object[name] }. - from(nil). - to(attribute) + expect { subject }.to change { object[name] } + .from(nil) + .to(attribute) end it 'indexes the new attribute under the string version of its #name property' do - expect { subject }.to change { object[name.to_s] }. - from(nil). - to(attribute) + expect { subject }.to change { object[name.to_s] } + .from(nil) + .to(attribute) end end context 'with a duplicate attribute' do - let(:attributes) { [Virtus::Attribute.build(String, :name => name)] } - let(:attribute) { Virtus::Attribute.build(String, :name => name) } + let(:attributes) { [Virtus::Attribute.build(String, name: name)] } + let(:attribute) { Virtus::Attribute.build(String, name: name) } it { is_expected.to equal(object) } - it "replaces the original attribute object" do - expect { subject }.to change { object.to_a.map(&:__id__) }. - from(attributes.map(&:__id__)). - to([attribute.__id__]) + it 'replaces the original attribute object' do + expect { subject }.to change { object.to_a.map(&:__id__) } + .from(attributes.map(&:__id__)) + .to([attribute.__id__]) end end end diff --git a/spec/unit/virtus/attribute_set/define_reader_method_spec.rb b/spec/unit/virtus/attribute_set/define_reader_method_spec.rb index 4fd58acc..fddee2c0 100644 --- a/spec/unit/virtus/attribute_set/define_reader_method_spec.rb +++ b/spec/unit/virtus/attribute_set/define_reader_method_spec.rb @@ -3,33 +3,33 @@ describe Virtus::AttributeSet, '#define_reader_method' do subject(:attribute_set) { described_class.new } - let(:attribute) { Virtus::Attribute.build(String, :name => method_name) } + let(:attribute) { Virtus::Attribute.build(String, name: method_name) } let(:method_name) { :foo_bar } before do attribute_set.define_reader_method(attribute, method_name, visibility) end - context "with public visibility" do + context 'with public visibility' do let(:visibility) { :public } - it "defines public writer" do + it 'defines public writer' do expect(attribute_set.public_instance_methods).to include(method_name) end end - context "with private visibility" do + context 'with private visibility' do let(:visibility) { :private } - it "defines public writer" do + it 'defines public writer' do expect(attribute_set.private_instance_methods).to include(method_name) end end - context "with protected visibility" do + context 'with protected visibility' do let(:visibility) { :protected } - it "defines protected writer" do + it 'defines protected writer' do expect(attribute_set.protected_instance_methods).to include(method_name) end end diff --git a/spec/unit/virtus/attribute_set/define_writer_method_spec.rb b/spec/unit/virtus/attribute_set/define_writer_method_spec.rb index 4814a03e..b6f3b61c 100644 --- a/spec/unit/virtus/attribute_set/define_writer_method_spec.rb +++ b/spec/unit/virtus/attribute_set/define_writer_method_spec.rb @@ -3,33 +3,33 @@ describe Virtus::AttributeSet, '#define_writer_method' do subject(:attribute_set) { described_class.new } - let(:attribute) { Virtus::Attribute.build(String, :name => method_name) } + let(:attribute) { Virtus::Attribute.build(String, name: method_name) } let(:method_name) { :foo_bar } before do attribute_set.define_writer_method(attribute, method_name, visibility) end - context "with public visibility" do + context 'with public visibility' do let(:visibility) { :public } - it "defines public writer" do + it 'defines public writer' do expect(attribute_set.public_instance_methods).to include(method_name) end end - context "with private visibility" do + context 'with private visibility' do let(:visibility) { :private } - it "defines private writer" do + it 'defines private writer' do expect(attribute_set.private_instance_methods).to include(method_name) end end - context "with protected visibility" do + context 'with protected visibility' do let(:visibility) { :protected } - it "defines protected writer" do + it 'defines protected writer' do expect(attribute_set.protected_instance_methods).to include(method_name) end end diff --git a/spec/unit/virtus/attribute_set/each_spec.rb b/spec/unit/virtus/attribute_set/each_spec.rb index 86c9fd6b..584368ac 100644 --- a/spec/unit/virtus/attribute_set/each_spec.rb +++ b/spec/unit/virtus/attribute_set/each_spec.rb @@ -4,8 +4,8 @@ subject(:attribute_set) { described_class.new(parent, attributes) } let(:name) { :name } - let(:attribute) { Virtus::Attribute.build(String, :name => :name) } - let(:attributes) { [ attribute ] } + let(:attribute) { Virtus::Attribute.build(String, name: :name) } + let(:attributes) { [attribute] } let(:parent) { described_class.new } let(:yields) { Set[] } @@ -28,15 +28,15 @@ it { is_expected.to equal(attribute_set) } it 'yields the expected attributes' do - expect { subject }.to change { yields.dup }. - from(Set[]). - to(attributes.to_set) + expect { subject }.to change { yields.dup } + .from(Set[]) + .to(attributes.to_set) end end context 'when the parent has attributes that are not duplicates' do - let(:parent_attribute) { Virtus::Attribute.build(String, :name => :parent_name) } - let(:parent) { described_class.new([ parent_attribute ]) } + let(:parent_attribute) { Virtus::Attribute.build(String, name: :parent_name) } + let(:parent) { described_class.new([parent_attribute]) } it { is_expected.to equal(attribute_set) } @@ -50,15 +50,15 @@ end context 'when the parent has attributes that are duplicates' do - let(:parent_attribute) { Virtus::Attribute.build(String, :name => name) } - let(:parent) { described_class.new([ parent_attribute ]) } + let(:parent_attribute) { Virtus::Attribute.build(String, name: name) } + let(:parent) { described_class.new([parent_attribute]) } it { is_expected.to equal(attribute_set) } it 'yields the expected attributes' do - expect { subject }.to change { yields.dup }. - from(Set[]). - to(Set[ attribute ]) + expect { subject }.to change { yields.dup } + .from(Set[]) + .to(Set[attribute]) end end end diff --git a/spec/unit/virtus/attribute_set/element_reference_spec.rb b/spec/unit/virtus/attribute_set/element_reference_spec.rb index 73fc3e24..7bbe6c56 100644 --- a/spec/unit/virtus/attribute_set/element_reference_spec.rb +++ b/spec/unit/virtus/attribute_set/element_reference_spec.rb @@ -3,9 +3,9 @@ describe Virtus::AttributeSet, '#[]' do subject { object[name] } - let(:name) { :name } - let(:attribute) { Virtus::Attribute.build(String, :name => :name) } - let(:attributes) { [ attribute ] } + let(:name) { :name } + let(:attribute) { Virtus::Attribute.build(String, name: :name) } + let(:attributes) { [attribute] } let(:parent) { described_class.new } let(:object) { described_class.new(parent, attributes) } diff --git a/spec/unit/virtus/attribute_set/element_set_spec.rb b/spec/unit/virtus/attribute_set/element_set_spec.rb index 8d9c6385..2ad32c05 100644 --- a/spec/unit/virtus/attribute_set/element_set_spec.rb +++ b/spec/unit/virtus/attribute_set/element_set_spec.rb @@ -9,12 +9,12 @@ let(:name) { :name } context 'with a new attribute' do - let(:attribute) { Virtus::Attribute.build(String, :name => name) } + let(:attribute) { Virtus::Attribute.build(String, name: name) } it { is_expected.to equal(attribute) } it 'adds an attribute' do - expect { subject }.to change { object.to_a }.from(attributes).to([ attribute ]) + expect { subject }.to change { object.to_a }.from(attributes).to([attribute]) end it 'allows #[] to access the attribute with a symbol' do @@ -26,39 +26,39 @@ end it 'allows #reset to track overridden attributes' do - expect { subject }.to change { object.reset.to_a }.from(attributes).to([ attribute ]) + expect { subject }.to change { object.reset.to_a }.from(attributes).to([attribute]) end end context 'with a duplicate attribute' do - let(:original) { Virtus::Attribute.build(String, :name => name) } - let(:attributes) { [ original ] } - let(:attribute) { Virtus::Attribute.build(String, :name => name) } + let(:original) { Virtus::Attribute.build(String, name: name) } + let(:attributes) { [original] } + let(:attribute) { Virtus::Attribute.build(String, name: name) } it { is_expected.to equal(attribute) } - it "replaces the original attribute object" do - expect { subject }.to change { object.to_a.map(&:__id__) }. - from(attributes.map(&:__id__)). - to([attribute.__id__]) + it 'replaces the original attribute object' do + expect { subject }.to change { object.to_a.map(&:__id__) } + .from(attributes.map(&:__id__)) + .to([attribute.__id__]) end it 'allows #[] to access the attribute with a string' do - expect { subject }.to change { object['name'].__id__ }. - from(original.__id__). - to(attribute.__id__) + expect { subject }.to change { object['name'].__id__ } + .from(original.__id__) + .to(attribute.__id__) end it 'allows #[] to access the attribute with a symbol' do - expect { subject }.to change { object[:name].__id__ }. - from(original.__id__). - to(attribute.__id__) + expect { subject }.to change { object[:name].__id__ } + .from(original.__id__) + .to(attribute.__id__) end it 'allows #reset to track overridden attributes' do - expect { subject }.to change { object.reset.to_a.map(&:__id__) }. - from(attributes.map(&:__id__)). - to([attribute.__id__]) + expect { subject }.to change { object.reset.to_a.map(&:__id__) } + .from(attributes.map(&:__id__)) + .to([attribute.__id__]) end end end diff --git a/spec/unit/virtus/attribute_set/merge_spec.rb b/spec/unit/virtus/attribute_set/merge_spec.rb index 9981ece8..5a7e093b 100644 --- a/spec/unit/virtus/attribute_set/merge_spec.rb +++ b/spec/unit/virtus/attribute_set/merge_spec.rb @@ -10,7 +10,7 @@ context 'with a new attribute' do let(:attributes) { [] } - let(:attribute) { Virtus::Attribute.build(String, :name => name) } + let(:attribute) { Virtus::Attribute.build(String, name: name) } it { is_expected.to equal(object) } @@ -20,15 +20,15 @@ end context 'with a duplicate attribute' do - let(:attributes) { [Virtus::Attribute.build(String, :name => name)] } - let(:attribute) { Virtus::Attribute.build(String, :name => name) } + let(:attributes) { [Virtus::Attribute.build(String, name: name)] } + let(:attribute) { Virtus::Attribute.build(String, name: name) } it { is_expected.to equal(object) } - it "replaces the original attribute object" do - expect { subject }.to change { object.to_a.map(&:__id__) }. - from(attributes.map(&:__id__)). - to([attribute.__id__]) + it 'replaces the original attribute object' do + expect { subject }.to change { object.to_a.map(&:__id__) } + .from(attributes.map(&:__id__)) + .to([attribute.__id__]) end end end diff --git a/spec/unit/virtus/attribute_set/reset_spec.rb b/spec/unit/virtus/attribute_set/reset_spec.rb index b0d6698f..6436ae03 100644 --- a/spec/unit/virtus/attribute_set/reset_spec.rb +++ b/spec/unit/virtus/attribute_set/reset_spec.rb @@ -4,8 +4,8 @@ subject { object.reset } let(:name) { :name } - let(:attribute) { Virtus::Attribute.build(String, :name => :name) } - let(:attributes) { [ attribute ] } + let(:attribute) { Virtus::Attribute.build(String, name: :name) } + let(:attributes) { [attribute] } let(:object) { described_class.new(parent, attributes) } context 'when the parent has no attributes' do @@ -15,38 +15,38 @@ describe '#to_set' do subject { super().to_set } - it { is_expected.to eq(Set[ attribute ]) } + it { is_expected.to eq(Set[attribute]) } end end context 'when the parent has attributes that are not duplicates' do - let(:parent_attribute) { Virtus::Attribute.build(String, :name => :parent_name) } - let(:parent) { described_class.new([ parent_attribute ]) } + let(:parent_attribute) { Virtus::Attribute.build(String, name: :parent_name) } + let(:parent) { described_class.new([parent_attribute]) } it { is_expected.to equal(object) } describe '#to_set' do subject { super().to_set } - it { is_expected.to eq(Set[ attribute, parent_attribute ]) } + it { is_expected.to eq(Set[attribute, parent_attribute]) } end end context 'when the parent has attributes that are duplicates' do - let(:parent_attribute) { Virtus::Attribute.build(String, :name => name) } - let(:parent) { described_class.new([ parent_attribute ]) } + let(:parent_attribute) { Virtus::Attribute.build(String, name: name) } + let(:parent) { described_class.new([parent_attribute]) } it { is_expected.to equal(object) } describe '#to_set' do subject { super().to_set } - it { is_expected.to eq(Set[ attribute ]) } + it { is_expected.to eq(Set[attribute]) } end end context 'when the parent has changed' do - let(:parent_attribute) { Virtus::Attribute.build(String, :name => :parent_name) } - let(:parent) { described_class.new([ parent_attribute ]) } - let(:new_attribute) { Virtus::Attribute.build(String, :name => :parent_name) } + let(:parent_attribute) { Virtus::Attribute.build(String, name: :parent_name) } + let(:parent) { described_class.new([parent_attribute]) } + let(:new_attribute) { Virtus::Attribute.build(String, name: :parent_name) } it { is_expected.to equal(object) } diff --git a/spec/unit/virtus/attribute_spec.rb b/spec/unit/virtus/attribute_spec.rb index f280e9c8..2b369b72 100644 --- a/spec/unit/virtus/attribute_spec.rb +++ b/spec/unit/virtus/attribute_spec.rb @@ -163,13 +163,13 @@ class Test end context 'with a module' do - let(:mod) { - Module.new { + let(:mod) do + Module.new do include Virtus attribute :test, String - } - } + end + end let(:model) { Class.new } @@ -210,13 +210,13 @@ class Test context 'using custom module' do subject { model.new } - let(:model) { - Class.new { + let(:model) do + Class.new do include Virtus.model { |config| config.coerce = false } attribute :test, String - } - } + end + end it { is_expected.to respond_to(:test) } it { is_expected.to respond_to(:test=) } diff --git a/spec/unit/virtus/attributes_reader_spec.rb b/spec/unit/virtus/attributes_reader_spec.rb index 82774c37..4764038c 100644 --- a/spec/unit/virtus/attributes_reader_spec.rb +++ b/spec/unit/virtus/attributes_reader_spec.rb @@ -1,24 +1,23 @@ require 'spec_helper' describe Virtus, '#attributes' do - shared_examples_for 'attribute hash' do it 'includes all attributes' do - subject.attributes = { :test => 'Hello World', :test_priv => 'Yo' } + subject.attributes = { test: 'Hello World', test_priv: 'Yo' } - expect(subject.attributes).to eql(:test => 'Hello World') + expect(subject.attributes).to eql(test: 'Hello World') end end context 'with a class' do - let(:model) { - Class.new { + let(:model) do + Class.new do include Virtus attribute :test, String - attribute :test_priv, String, :reader => :private - } - } + attribute :test_priv, String, reader: :private + end + end it_behaves_like 'attribute hash' do subject { model.new } @@ -33,7 +32,7 @@ before do subject.extend(Virtus) subject.attribute :test, String - subject.attribute :test_priv, String, :reader => :private + subject.attribute :test_priv, String, reader: :private end it_behaves_like 'attribute hash' diff --git a/spec/unit/virtus/attributes_writer_spec.rb b/spec/unit/virtus/attributes_writer_spec.rb index e52ec1ac..cfd464ca 100644 --- a/spec/unit/virtus/attributes_writer_spec.rb +++ b/spec/unit/virtus/attributes_writer_spec.rb @@ -1,29 +1,28 @@ require 'spec_helper' describe Virtus, '#attributes=' do - shared_examples_for 'mass-assignment' do it 'allows writing known attributes' do - subject.attributes = { :test => 'Hello World' } + subject.attributes = { test: 'Hello World' } expect(subject.test).to eql('Hello World') end it 'skips writing unknown attributes' do - subject.attributes = { :test => 'Hello World', :nothere => 'boom!' } + subject.attributes = { test: 'Hello World', nothere: 'boom!' } expect(subject.test).to eql('Hello World') end end context 'with a class' do - let(:model) { - Class.new { + let(:model) do + Class.new do include Virtus attribute :test, String - } - } + end + end it_behaves_like 'mass-assignment' do subject { model.new } diff --git a/spec/unit/virtus/class_methods/finalize_spec.rb b/spec/unit/virtus/class_methods/finalize_spec.rb index ee46987e..34297d6a 100644 --- a/spec/unit/virtus/class_methods/finalize_spec.rb +++ b/spec/unit/virtus/class_methods/finalize_spec.rb @@ -4,7 +4,7 @@ before do module Examples class Person - include Virtus.model(:finalize => false) + include Virtus.model(finalize: false) attribute :name, String attribute :articles, Array['Examples::Article'] @@ -12,7 +12,7 @@ class Person end class Article - include Virtus.model(:finalize => false) + include Virtus.model(finalize: false) attribute :posts, Hash['Examples::Person' => 'Examples::Post'] attribute :person, :'Examples::Person' diff --git a/spec/unit/virtus/class_methods/new_spec.rb b/spec/unit/virtus/class_methods/new_spec.rb index 63ca576b..9a9ff232 100644 --- a/spec/unit/virtus/class_methods/new_spec.rb +++ b/spec/unit/virtus/class_methods/new_spec.rb @@ -1,15 +1,15 @@ require 'spec_helper' describe Virtus, '.new' do - let(:model) { - Class.new { + let(:model) do + Class.new do include Virtus attribute :id, Integer - attribute :name, String, :default => 'John Doe' - attribute :email, String, :default => 'john@doe.com', :lazy => true, :writer => :private - } - } + attribute :name, String, default: 'John Doe' + attribute :email, String, default: 'john@doe.com', lazy: true, writer: :private + end + end context 'without attribute hash' do subject { model.new } @@ -24,7 +24,7 @@ end context 'with attribute hash' do - subject { model.new(:id => 1, :name => 'Jane Doe') } + subject { model.new(id: 1, name: 'Jane Doe') } it 'sets attributes with public writers' do expect(subject.id).to be(1) @@ -35,5 +35,4 @@ expect(subject.instance_variable_get('@email')).to be(nil) end end - end diff --git a/spec/unit/virtus/element_reader_spec.rb b/spec/unit/virtus/element_reader_spec.rb index 7353c00a..129671e0 100644 --- a/spec/unit/virtus/element_reader_spec.rb +++ b/spec/unit/virtus/element_reader_spec.rb @@ -3,13 +3,13 @@ describe Virtus, '#[]' do subject { object[:test] } - let(:model) { - Class.new { + let(:model) do + Class.new do include Virtus attribute :test, String - } - } + end + end let(:object) { model.new } diff --git a/spec/unit/virtus/element_writer_spec.rb b/spec/unit/virtus/element_writer_spec.rb index 44af0299..76e309af 100644 --- a/spec/unit/virtus/element_writer_spec.rb +++ b/spec/unit/virtus/element_writer_spec.rb @@ -3,13 +3,13 @@ describe Virtus, '#[]=' do subject { object[:test] = 'foo' } - let(:model) { - Class.new { + let(:model) do + Class.new do include Virtus attribute :test, String - } - } + end + end let(:object) { model.new } diff --git a/spec/unit/virtus/freeze_spec.rb b/spec/unit/virtus/freeze_spec.rb index aa018f99..79ece12a 100644 --- a/spec/unit/virtus/freeze_spec.rb +++ b/spec/unit/virtus/freeze_spec.rb @@ -3,15 +3,15 @@ describe Virtus, '#freeze' do subject { object.freeze } - let(:model) { - Class.new { + let(:model) do + Class.new do include Virtus - attribute :name, String, :default => 'foo', :lazy => true - attribute :age, Integer, :default => 30 - attribute :rand, Float, :default => Proc.new { rand } - } - } + attribute :name, String, default: 'foo', lazy: true + attribute :age, Integer, default: 30 + attribute :rand, Float, default: proc { rand } + end + end let(:object) { model.new } @@ -27,13 +27,13 @@ it { is_expected.to be(30) } end - it "does not change dynamic default values" do + it 'does not change dynamic default values' do original_value = object.rand object.freeze expect(object.rand).to eq original_value end - it "does not change default attributes that have been explicitly set" do + it 'does not change default attributes that have been explicitly set' do object.rand = 3.14 object.freeze expect(object.rand).to eq 3.14 diff --git a/spec/unit/virtus/model_spec.rb b/spec/unit/virtus/model_spec.rb index 0bfcc1e5..0fa7c55b 100644 --- a/spec/unit/virtus/model_spec.rb +++ b/spec/unit/virtus/model_spec.rb @@ -3,14 +3,14 @@ describe Virtus, '.model' do shared_examples_for 'a model with constructor' do it 'accepts attribute hash' do - instance = subject.new(:name => 'Jane') + instance = subject.new(name: 'Jane') expect(instance.name).to eql('Jane') end end shared_examples_for 'a model with mass-assignment' do let(:attributes) do - { :name => 'Jane', :something => nil } + { name: 'Jane', something: nil } end before do @@ -38,7 +38,7 @@ before do subject.send(:include, mod) - subject.attribute :name, String, :default => 'Jane' + subject.attribute :name, String, default: 'Jane' subject.attribute :something end @@ -68,9 +68,9 @@ it_behaves_like 'a model with mass-assignment' do let(:instance) { subject.new } - let(:attributes) { - { :name => 'Jane', :something => nil, :age => 23 } - } + let(:attributes) do + { name: 'Jane', something: nil, age: 23 } + end end it 'has its own attributes' do @@ -99,7 +99,7 @@ context 'when constructor is disabled' do subject { Class.new.send(:include, mod) } - let(:mod) { Virtus.model(:constructor => false) } + let(:mod) { Virtus.model(constructor: false) } it 'does not accept attribute hash in the constructor' do expect { subject.new({}) }.to raise_error(ArgumentError) @@ -107,7 +107,7 @@ end context 'when strict mode is enabled' do - let(:mod) { Virtus.model(:strict => true) } + let(:mod) { Virtus.model(strict: true) } let(:model) { Class.new } context 'with a class' do @@ -138,7 +138,7 @@ end context 'when mass-assignment is disabled' do - let(:mod) { Virtus.model(:mass_assignment => false) } + let(:mod) { Virtus.model(mass_assignment: false) } let(:model) { Class.new } context 'with a class' do @@ -165,7 +165,7 @@ end context 'when :required is set' do - let(:mod) { Virtus.model(:required => false) } + let(:mod) { Virtus.model(required: false) } let(:model) { Class.new } context 'with a class' do diff --git a/spec/unit/virtus/module_spec.rb b/spec/unit/virtus/module_spec.rb index 74bf59ea..b5f1705b 100644 --- a/spec/unit/virtus/module_spec.rb +++ b/spec/unit/virtus/module_spec.rb @@ -22,7 +22,7 @@ end context 'with constructor turned off' do - subject { Virtus.module(:constructor => false) } + subject { Virtus.module(constructor: false) } it_behaves_like 'a valid virtus object' do let(:instance) { model.new } @@ -34,7 +34,7 @@ end context 'with mass assignment is turned off' do - subject { Virtus.module(:mass_assignment => false) } + subject { Virtus.module(mass_assignment: false) } it_behaves_like 'a valid virtus object' @@ -45,7 +45,7 @@ end context 'with coercion turned off' do - subject { Virtus.module(:coerce => false) } + subject { Virtus.module(coerce: false) } it_behaves_like 'a valid virtus object' @@ -61,7 +61,7 @@ before do mod.send(:include, subject) - mod.attribute :name, String, :default => 'Jane' + mod.attribute :name, String, default: 'Jane' mod.attribute :something end @@ -116,11 +116,11 @@ context 'as a peer to another module within a class' do subject { Virtus.module } - let(:other) { Module.new } + let(:other) { Module.new } before do other.send(:include, Virtus.module) - other.attribute :last_name, String, :default => 'Doe' + other.attribute :last_name, String, default: 'Doe' other.attribute :something_else model.send(:include, mod) model.send(:include, other) @@ -142,17 +142,17 @@ context 'with multiple other modules mixed into it' do subject { Virtus.module } - let(:other) { Module.new } - let(:yet_another) { Module.new } + let(:other) { Module.new } + let(:yet_another) { Module.new } before do other.send(:include, Virtus.module) - other.attribute :last_name, String, :default => 'Doe' + other.attribute :last_name, String, default: 'Doe' other.attribute :something_else yet_another.send(:include, Virtus.module) yet_another.send(:include, mod) yet_another.send(:include, other) - yet_another.attribute :middle_name, String, :default => 'Foobar' + yet_another.attribute :middle_name, String, default: 'Foobar' model.send(:include, yet_another) end @@ -170,5 +170,4 @@ ) end end - end diff --git a/spec/unit/virtus/set_default_attributes_spec.rb b/spec/unit/virtus/set_default_attributes_spec.rb index ee059012..e8fc49cf 100644 --- a/spec/unit/virtus/set_default_attributes_spec.rb +++ b/spec/unit/virtus/set_default_attributes_spec.rb @@ -3,14 +3,14 @@ describe Virtus, '#set_default_attributes!' do subject { object.set_default_attributes! } - let(:model) { - Class.new { + let(:model) do + Class.new do include Virtus - attribute :name, String, :default => 'foo', :lazy => true - attribute :age, Integer, :default => 30 - } - } + attribute :name, String, default: 'foo', lazy: true + attribute :age, Integer, default: 30 + end + end let(:object) { model.new } diff --git a/spec/unit/virtus/value_object_spec.rb b/spec/unit/virtus/value_object_spec.rb index e536a676..4c8755a7 100644 --- a/spec/unit/virtus/value_object_spec.rb +++ b/spec/unit/virtus/value_object_spec.rb @@ -4,7 +4,7 @@ shared_examples_for 'a valid value object' do subject { model.new(attributes) } - let(:attributes) { Hash[:id => 1, :name => 'Jane Doe'] } + let(:attributes) { Hash[id: 1, name: 'Jane Doe'] } describe '#id' do subject { super().id } @@ -44,9 +44,9 @@ end it 'allows to construct new values using #with' do - new_instance = subject.with(:name => "John Doe") + new_instance = subject.with(name: 'John Doe') expect(new_instance.id).to eql(subject.id) - expect(new_instance.name).to eql("John Doe") + expect(new_instance.name).to eql('John Doe') end end @@ -59,10 +59,10 @@ end context 'using new values {} block' do - let(:model) { - model = Virtus.value_object(:coerce => false, :mass_assignment => mass_assignment) + let(:model) do + model = Virtus.value_object(coerce: false, mass_assignment: mass_assignment) - Class.new { + Class.new do include model def self.name @@ -73,8 +73,8 @@ def self.name attribute :id, Integer attribute :name, String end - } - } + end + end context 'without mass-assignment' do let(:mass_assignment) { false } @@ -89,18 +89,18 @@ def self.name it_behaves_like 'a valid value object with mass-assignment turned on' context 'with a model subclass' do - let(:subclass) { - Class.new(model) { + let(:subclass) do + Class.new(model) do values do attribute :email, String end - } - } + end + end it_behaves_like 'a valid value object' do subject { subclass.new(attributes) } - let(:attributes) { Hash[:id => 1, :name => 'Jane Doe', :email => 'jane@doe.com'] } + let(:attributes) { Hash[id: 1, name: 'Jane Doe', email: 'jane@doe.com'] } describe '#email' do subject { super().email } @@ -112,7 +112,7 @@ def self.name end it 'defines valid #== for a subclass' do - expect(subject == subject.class.new(attributes.merge(:id => 2))).to be(false) + expect(subject == subject.class.new(attributes.merge(id: 2))).to be(false) end end end @@ -120,8 +120,8 @@ def self.name end context 'using deprecated inclusion' do - let(:model) { - Class.new { + let(:model) do + Class.new do include Virtus::ValueObject def self.name @@ -130,8 +130,8 @@ def self.name attribute :id, Integer attribute :name, String - } - } + end + end it_behaves_like 'a valid value object' end diff --git a/virtus.gemspec b/virtus.gemspec index d73ba617..9eb566e4 100644 --- a/virtus.gemspec +++ b/virtus.gemspec @@ -3,19 +3,19 @@ require File.expand_path('../lib/virtus/version', __FILE__) Gem::Specification.new do |gem| - gem.name = "virtus" + gem.name = 'virtus' gem.version = Virtus::VERSION.dup - gem.authors = [ "Piotr Solnica" ] - gem.email = [ "piotr.solnica@gmail.com" ] - gem.description = "Attributes on Steroids for Plain Old Ruby Objects" + gem.authors = ['Piotr Solnica'] + gem.email = ['piotr.solnica@gmail.com'] + gem.description = 'Attributes on Steroids for Plain Old Ruby Objects' gem.summary = gem.description - gem.homepage = "https://github.com/solnic/virtus" + gem.homepage = 'https://github.com/solnic/virtus' gem.license = 'MIT' - gem.require_paths = [ "lib" ] + gem.require_paths = ['lib'] gem.files = `git ls-files`.split("\n") gem.test_files = `git ls-files -- {spec}/*`.split("\n") - gem.extra_rdoc_files = %w[LICENSE README.md TODO.md] + gem.extra_rdoc_files = %w(LICENSE README.md TODO.md) gem.add_dependency('descendants_tracker', '~> 0.0', '>= 0.0.3') gem.add_dependency('equalizer', '~> 0.0', '>= 0.0.9') From f4d5a9e6348fc110f7d39bcd7a1da4d4ce4af0e9 Mon Sep 17 00:00:00 2001 From: Filip Bartuzi Date: Wed, 18 Nov 2015 13:00:10 +0000 Subject: [PATCH 2/2] Change rubocop config for And/Or style Solves: https://github.com/dblock/virtus/commit/80202e321c4247f8c59274834b21f30d84cc4ff1#commitcomment-14453662 --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 04b22169..6287bb5b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,3 +3,6 @@ AllCops: - vendor/**/* inherit_from: .rubocop_todo.yml + +Style/AndOr: + EnforcedStyle: conditionals