Permalink
Browse files

Extract l10n keys.

  • Loading branch information...
1 parent dff1c03 commit 21a4bf63e2d07df3167d56d4c91308136acb56f2 @threedaymonk threedaymonk committed Mar 22, 2012
Showing with 69 additions and 7 deletions.
  1. +6 −0 lib/sibyl/graph.rb
  2. +25 −7 lib/sibyl/unit.rb
  3. +38 −0 test/graph_l10n_test.rb
View
@@ -17,6 +17,12 @@ def valid?
!has_unresolved_targets?
end
+ def l10n_keys
+ @steps.inject([]) { |keys, step|
+ keys + step.l10n_keys
+ }
+ end
+
private
def has_unreachable_steps?
(step_names - target_names - [@steps.first.name]).any?
View
@@ -33,7 +33,7 @@ def def_false(*names)
end
end
- def_false :metadata, :option, :statement, :step, :sink
+ def_false :metadata, :option, :statement, :step, :sink, :branch
end
class Declaration < Element
@@ -57,33 +57,46 @@ class Step < Node
construct_with :type, :name, :body
def_true :step
- def options
- body.select(&:option?)
+ def branches
+ body.select(&:branch?)
end
def statements
body.select(&:statement?)
end
def inputs
- options.map(&:text)
+ body.select(&:option?).map(&:text)
end
def exits
- body.select(&:option?).map(&:branches).flatten.map(&:target).uniq
+ branches.map(&:branches).flatten.map(&:target).uniq
end
def compute(input, context)
context.input = input
statements.each do |s|
s.execute(context)
end
- options.each do |o|
+ branches.each do |o|
result = o.compute(context)
return result if result
end
raise ValidationError
end
+
+ def l10n_keys
+ prefix = keyify(name)
+ standard = ["#{prefix}.title"]
+ body.select(&:option?).inject(standard) { |keys, option|
+ keys << "#{prefix}.options.#{option.text}"
+ }
+ end
+
+ private
+ def keyify(s)
+ s.downcase.gsub(/^\W+|\W+$/, "").gsub(/\W+/, "_")
+ end
end
class Outcome < Step
@@ -93,6 +106,10 @@ class Outcome < Step
def exits
[]
end
+
+ def body
+ []
+ end
end
class Jump < Node
@@ -118,7 +135,7 @@ def compute(context)
class Brancher < Node
construct_with :branches
- def_true :option
+ def_true :branch
def compute(context)
branches.each do |b|
@@ -131,6 +148,7 @@ def compute(context)
class OptionBrancher < Brancher
construct_with :text, :branches
+ def_true :option
def compute(context)
if context.input == text
View
@@ -0,0 +1,38 @@
+require "common"
+require "sibyl/graph"
+
+describe "Graph l10n" do
+ def graph(source)
+ Sibyl::Graph.new(source)
+ end
+
+ it "should list necessary l10n keys" do
+ g = graph(%{
+ step multiple "step 1"
+ option yes -> "step 2"
+ option no -> "outcome 1"
+
+ step salary "step 2"
+ go ->
+ if { input.per_week >= 102 } ->
+ "outcome 2"
+ otherwise -> "outcome 3"
+
+ outcome "outcome 1"
+ outcome "outcome 2"
+ outcome "outcome 3"
+ })
+
+ expected = %w[
+ step_1.title
+ step_1.options.yes
+ step_1.options.no
+ step_2.title
+ outcome_1.title
+ outcome_2.title
+ outcome_3.title
+ ]
+ assert_equal expected.sort, g.l10n_keys.sort
+ end
+end
+

0 comments on commit 21a4bf6

Please sign in to comment.