Skip to content

Commit

Permalink
rubocop -a
Browse files Browse the repository at this point in the history
  • Loading branch information
jasl committed May 16, 2019
1 parent 6360009 commit 2e8d599
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 74 deletions.
8 changes: 3 additions & 5 deletions lib/options_model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ class Base
validate do
self.class.attribute_names.each do |attribute_name|
attribute = public_send(attribute_name)
if attribute.is_a?(self.class) && attribute.invalid?
errors.add attribute_name, :invalid
end
errors.add attribute_name, :invalid if attribute.is_a?(self.class) && attribute.invalid?
end
end

Expand All @@ -22,14 +20,14 @@ def ==(other)
nested_attributes == other.nested_attributes &&
unused_attributes == other.unused_attributes
end
alias :eql? :==
alias eql? ==

def hash
[attributes, nested_attributes, unused_attributes].hash
end

def inspect
"#<#{self.class.name}:OptionsModel #{self.to_h}>"
"#<#{self.class.name}:OptionsModel #{to_h}>"
end

def self.inspect
Expand Down
8 changes: 2 additions & 6 deletions lib/options_model/concerns/attribute_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def initialize_dup(other)
def update(other)
return unless other

unless other.respond_to?(:to_h)
raise ArgumentError, "#{other} must be respond to `to_h`"
end
raise ArgumentError, "#{other} must be respond to `to_h`" unless other.respond_to?(:to_h)

other.to_h.each do |k, v|
if respond_to?("#{k}=")
Expand All @@ -47,9 +45,7 @@ def []=(key, val)
end

def fetch(key, default = nil)
if self.class.attribute_names.exclude?(key.to_sym) && default.nil? && !block_given?
raise KeyError, "attribute not found"
end
raise KeyError, "attribute not found" if self.class.attribute_names.exclude?(key.to_sym) && default.nil? && !block_given?

value = respond_to?(key) ? public_send(key) : nil
return value if value
Expand Down
102 changes: 46 additions & 56 deletions lib/options_model/concerns/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ def attribute(name, cast_type, default: nil, array: false)

attribute_defaults[name] = default
default_extractor =
case
when default.respond_to?(:call)
if default.respond_to?(:call)
".call"
when default.duplicable?
elsif default.duplicable?
".deep_dup"
else
""
Expand Down Expand Up @@ -55,23 +54,20 @@ def #{name}=(value)
end
STR

if cast_type == :boolean
generated_attribute_methods.send :alias_method, :"#{name}?", name
end
generated_attribute_methods.send :alias_method, :"#{name}?", name if cast_type == :boolean
end
end

self.attribute_names_for_inlining << name
attribute_names_for_inlining << name

self
end

def enum_attribute(name, enum, default: nil, allow_nil: false)
check_not_finalized!

unless enum.is_a?(Array) && enum.any?
raise ArgumentError, "enum should be an Array and can't empty"
end
raise ArgumentError, "enum should be an Array and can't empty" unless enum.is_a?(Array) && enum.any?

enum = enum.map(&:to_s)

