Class: TT::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ns, section = nil) ⇒ Base

Returns a new instance of Base.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/t_t/base.rb', line 97

def initialize(ns, section = nil)
  @lookup   = Utils.lookup(self.class.config[:prefix], nil)
  @b_lookup = Utils.lookup(self.class.config[:prefix], :base)
  @e_lookup = Utils.lookup(self.class.config[:prefix], :messages)

  ns = Utils.to_parts(ns).join('.')
  @config = { ns: ns, root: (section ? "#{ ns }.#{ section }" : ns) }
  default_model = ns.to_s.singularize

  @config[:attributes] = @lookup.call(default_model, :attributes)
  @config[:models]     = @lookup.call(default_model, :models)
  @config[:actions]    = @b_lookup.call(default_model, :actions)
  @config[:enums]      = @b_lookup.call(default_model, :enums)
  @config[:errors]     = @e_lookup.call(default_model, :errors)

  @downcase = self.class.config.fetch(:downcase, Utils::DOWNCASE)
end

Class Method Details

.config(custom = nil, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/t_t/base.rb', line 78

def self.config(custom = nil, &block)
  @settings ||= {}

  if custom
    unknown = custom.keys.detect { |key| ![:downcase, :prefix].include?(key) }
    if unknown
      TT.raise_error "`#{ unknown }` is a wrong key in the configuration"
    else
      @settings.merge!(custom)
    end
  end

  instance_exec(&block) if block_given?

  @settings
end

.lookup_key_method(meth_name, path) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/t_t/base.rb', line 69

def self.lookup_key_method(meth_name, path)
  class_eval <<-RUBY
    def #{ meth_name }(key, options = {})
      I18n.t "\#{ _config.fetch(:ns) }.#{ path }.\#{ key }",
        { default: [:"#{ path }.\#{ key }"] }.merge(options)
    end
  RUBY
end

Instance Method Details

#a(name, model_name = nil, custom = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/t_t/base.rb', line 115

def a(name, model_name = nil, custom = {})
  path, defaults = _resolve(@b_lookup, model_name, :actions, name)

  resource = r(model_name)
  resources = rs(model_name)
  I18n.t path, {
    default: defaults, r: @downcase.call(resource, I18n.locale), R: resource,
    rs: @downcase.call(resources, I18n.locale), RS: resources
  }.merge!(custom)
end

#attr(name, model_name = nil) ⇒ Object



126
127
128
129
# File 'lib/t_t/base.rb', line 126

def attr(name, model_name = nil)
  path, defaults = _resolve(@lookup, model_name, :attributes, name)
  I18n.t path, default: defaults
end

#e(attr_name, error_name, *args) ⇒ Object



136
137
138
139
140
141
# File 'lib/t_t/base.rb', line 136

def e(attr_name, error_name, *args)
  custom = args.last.is_a?(Hash) ? args.pop : {}
  model_name = args.first
  path, defaults = _resolve_errors(model_name, attr_name, error_name)
  I18n.t path, { default: defaults }.merge!(custom)
end

#enum(name, kind, model_name = nil) ⇒ Object



131
132
133
134
# File 'lib/t_t/base.rb', line 131

def enum(name, kind, model_name = nil)
  path, defaults = _resolve(@b_lookup, model_name, :enums, "#{ name }.#{ kind }")
  I18n.t path, default: defaults
end

#r(model_name = nil) ⇒ Object



143
144
145
# File 'lib/t_t/base.rb', line 143

def r(model_name = nil)
  rs(model_name, 1)
end

#rs(model_name = nil, count = 10) ⇒ Object



147
148
149
150
151
# File 'lib/t_t/base.rb', line 147

def rs(model_name = nil, count = 10)
  path, defaults = _resolve(@lookup, model_name, :models, nil)
  # cut from defaults :"#{ orm }.models", :models
  I18n.t path, default: defaults[0...-2], count: count
end

#t(key, custom = {}) ⇒ Object



153
154
155
156
# File 'lib/t_t/base.rb', line 153

def t(key, custom = {})
  defaults = [:"#{ _config.fetch(:ns) }.common.#{ key }"].concat(Array(custom[:default]))
  I18n.t "#{ _config.fetch(:root) }.#{ key  }", custom.merge(default: defaults)
end