class It::Interpolation

Contains one interpolation and will delegate the work to the It::Tag (or subclass) or ERB::Util.h

Attributes

key[R]
label[R]
value[R]

Public Class Methods

call(string, values) click to toggle source
# File lib/it/interpolation.rb, line 6
def call(string, values)
  key, label = extract_key_and_label_from(string)
  value = values[key]

  raise KeyError, "key{#{key}} not found" unless values.key?(key)

  new(key, value, label).process
end
new(key, value, label) click to toggle source
# File lib/it/interpolation.rb, line 24
def initialize(key, value, label)
  @key = key
  @value = value
  @label = label
end

Private Class Methods

extract_key_and_label_from(string) click to toggle source

This is a :reek:UtilityFunction, but it's not an instance method

# File lib/it/interpolation.rb, line 18
def extract_key_and_label_from(string)
  # eg: %{key:label} or %{key} or %{key: label}
  string[2..-2].split(/:\s*/, 2)
end

Public Instance Methods

process() click to toggle source
# File lib/it/interpolation.rb, line 30
def process
  convert_link

  validate_value_for_arguments

  if value.is_a?(It::Tag)
    process_it_tag
  else # Normal interpolations, as I18n.t would do it.
    ERB::Util.h(value)
  end
end

Private Instance Methods

process_it_tag() click to toggle source
# File lib/it/interpolation.rb, line 51
def process_it_tag
  if label
    value.process(label.html_safe)
  else
    value.process
  end
end
validate_value_for_arguments() click to toggle source
# File lib/it/interpolation.rb, line 59
def validate_value_for_arguments
  if label && !value.is_a?(It::Tag)
    raise ArgumentError, "key{#{key}} has an argument, so it cannot resolved with a #{value.class}"
  end
end