diff --git a/lib/active_meta/attribute.rb b/lib/active_meta/attribute.rb index 8f00143..a63712d 100644 --- a/lib/active_meta/attribute.rb +++ b/lib/active_meta/attribute.rb @@ -31,9 +31,7 @@ def overload(&block) end def register_rule(rule) - unless rule.is_a? ActiveMeta::Rule - raise ArgumentError, "no rule given for attribute #{@attribute}" - end + raise ArgumentError, "no rule given for attribute #{@attribute}" unless rule.is_a? ActiveMeta::Rule # rule.attribute = self.attribute rule.contexts = @context_chain rule.parent = self @@ -41,7 +39,7 @@ def register_rule(rule) end def [](arg) - rules.select(&:validates_context?).find{|rule| rule.rule_name.to_s == arg.to_s } + rules.select(&:validates_context?).find { |rule| rule.rule_name.to_s == arg.to_s } end end diff --git a/lib/active_meta/context.rb b/lib/active_meta/context.rb index ed94365..c0984ba 100644 --- a/lib/active_meta/context.rb +++ b/lib/active_meta/context.rb @@ -3,7 +3,7 @@ module Context class << self def new(&block) raise ArgumentError, 'no block given for context' unless block_given? - mod = Module.new do + Module.new do @eval_block = block class << self def valid_for_rule?(rule) diff --git a/lib/active_meta/core.rb b/lib/active_meta/core.rb index 72c327e..e69ac4a 100644 --- a/lib/active_meta/core.rb +++ b/lib/active_meta/core.rb @@ -46,7 +46,7 @@ def rules def [](*args) args = args.map(&:to_s) - rules.select{|rule| args.include? rule.rule_name.to_s } + rules.select { |rule| args.include? rule.rule_name.to_s } end end end diff --git a/lib/active_meta/rule.rb b/lib/active_meta/rule.rb index 0a102c0..458bec3 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 @@ -27,9 +27,9 @@ def opts 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