class TT::ActionFactory

Constants

Action
Option

Public Class Methods

new(*locales) click to toggle source
# File lib/t_t/action_factory.rb, line 45
def initialize(*locales)
  @actions = {}
  @locales = {}
  @exceptions = {}

  locales.each do |lkey|
    @actions[lkey]    = {}
    @exceptions[lkey] = {}
    @locales[lkey]    = Locale.new
  end
end

Public Instance Methods

activate_rules(*list) click to toggle source
# File lib/t_t/action_factory.rb, line 61
def activate_rules(*list)
  require 't_t/builtin_rules'
  list.each { |rkey| BuiltinRules.send(rkey, self) }
end
add(akey, list) click to toggle source
# File lib/t_t/action_factory.rb, line 66
def add(akey, list)
  @locales.each do |lkey, locale|
    unless action = list[lkey]
      TT.raise_error "action `#{ akey }` is missing for `#{ lkey }` locale"
    end

    action = Action.new(action, []) if action.is_a?(String)

    if action.is_a?(Action)
      action.rules.each do |rule|
        next if locale.knows_rule?(rule.key)
        TT.raise_error "`#{ rule.key }` is an unknown rule for `#{ lkey }` locale"
      end
    else
      TT.raise_error "the value of `#{ akey }` action for `#{ lkey }` locale has a wrong type"
    end

    @actions[lkey][akey] = action
  end
end
add_exception(mkey, schema) click to toggle source
# File lib/t_t/action_factory.rb, line 91
def add_exception(mkey, schema)
  schema.each do |lkey, list|
    TT.raise_error("`#{ lkey }` is an unknown locale") unless @locales.has_key?(lkey)

    list.each do |akey, str|
      unless @actions[lkey].has_key?(akey)
        TT.raise_error "`#{ akey }` action is not specified. Do it before add an exception"
      end

      @exceptions[lkey][akey] ||= {}
      @exceptions[lkey][akey][mkey] = str
    end
  end
end
as_hash() click to toggle source
# File lib/t_t/action_factory.rb, line 106
def as_hash
  @actions.inject({}) do |hash, (lkey, list)|
    locale = @locales.fetch(lkey)

    actions = list.inject({}) do |result, (akey, action)|
      keys = locale.compile(action).merge!(@exceptions[lkey].fetch(akey, {}))
      keys.each do |mkey, str|
        result[mkey] = {} unless result.has_key?(mkey)
        result[mkey][akey] = str
      end

      result
    end

    hash.merge!(lkey => { actions: actions })
  end
end
for(key) { |fetch{ raise_error "`#{ key }` is unknown" }| ... } click to toggle source
# File lib/t_t/action_factory.rb, line 57
def for(key, &block)
  yield @locales.fetch(key) { TT.raise_error "`#{ key }` is unknown" }
end
with_rules(base, *list) click to toggle source
# File lib/t_t/action_factory.rb, line 87
def with_rules(base, *list)
  Action.new(base, Option.parse(list))
end