Class: TT::ActionFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/t_t/action_factory.rb

Defined Under Namespace

Classes: Action, Locale, Option

Instance Method Summary collapse

Constructor Details

#initialize(*locales) ⇒ ActionFactory

Returns a new instance of ActionFactory.



45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Method Details

#activate_rules(*list) ⇒ Object



61
62
63
64
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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_hashObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) {|@locales.fetch(key) { TT.raise_error "`#{ key }` is unknown" }| ... } ⇒ Object

Yields:

  • (@locales.fetch(key) { TT.raise_error "`#{ key }` is unknown" })


57
58
59
# 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) ⇒ Object



87
88
89
# File 'lib/t_t/action_factory.rb', line 87

def with_rules(base, *list)
  Action.new(base, Option.parse(list))
end