Skip to content

Commit

Permalink
refactor(activemeta/rule): look into source code
Browse files Browse the repository at this point in the history
  test regexps until end of string instead of end of line + specs.
  use Hash::try_convert for code readability.
  fix some norm issue.
  • Loading branch information
mehdi-farsi committed Dec 1, 2015
1 parent 679bca7 commit 19c414c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
16 changes: 8 additions & 8 deletions lib/active_meta/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 16 additions & 3 deletions spec/lib/active_meta/rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 19c414c

Please sign in to comment.