Sha256: a4569cf788fb9e19f32d2b97ad3edbed43e0019889b7c57fb1a7febe893bac1e

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module Sablon
  # A context represents the user supplied arguments to render a
  # template.
  #
  # This module contains transformation functions to turn a
  # user supplied hash into a data structure suitable for rendering the
  # docx template.
  module Context
    class << self; attr_accessor :content_regex end
    self.content_regex = /\A([^:]+):(.+)\z/

    class << self
      def transform_hash(hash)
        Hash[hash.map { |k, v| transform_pair(k.to_s, v) }]
      end

      private

      def transform_standard_key(key, value)
        case value
        when Hash
          [key, transform_hash(value)]
        when Array
          [key, value.map { |v| v.is_a?(Hash) ? transform_hash(v) : v }]
        else
          [key, value]
        end
      end

      def transform_pair(key, value)
        if match = content_regex.match(key)
          if value.nil?
            [match[2], value]
          else
            type_id = match[1].to_sym
            [match[2], Content.make(type_id, value)]
          end
        else
          transform_standard_key(key, value)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sablon-0.4.1 lib/sablon/context.rb