Sha256: ad7fc1acd4a341025101158b801dd32dd13a42a85e2f84592182e8daeebfe3e8

Contents?: true

Size: 926 Bytes

Versions: 5

Compression:

Stored size: 926 Bytes

Contents

autoload :Mustache, 'mustache'

module Psychic
  class RegexpTokenHandler
    def initialize(template, token_pattern, token_replacement)
      @template = template
      @token_pattern = token_pattern
      @token_replacement = token_replacement
    end

    def tokens
      @template.scan(@token_pattern).flatten.uniq
    end

    def render(variables = {})
      @template.gsub(@token_pattern) do
        full_match = Regexp.last_match[0]
        key = Regexp.last_match[1]
        value = variables[key]
        value = @token_replacement.gsub('\\1', value.to_s) unless @token_replacement.nil?
        full_match.gsub(@token_pattern, value)
      end
    end
  end

  class MustacheTokenHandler
    def initialize(template)
      @template = Mustache::Template.new(template)
    end

    def tokens
      @template.tags
    end

    def render(variables = {})
      Mustache.render(@template, variables)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
psychic-runner-0.0.8 lib/psychic/tokens.rb
psychic-runner-0.0.7 lib/psychic/tokens.rb
psychic-runner-0.0.6 lib/psychic/tokens.rb
psychic-runner-0.0.5 lib/psychic/tokens.rb
psychic-runner-0.0.4 lib/psychic/tokens.rb