Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learn and Clean (WIP) /!\ Please, don't merge or close yet #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions lib/active_meta/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ 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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use inline condition as much as possible. But maybe it's not good for the clarity of the code ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @joshleaves shoud set up a style code guidelines with some linter 👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea ! Personally, I refer to the Github's one.

# rule.attribute = self.attribute
rule.contexts = @context_chain
rule.parent = self
rules.push(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
Expand Down
2 changes: 1 addition & 1 deletion lib/active_meta/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_meta/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def rules

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this = self

Someone gonna die soon..
TO BE CONTINUED

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
14 changes: 7 additions & 7 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 @@ -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
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