From 19c414c7c53f7cfdde3c5b4975e13638f49c1ea4 Mon Sep 17 00:00:00 2001 From: Mehdi FARSI Date: Tue, 1 Dec 2015 08:27:50 +0100 Subject: [PATCH] refactor(activemeta/rule): look into source code test regexps until end of string instead of end of line + specs. use Hash::try_convert for code readability. fix some norm issue. --- lib/active_meta/rule.rb | 16 ++++++++-------- spec/lib/active_meta/rule_spec.rb | 19 ++++++++++++++++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/active_meta/rule.rb b/lib/active_meta/rule.rb index 0a102c0..ce24ef8 100644 --- a/lib/active_meta/rule.rb +++ b/lib/active_meta/rule.rb @@ -6,11 +6,11 @@ class Rule alias_method :args, :arguments def initialize(attribute, rule_name, *arguments) - unless /^[a-z_]+$/ =~ attribute - raise ArgumentError, "invalide attribute #{attribute}" + unless /\A[a-z_]+\z/ =~ attribute + raise ArgumentError, "invalid attribute #{attribute}" end - unless /^[a-z_]+$/ =~ rule_name - raise ArgumentError, "invalide rule_name for attribute #{attribute}" + unless /\A[a-z_]+\z/ =~ rule_name + raise ArgumentError, "invalid rule_name for attribute #{attribute}" end @attribute = attribute @rule_name = rule_name @@ -22,14 +22,14 @@ def metaclass end def opts - arguments.last && arguments.last.is_a?(Hash) ? arguments.last : {} + Hash.try_convert(arguments.last) || {} end def validates_context? @validates_context ||= begin - if contexts && contexts.length > 0 - context_classes = contexts.map{|context| ActiveMeta::Contexts.const_get(context) } - context_classes.all?{|ctx| ctx.valid_for_rule?(self) } + if contexts && !contexts.empty? + context_classes = contexts.map{ |context| ActiveMeta::Contexts.const_get(context) } + context_classes.all? { |ctx| ctx.valid_for_rule?(self) } else true end diff --git a/spec/lib/active_meta/rule_spec.rb b/spec/lib/active_meta/rule_spec.rb index d0b4e56..58d8c67 100644 --- a/spec/lib/active_meta/rule_spec.rb +++ b/spec/lib/active_meta/rule_spec.rb @@ -12,11 +12,24 @@ context 'instance methods' do context '#initialize' do it 'should raise an ArgumentError if passed attribute is invalid' do - ->{ subject.new('Foo', 't') }.should raise_error ArgumentError + -> { subject.new('Foo', 't') }.should raise_error ArgumentError end - it 'should raise an ArgumentError if passed attribute is invalid' do - ->{ subject.new('t', 'Foo') }.should raise_error ArgumentError + it 'should raise an ArgumentError if passed rule_name is invalid' do + -> { subject.new('t', 'Foo') }.should raise_error ArgumentError + end + + # http://ruby-doc.org/core-2.2.1/Regexp.html#class-Regexp-label-Anchors + it 'should raise an ArgumentError if a multiline string is passed as attribute' do + arg = "i_am_valid but_not_now".gsub(' ', "\n") + + -> { subject.new(arg, 't') }.should raise_error ArgumentError + end + + it 'should raise an ArgumentError if a multiline string is passed as rule_name' do + arg = "i_am_valid but_not_now".gsub(' ', "\n") + + -> { subject.new('t', arg) }.should raise_error ArgumentError end it 'should store `attribute` as @attribute' do