diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..4d7c0559 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,17 @@ +AllCops: + Exclude: + - vendor/**/* + +inherit_from: .rubocop_todo.yml + +Style/AndOr: + EnforcedStyle: conditionals + +Style/SignalException: + EnforcedStyle: only_raise + +Style/HashSyntax: + EnforcedStyle: hash_rockets + +Style/AlignHash: + EnforcedHashRocketStyle: table diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 00000000..8cb1fc82 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,87 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2015-11-18 09:55:16 -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: 106 +# Configuration parameters: AllowURI, URISchemes. +Metrics/LineLength: + Max: 143 + +# 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..5a2414b3 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..1ea41763 100644 --- a/Guardfile +++ b/Guardfile @@ -1,5 +1,5 @@ -guard :rspec, spec_paths: 'spec/unit' do - #run all specs if configuration is modified +guard :rspec, :spec_paths => 'spec/unit' do + # run all specs if configuration is modified watch('Guardfile') { 'spec' } watch('Gemfile.lock') { 'spec' } watch('spec/spec_helper.rb') { 'spec' } diff --git a/Rakefile b/Rakefile index 179aabe1..78a0af50 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] +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..f3e2b9bd 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 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..c8c7ddfc 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 # @@ -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..683ab161 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 @@ -36,7 +36,6 @@ def determine_type 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 @@ -181,8 +179,6 @@ def determine_visibility writer_visibility = options.fetch(:writer, default_accessor) 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..27293e31 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 @@ -66,7 +65,7 @@ def self.build_type(definition) # @api private def self.merge_options!(type, options) - options[:member_type] ||= Attribute.build(type.member_type, strict: options[:strict]) + options[:member_type] ||= Attribute.build(type.member_type, :strict => options[:strict]) end # @api public @@ -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..9319c516 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}`" 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,7 +92,7 @@ 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[:key_type] ||= Attribute.build(type.key_type, :strict => options[:strict]) options[:value_type] ||= Attribute.build(type.value_type, :strict => options[:strict]) end @@ -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..762f02a5 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 @@ -19,8 +17,6 @@ def coerce(*) raise 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..be9565e8 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 @@ -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..0cb230e8 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 @@ -127,7 +122,5 @@ def extensions def options 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..5b88ab92 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 @@ -85,6 +84,5 @@ def assert_valid_name(name) raise 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..74f62911 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) @@ -67,6 +65,5 @@ def to_h :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..5691e21e 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 @@ -68,9 +66,9 @@ def attribute(name, type = nil, options = {}) # @see Virtus.default_value # # @api public - def values(&block) + def values private :attributes= if instance_methods.include?(:attributes=) - yield + yield if block_given? include(::Equalizer.new(*attribute_set.map(&:name))) end @@ -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..df433ba6 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 @@ -213,6 +207,5 @@ def assert_valid_name(name) raise 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..b88f66a7 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 @@ -44,7 +42,7 @@ def primitive raise 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..8419e414 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) @@ -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..8c30fbff 100644 --- a/spec/integration/attributes_attribute_spec.rb +++ b/spec/integration/attributes_attribute_spec.rb @@ -1,27 +1,26 @@ 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 - include Virtus.model(mass_assignment: false) + include Virtus.model(:mass_assignment => false) attribute :attributes end 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 - user = Examples::User.new(attributes: 'attributes string') + user = Examples::User.new(:attributes => 'attributes string') expect(user.attributes).to eq(nil) end end diff --git a/spec/integration/building_module_spec.rb b/spec/integration/building_module_spec.rb index ed492a21..1b86fa6e 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 diff --git a/spec/integration/collection_member_coercion_spec.rb b/spec/integration/collection_member_coercion_spec.rb index f6ef068a..687805a5 100644 --- a/spec/integration/collection_member_coercion_spec.rb +++ b/spec/integration/collection_member_coercion_spec.rb @@ -31,18 +31,24 @@ 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 +86,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..bc62c47e 100644 --- a/spec/integration/custom_collection_attributes_spec.rb +++ b/spec/integration/custom_collection_attributes_spec.rb @@ -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..ef1e299a 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,7 +13,7 @@ class Page include Virtus attribute :title, String - attribute :slug, String, :default => lambda { |post, attribute| post.title.downcase.gsub(' ', '-') }, :lazy => true + 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 @@ -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..75a8c88d 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 @@ -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) diff --git a/spec/integration/embedded_value_spec.rb b/spec/integration/embedded_value_spec.rb index f33b3477..7e65066f 100644 --- a/spec/integration/embedded_value_spec.rb +++ b/spec/integration/embedded_value_spec.rb @@ -26,17 +26,23 @@ 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', + 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 +52,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/hash_attributes_coercion_spec.rb b/spec/integration/hash_attributes_coercion_spec.rb index c225daa4..303f38a6 100644 --- a/spec/integration/hash_attributes_coercion_spec.rb +++ b/spec/integration/hash_attributes_coercion_spec.rb @@ -1,19 +1,24 @@ 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 +36,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..547118d4 100644 --- a/spec/integration/inheritance_spec.rb +++ b/spec/integration/inheritance_spec.rb @@ -9,7 +9,7 @@ class Base class First < Base attribute :id, Fixnum - attribute :name, String, default: ->(first, _) { "Named: #{first.id}" } + attribute :name, String, :default => ->(first, _) { "Named: #{first.id}" } attribute :description, String end diff --git a/spec/integration/injectible_coercers_spec.rb b/spec/integration/injectible_coercers_spec.rb index bf891733..2101f112 100644 --- a/spec/integration/injectible_coercers_spec.rb +++ b/spec/integration/injectible_coercers_spec.rb @@ -7,7 +7,7 @@ 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) @@ -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 @@ -44,5 +44,4 @@ class User 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..63b006e6 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..84c0dbb6 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 diff --git a/spec/integration/struct_as_embedded_value_spec.rb b/spec/integration/struct_as_embedded_value_spec.rb index f83465a5..384793c4 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..ea0bf772 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 diff --git a/spec/integration/value_object_with_custom_constructor_spec.rb b/spec/integration/value_object_with_custom_constructor_spec.rb index 0be0a883..910efcfd 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..7669ba3c 100644 --- a/spec/integration/virtus/value_object_spec.rb +++ b/spec/integration/virtus/value_object_spec.rb @@ -26,7 +26,7 @@ def self.name 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/coerce_spec.rb b/spec/unit/virtus/attribute/coerce_spec.rb index a9d11906..0afe6444 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/collection/class_methods/build_spec.rb b/spec/unit/virtus/attribute/collection/class_methods/build_spec.rb index 417ad64b..245d41db 100644 --- a/spec/unit/virtus/attribute/collection/class_methods/build_spec.rb +++ b/spec/unit/virtus/attribute/collection/class_methods/build_spec.rb @@ -96,7 +96,7 @@ context 'when strict mode is used' do let(:type) { Array[String] } - let(:options) { { strict: true } } + let(:options) { { :strict => true } } it 'sets strict mode for member type' do expect(subject.member_type).to be_strict diff --git a/spec/unit/virtus/attribute/collection/coerce_spec.rb b/spec/unit/virtus/attribute/collection/coerce_spec.rb index e4ad256a..d9f0e756 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) { + 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 + 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/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/embedded_value/coerce_spec.rb b/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb index 34670690..fd343a4a 100644 --- a/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb +++ b/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb @@ -7,10 +7,10 @@ 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] } + let(:input) { Hash[:name => 'Piotr', :age => 30] } it { is_expected.to be_instance_of(model) } @@ -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] } diff --git a/spec/unit/virtus/attribute/hash/coerce_spec.rb b/spec/unit/virtus/attribute/hash/coerce_spec.rb index 502cea4f..5cbbc5ca 100644 --- a/spec/unit/virtus/attribute/hash/coerce_spec.rb +++ b/spec/unit/virtus/attribute/hash/coerce_spec.rb @@ -7,14 +7,20 @@ 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') } @@ -29,9 +35,9 @@ context 'when input is a hash' do context 'when key/value types are primitives' do - let(:options) { + let(:options) do { :coercer => coercer, :key_type => key_type, :value_type => value_type } - } + end let(:key_primitive) { String } let(:value_primitive) { Integer } @@ -63,7 +69,7 @@ let(:key_primitive) { OpenStruct } let(:value_primitive) { Struct.new(:id) } - let(:input) { Hash[{:name => 'Test'} => [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 @@ -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/set_default_value_spec.rb b/spec/unit/virtus/attribute/set_default_value_spec.rb index 9151b846..c6419c2c 100644 --- a/spec/unit/virtus/attribute/set_default_value_spec.rb +++ b/spec/unit/virtus/attribute/set_default_value_spec.rb @@ -3,7 +3,16 @@ describe Virtus::Attribute, '#set_default_value' do let(:object) { described_class.build(String, options.merge(:name => name, :default => default)) } - let(:model) { Class.new { def name; 'model'; end; attr_reader :test } } + let(:model) do + Class.new do + def name + 'model' + end + + attr_reader :test + end + end + let(:name) { :test } let(:instance) { model.new } let(:options) { {} } @@ -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/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..68b34207 100644 --- a/spec/unit/virtus/attribute_set/append_spec.rb +++ b/spec/unit/virtus/attribute_set/append_spec.rb @@ -14,21 +14,21 @@ 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 @@ -38,10 +38,10 @@ 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..812d6cec 100644 --- a/spec/unit/virtus/attribute_set/define_reader_method_spec.rb +++ b/spec/unit/virtus/attribute_set/define_reader_method_spec.rb @@ -10,26 +10,26 @@ 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..31530305 100644 --- a/spec/unit/virtus/attribute_set/define_writer_method_spec.rb +++ b/spec/unit/virtus/attribute_set/define_writer_method_spec.rb @@ -10,26 +10,26 @@ 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..7ffebc0a 100644 --- a/spec/unit/virtus/attribute_set/each_spec.rb +++ b/spec/unit/virtus/attribute_set/each_spec.rb @@ -5,7 +5,7 @@ let(:name) { :name } let(:attribute) { Virtus::Attribute.build(String, :name => :name) } - let(:attributes) { [ attribute ] } + 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) { described_class.new([parent_attribute]) } it { is_expected.to equal(attribute_set) } @@ -51,14 +51,14 @@ 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) { 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..6c852446 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(:name) { :name } let(:attribute) { Virtus::Attribute.build(String, :name => :name) } - let(:attributes) { [ attribute ] } + 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..e7d6b2da 100644 --- a/spec/unit/virtus/attribute_set/element_set_spec.rb +++ b/spec/unit/virtus/attribute_set/element_set_spec.rb @@ -14,7 +14,7 @@ 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(: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..3ef8880a 100644 --- a/spec/unit/virtus/attribute_set/merge_spec.rb +++ b/spec/unit/virtus/attribute_set/merge_spec.rb @@ -25,10 +25,10 @@ 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..d5ce7f6e 100644 --- a/spec/unit/virtus/attribute_set/reset_spec.rb +++ b/spec/unit/virtus/attribute_set/reset_spec.rb @@ -5,7 +5,7 @@ let(:name) { :name } let(:attribute) { Virtus::Attribute.build(String, :name => :name) } - let(:attributes) { [ attribute ] } + let(:attributes) { [attribute] } let(:object) { described_class.new(parent, attributes) } context 'when the parent has no attributes' do @@ -15,37 +15,37 @@ 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) { 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) { 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(: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..953205d1 100644 --- a/spec/unit/virtus/attributes_reader_spec.rb +++ b/spec/unit/virtus/attributes_reader_spec.rb @@ -1,7 +1,6 @@ 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' } @@ -11,14 +10,14 @@ 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 - } - } + end + end it_behaves_like 'attribute hash' do subject { model.new } diff --git a/spec/unit/virtus/attributes_writer_spec.rb b/spec/unit/virtus/attributes_writer_spec.rb index e52ec1ac..5cc6857b 100644 --- a/spec/unit/virtus/attributes_writer_spec.rb +++ b/spec/unit/virtus/attributes_writer_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' describe Virtus, '#attributes=' do - shared_examples_for 'mass-assignment' do it 'allows writing known attributes' do subject.attributes = { :test => 'Hello World' } @@ -17,13 +16,13 @@ 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/new_spec.rb b/spec/unit/virtus/class_methods/new_spec.rb index 63ca576b..bda5865c 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 - } - } + end + end context 'without attribute hash' do subject { model.new } @@ -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..6d50c642 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 :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..09ce80ca 100644 --- a/spec/unit/virtus/model_spec.rb +++ b/spec/unit/virtus/model_spec.rb @@ -68,9 +68,9 @@ it_behaves_like 'a model with mass-assignment' do let(:instance) { subject.new } - let(:attributes) { + let(:attributes) do { :name => 'Jane', :something => nil, :age => 23 } - } + end end it 'has its own attributes' do diff --git a/spec/unit/virtus/module_spec.rb b/spec/unit/virtus/module_spec.rb index 74bf59ea..4cb167fd 100644 --- a/spec/unit/virtus/module_spec.rb +++ b/spec/unit/virtus/module_spec.rb @@ -116,7 +116,7 @@ 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) @@ -142,8 +142,8 @@ 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) @@ -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..26a31475 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 - } - } + 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..1d04f4c5 100644 --- a/spec/unit/virtus/value_object_spec.rb +++ b/spec/unit/virtus/value_object_spec.rb @@ -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) { + 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,13 +89,13 @@ 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) } @@ -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')