Permalink
Browse files

Merge pull request #8567 from frodsan/update_coding_conventions_journey

update AD::Journey to follow Rails coding conventions
  • Loading branch information...
rafaelfranca committed Dec 20, 2012
2 parents a8ad694 + eb493f5 commit 3aa327f9887d272e225c825bfbb1d698e37d1bee
View
@@ -80,4 +80,4 @@ rule '.rb' => '.y' do |t|
sh "racc -l -o #{t.name} #{t.source}"
end
-task :compile => 'lib/action_dispatch/journey/parser.rb'
+task compile: 'lib/action_dispatch/journey/parser.rb'
@@ -5,17 +5,17 @@ module Journey
class Formatter # :nodoc:
attr_reader :routes
- def initialize routes
+ def initialize(routes)
@routes = routes
@cache = nil
end
- def generate type, name, options, recall = {}, parameterize = nil
- constraints = recall.merge options
+ def generate(type, name, options, recall = {}, parameterize = nil)
+ constraints = recall.merge(options)
missing_keys = []
match_route(name, constraints) do |route|
- parameterized_parts = extract_parameterized_parts route, options, recall, parameterize
+ parameterized_parts = extract_parameterized_parts(route, options, recall, parameterize)
next if !name && route.requirements.empty? && route.parts.empty?
missing_keys = missing_keys(route, parameterized_parts)
@@ -35,112 +35,110 @@ def clear
end
private
- def extract_parameterized_parts route, options, recall, parameterize = nil
- constraints = recall.merge options
- data = constraints.dup
- keys_to_keep = route.parts.reverse.drop_while { |part|
- !options.key?(part) || (options[part] || recall[part]).nil?
- } | route.required_parts
+ def extract_parameterized_parts(route, options, recall, parameterize = nil)
+ constraints = recall.merge(options)
+ data = constraints.dup
- (data.keys - keys_to_keep).each do |bad_key|
- data.delete bad_key
- end
+ keys_to_keep = route.parts.reverse.drop_while { |part|
+ !options.key?(part) || (options[part] || recall[part]).nil?
+ } | route.required_parts
+
+ (data.keys - keys_to_keep).each do |bad_key|
+ data.delete(bad_key)
+ end
- parameterized_parts = data.dup
+ parameterized_parts = data.dup
- if parameterize
- parameterized_parts.each do |k,v|
- parameterized_parts[k] = parameterize.call(k, v)
+ if parameterize
+ parameterized_parts.each do |k, v|
+ parameterized_parts[k] = parameterize.call(k, v)
+ end
end
- end
- parameterized_parts.keep_if { |_,v| v }
- parameterized_parts
- end
+ parameterized_parts.keep_if { |_, v| v }
+ parameterized_parts
+ end
- def named_routes
- routes.named_routes
- end
+ def named_routes
+ routes.named_routes
+ end
- def match_route name, options
- if named_routes.key? name
- yield named_routes[name]
- else
- #routes = possibles(@cache, options.to_a)
- routes = non_recursive(cache, options.to_a)
+ def match_route(name, options)
+ if named_routes.key?(name)
+ yield named_routes[name]
+ else
+ routes = non_recursive(cache, options.to_a)
- hash = routes.group_by { |_, r|
- r.score options
- }
+ hash = routes.group_by { |_, r| r.score(options) }
- hash.keys.sort.reverse_each do |score|
- next if score < 0
+ hash.keys.sort.reverse_each do |score|
+ next if score < 0
- hash[score].sort_by { |i,_| i }.each do |_,route|
- yield route
+ hash[score].sort_by { |i, _| i }.each do |_, route|
+ yield route
+ end
end
end
end
- end
- def non_recursive cache, options
- routes = []
- stack = [cache]
+ def non_recursive(cache, options)
+ routes = []
+ stack = [cache]
- while stack.any?
- c = stack.shift
- routes.concat c[:___routes] if c.key? :___routes
+ while stack.any?
+ c = stack.shift
+ routes.concat(c[:___routes]) if c.key?(:___routes)
- options.each do |pair|
- stack << c[pair] if c.key? pair
+ options.each do |pair|
+ stack << c[pair] if c.key?(pair)
+ end
end
- end
- routes
- end
+ routes
+ end
- # Returns an array populated with missing keys if any are present.
- def missing_keys route, parts
- missing_keys = []
- tests = route.path.requirements
- route.required_parts.each { |key|
- if tests.key? key
- missing_keys << key unless /\A#{tests[key]}\Z/ === parts[key]
- else
- missing_keys << key unless parts[key]
- end
- }
- missing_keys
- end
+ # Returns an array populated with missing keys if any are present.
+ def missing_keys(route, parts)
+ missing_keys = []
+ tests = route.path.requirements
+ route.required_parts.each { |key|
+ if tests.key?(key)
+ missing_keys << key unless /\A#{tests[key]}\Z/ === parts[key]
+ else
+ missing_keys << key unless parts[key]
+ end
+ }
+ missing_keys
+ end
- def possibles cache, options, depth = 0
- cache.fetch(:___routes) { [] } + options.find_all { |pair|
- cache.key? pair
- }.map { |pair|
- possibles(cache[pair], options, depth + 1)
- }.flatten(1)
- end
+ def possibles(cache, options, depth = 0)
+ cache.fetch(:___routes) { [] } + options.find_all { |pair|
+ cache.key?(pair)
+ }.map { |pair|
+ possibles(cache[pair], options, depth + 1)
+ }.flatten(1)
+ end
- # Returns +true+ if no missing keys are present, otherwise +false+.
- def verify_required_parts! route, parts
- missing_keys(route, parts).empty?
- end
+ # Returns +true+ if no missing keys are present, otherwise +false+.
+ def verify_required_parts!(route, parts)
+ missing_keys(route, parts).empty?
+ end
- def build_cache
- root = { :___routes => [] }
- routes.each_with_index do |route, i|
- leaf = route.required_defaults.inject(root) do |h, tuple|
- h[tuple] ||= {}
+ def build_cache
+ root = { ___routes: [] }
+ routes.each_with_index do |route, i|
+ leaf = route.required_defaults.inject(root) do |h, tuple|
+ h[tuple] ||= {}
+ end
+ (leaf[:___routes] ||= []) << [i, route]
end
- (leaf[:___routes] ||= []) << [i, route]
+ root
end
- root
- end
- def cache
- @cache ||= build_cache
- end
+ def cache
+ @cache ||= build_cache
+ end
end
end
end
@@ -8,10 +8,10 @@ class Builder # :nodoc:
attr_reader :root, :ast, :endpoints
- def initialize root
- @root = root
- @ast = Nodes::Cat.new root, DUMMY
- @followpos = nil
+ def initialize(root)
+ @root = root
+ @ast = Nodes::Cat.new root, DUMMY
+ @followpos = nil
end
def transition_table
@@ -35,21 +35,21 @@ def transition_table
to = state_id[Object.new]
dtrans[from, to] = sym
- dtrans.add_accepting to
- ps.each { |state| dtrans.add_memo to, state.memo }
+ dtrans.add_accepting(to)
+ ps.each { |state| dtrans.add_memo(to, state.memo) }
else
dtrans[state_id[s], state_id[u]] = sym
- if u.include? DUMMY
+ if u.include?(DUMMY)
to = state_id[u]
- accepting = ps.find_all { |l| followpos(l).include? DUMMY }
+ accepting = ps.find_all { |l| followpos(l).include?(DUMMY) }
accepting.each { |accepting_state|
- dtrans.add_memo to, accepting_state.memo
+ dtrans.add_memo(to, accepting_state.memo)
}
- dtrans.add_accepting state_id[u]
+ dtrans.add_accepting(state_id[u])
end
end
@@ -60,7 +60,7 @@ def transition_table
dtrans
end
- def nullable? node
+ def nullable?(node)
case node
when Nodes::Group
true
@@ -73,18 +73,18 @@ def nullable? node
when Nodes::Terminal
!node.left
when Nodes::Unary
- nullable? node.left
+ nullable?(node.left)
else
raise ArgumentError, 'unknown nullable: %s' % node.class.name
end
end
- def firstpos node
+ def firstpos(node)
case node
when Nodes::Star
firstpos(node.left)
when Nodes::Cat
- if nullable? node.left
+ if nullable?(node.left)
firstpos(node.left) | firstpos(node.right)
else
firstpos(node.left)
@@ -100,14 +100,14 @@ def firstpos node
end
end
- def lastpos node
+ def lastpos(node)
case node
when Nodes::Star
firstpos(node.left)
when Nodes::Or
node.children.map { |c| lastpos(c) }.flatten.uniq
when Nodes::Cat
- if nullable? node.right
+ if nullable?(node.right)
lastpos(node.left) | lastpos(node.right)
else
lastpos(node.right)
@@ -121,40 +121,41 @@ def lastpos node
end
end
- def followpos node
+ def followpos(node)
followpos_table[node]
end
private
- def followpos_table
- @followpos ||= build_followpos
- end
- def build_followpos
- table = Hash.new { |h,k| h[k] = [] }
- @ast.each do |n|
- case n
- when Nodes::Cat
- lastpos(n.left).each do |i|
- table[i] += firstpos(n.right)
- end
- when Nodes::Star
- lastpos(n).each do |i|
- table[i] += firstpos(n)
+ def followpos_table
+ @followpos ||= build_followpos
+ end
+
+ def build_followpos
+ table = Hash.new { |h, k| h[k] = [] }
+ @ast.each do |n|
+ case n
+ when Nodes::Cat
+ lastpos(n.left).each do |i|
+ table[i] += firstpos(n.right)
+ end
+ when Nodes::Star
+ lastpos(n).each do |i|
+ table[i] += firstpos(n)
+ end
end
end
+ table
end
- table
- end
- def symbol edge
- case edge
- when Journey::Nodes::Symbol
- edge.regexp
- else
- edge.left
+ def symbol(edge)
+ case edge
+ when Journey::Nodes::Symbol
+ edge.regexp
+ else
+ edge.left
+ end
end
- end
end
end
end
Oops, something went wrong.

0 comments on commit 3aa327f

Please sign in to comment.