Sha256: 76a4a10c0b0ac9d093622c76c749dc972bb281eaf3a64fce9c5b8064d753ed66

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module It
  # Contains one interpolation and will delegate the work to the It::Tag (or subclass) or
  # ERB::Util.h
  class Interpolation
    attr_accessor :key, :label, :value

    def initialize(string, values)
      self.key, self.label = string[2..-2].split(':', 2)
      validate_key_presence(values)

      self.value = values[key]
      convert_links
    end

    def process
      validate_value_for_arguments

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

    private

    # Convert keys with String arguments into It::Links, if they are named link, link_* or *_link
    def convert_links
      if key =~ /(\Alink\z|_link\z|\Alink_)/ && value.is_a?(String)
        self.value = It::Link.new(value)
      end
    end

    def process_it_tag
      if label
        value.process(label.html_safe)
      else
        value.process
      end
    end

    def validate_key_presence(values)
      fail KeyError, "key{#{key}} not found" unless values.key?(key)
    end

    def validate_value_for_arguments
      if label && !value.is_a?(It::Tag)
        fail ArgumentError, "key{#{key}} has an argument, so it cannot resolved with a #{value.class}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
it-0.8.0 lib/it/interpolation.rb