Class: Datacaster::I18nValues::Key

Inherits:
Base
  • Object
show all
Defined in:
lib/datacaster/i18n_values/key.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin

#&, #*, #call, #call_with_runtime, #cast_errors, #i18n_key, #i18n_map_keys, #i18n_scope, #i18n_vars, #then, #with_context, #with_object_context, #with_runtime, #|

Constructor Details

#initialize(keys_or_key, args = {}) ⇒ Key

Returns a new instance of Key.



6
7
8
9
10
# File 'lib/datacaster/i18n_values/key.rb', line 6

def initialize(keys_or_key, args = {})
  keys = Array(keys_or_key)
  @keys = keys
  @args = args
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



4
5
6
# File 'lib/datacaster/i18n_values/key.rb', line 4

def keys
  @keys
end

Instance Method Details

#==(other) ⇒ Object



12
13
14
# File 'lib/datacaster/i18n_values/key.rb', line 12

def ==(other)
  super && @keys == other.keys
end

#inspectObject



29
30
31
# File 'lib/datacaster/i18n_values/key.rb', line 29

def inspect
  "#<#{self.class.name}(#{@keys.inspect}) #{@args.inspect}>"
end

#resolveObject



16
17
18
19
20
21
22
23
# File 'lib/datacaster/i18n_values/key.rb', line 16

def resolve
  keys = @keys.select { |x| x[0] != '.' }
  if keys.empty?
    raise RuntimeError.new("No absolute keys among #{@keys.inspect}. Use #i18n_key in addition to #i18n_scope.")
  end
  key = keys.find(&Config.i18n_exists?) || keys.first
  Config.i18n_t.(key, **@args)
end

#with_args(args) ⇒ Object



25
26
27
# File 'lib/datacaster/i18n_values/key.rb', line 25

def with_args(args)
  self.class.new(@keys, @args.merge(args))
end