attribute name, :string, default: default
Expand All @@ -80,11 +76,11 @@ def enum_attribute(name, enum, default: nil, allow_nil: false)
generated_class_methods.synchronize do
generated_class_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
def #{pluralized_name}
%w(#{enum.join(" ")}).freeze
%w(#{enum.join(' ')}).freeze
end
STR

validates name, inclusion: {in: enum}, allow_nil: allow_nil
validates name, inclusion: { in: enum }, allow_nil: allow_nil
end

self
Expand All @@ -93,17 +89,15 @@ def #{pluralized_name}
def embeds_one(name, class_name: nil, anonymous_class: nil)
check_not_finalized!

if class_name.blank? && anonymous_class.nil?
raise ArgumentError, "must provide at least one of `class_name` or `anonymous_class`"
end
raise ArgumentError, "must provide at least one of `class_name` or `anonymous_class`" if class_name.blank? && anonymous_class.nil?

name = name.to_sym
check_name_validity! name

if class_name.present?
nested_classes[name] = class_name.constantize
nested_classes[name] = if class_name.present?
class_name.constantize
else
nested_classes[name] = anonymous_class
anonymous_class
end

generated_attribute_methods.synchronize do
Expand All @@ -130,7 +124,7 @@ def #{name}=(value)
STR
end

self.attribute_names_for_nesting << name
attribute_names_for_nesting << name

self
end
Expand Down Expand Up @@ -160,60 +154,56 @@ def finalized?
end

def finalize!(nested = true)
if nested
nested_classes.values.each(&:finalize!)
end
nested_classes.values.each(&:finalize!) if nested

@finalized = true
end

protected

def check_name_validity!(symbolized_name)
if dangerous_attribute_method?(symbolized_name)
raise ArgumentError, "#{symbolized_name} is defined by #{OptionsModel::Base}. Check to make sure that you don't have an attribute or method with the same name."
end
def check_name_validity!(symbolized_name)
if dangerous_attribute_method?(symbolized_name)
raise ArgumentError, "#{symbolized_name} is defined by #{OptionsModel::Base}. Check to make sure that you don't have an attribute or method with the same name."
end

if attribute_names_for_inlining.include?(symbolized_name) || attribute_names_for_nesting.include?(symbolized_name)
raise ArgumentError, "duplicate define attribute `#{symbolized_name}`"
if attribute_names_for_inlining.include?(symbolized_name) || attribute_names_for_nesting.include?(symbolized_name)
raise ArgumentError, "duplicate define attribute `#{symbolized_name}`"
end
end
end

def check_not_finalized!
if finalized?
raise "can't modify finalized #{self}"
def check_not_finalized!
raise "can't modify finalized #{self}" if finalized?
end
end

# A method name is 'dangerous' if it is already (re)defined by OptionsModel, but
# not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
def dangerous_attribute_method?(name) # :nodoc:
method_defined_within?(name, OptionsModel::Base)
end
# A method name is 'dangerous' if it is already (re)defined by OptionsModel, but
# not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
def dangerous_attribute_method?(name) # :nodoc:
method_defined_within?(name, OptionsModel::Base)
end

def method_defined_within?(name, klass, superklass = klass.superclass) # :nodoc:
if klass.method_defined?(name) || klass.private_method_defined?(name)
if superklass.method_defined?(name) || superklass.private_method_defined?(name)
klass.instance_method(name).owner != superklass.instance_method(name).owner
def method_defined_within?(name, klass, superklass = klass.superclass) # :nodoc:
if klass.method_defined?(name) || klass.private_method_defined?(name)
if superklass.method_defined?(name) || superklass.private_method_defined?(name)
klass.instance_method(name).owner != superklass.instance_method(name).owner
else
true
end
else
true
false
end
else
false
end
end

def generated_attribute_methods
@generated_attribute_methods ||= Module.new {
extend Mutex_m
}.tap { |mod| include mod }
end
def generated_attribute_methods
@generated_attribute_methods ||= Module.new do
extend Mutex_m
end.tap { |mod| include mod }
end

def generated_class_methods
@generated_class_methods ||= Module.new {
extend Mutex_m
}.tap { |mod| extend mod }
end
def generated_class_methods
@generated_class_methods ||= Module.new do
extend Mutex_m
end.tap { |mod| extend mod }
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/options_model/concerns/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def load(yaml)
return new unless yaml
return new unless yaml.is_a?(String) && /^---/.match?(yaml)

hash = YAML.load(yaml) || Hash.new
hash = YAML.safe_load(yaml) || {}

unless hash.is_a? Hash
raise ArgumentError,
Expand Down
2 changes: 1 addition & 1 deletion options_model.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

$:.push File.expand_path("../lib", __FILE__)
$LOAD_PATH.push File.expand_path("lib", __dir__)

# Maintain your gem's version:
require "options_model/version"
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
config.log_level = :debug

# Prepend all log lines with the following tags.
config.log_tags = [ :request_id ]
config.log_tags = [:request_id]

# Use a different cache store in production.
# config.cache_store = :mem_cache_store
Expand Down
4 changes: 2 additions & 2 deletions test/dummy/config/spring.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

%w(
%w[
.ruby-version
.rbenv-vars
tmp/restart.txt
tmp/caching-dev.txt
).each { |path| Spring.watch(path) }
].each { |path| Spring.watch(path) }
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
require File.expand_path("../test/dummy/config/environment.rb", __dir__)
require "rails/test_help"

# Filter out Minitest backtrace while allowing backtrace from other libraries
Expand All @@ -11,7 +11,7 @@

# Load fixtures from the engine
if ActiveSupport::TestCase.respond_to?(:fixture_path=)
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
ActiveSupport::TestCase.fixture_path = File.expand_path("fixtures", __dir__)
ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
ActiveSupport::TestCase.file_fixture_path = ActiveSupport::TestCase.fixture_path + "/files"
ActiveSupport::TestCase.fixtures :all
Expand Down

0 comments on commit 2e8d599

Please sign in to comment.