Permalink
Browse files

replacing class << self with extend self

  • Loading branch information...
1 parent b80828e commit 344faabaaff2ead5af6c0b60258e4bd7ce653ea8 bglusman committed Jan 28, 2012
Showing with 45 additions and 46 deletions.
  1. +45 −46 lib/outlaw/law_parser.rb
View
91 lib/outlaw/law_parser.rb
@@ -1,62 +1,61 @@
module Outlaw
module LawParser
- class << self
- def parse(restriction, message="")
- tokens = restriction.split
- parsed_restriction = []
- tokens.each do |token|
- case
- when multipart?(token) #this handles multi-token literals, Const.new etc
- parsed_restriction += Ripper.lex(token)
- .reduce([]){|array, tkn|
- array << token_type_regex(tkn) }
- when defined_collection?(token)
- parsed_restriction << Outlaw.const_get(string_to_sym(token.upcase))
- when parameter?(token)
- parsed_restriction << string_to_sym(token)
- else
- parsed_restriction += build_regex(token)
- end
+ extend self
+ def parse(restriction, message="")
+ tokens = restriction.split
+ parsed_restriction = []
+ tokens.each do |token|
+ case
+ when multipart?(token) #this handles multi-token literals, Const.new etc
+ parsed_restriction += Ripper.lex(token)
+ .reduce([]){|array, tkn|
+ array << token_type_regex(tkn) }
+ when defined_collection?(token)
+ parsed_restriction << Outlaw.const_get(string_to_sym(token.upcase))
+ when parameter?(token)
+ parsed_restriction << string_to_sym(token)
+ else
+ parsed_restriction += build_regex(token)
end
- Rule.new(message, restriction, &build_block(parsed_restriction))
end
+ Rule.new(message, restriction, &build_block(parsed_restriction))
+ end
- private
+ private
- def token_type_regex(token)
- /#{token[2]}/
- end
+ def token_type_regex(token)
+ /#{token[2]}/
+ end
- def parameter?(token)
- token[0].chr == ':'
- end
+ def parameter?(token)
+ token[0].chr == ':'
+ end
- def defined_collection?(token)
- parameter?(token) && Outlaw.const_defined?(string_to_sym(token.upcase))
- end
+ def defined_collection?(token)
+ parameter?(token) && Outlaw.const_defined?(string_to_sym(token.upcase))
+ end
- def string_to_sym(str)
- str[1..-1].to_sym
- end
+ def string_to_sym(str)
+ str[1..-1].to_sym
+ end
- def build_regex(token)
- #fully expect this hack to come back & haunt me, but passes curr. examples
- [/\A#{token}/]
- end
+ def build_regex(token)
+ #fully expect this hack to come back & haunt me, but passes curr. examples
+ [/\A#{token}/]
+ end
- def multipart?(token)
- !parameter?(token) && Ripper.lex(token).count > 1
- end
+ def multipart?(token)
+ !parameter?(token) && Ripper.lex(token).count > 1
+ end
- def build_block(pattern)
- lambda do |file|
- program = Ripper.tokenize(file)
- program.each_with_index do |token, index|
- next unless token.match(pattern.first)
- return true if Rule.test(program, index, pattern)
- end
- return false
+ def build_block(pattern)
+ ->(file) do
+ program = Ripper.tokenize(file)
+ program.each_with_index do |token, index|
+ next unless token.match(pattern.first)
+ return true if Rule.test(program, index, pattern)
end
+ return false
end
end
end

0 comments on commit 344faab

Please sign in to comment.