Sha256: aa22a86f890fe5ad63b5f480f6567c830cc0661aa414399954bab737dd590fd7

Contents?: true

Size: 1022 Bytes

Versions: 2

Compression:

Stored size: 1022 Bytes

Contents

# encoding: utf-8
# Main Class
module Filegen
  # This class is used as context for the erb-template
  class Data
    private

    attr_reader :data_sources

    public

    # Create context class
    #
    # @param [Array,DataSource] data_sources
    #   The data sources which should be available for variable lookup
    def initialize(data_sources)
      @data_sources = Array(data_sources)
    end

    # Lookup a variable within the data sources
    #
    # @param [String] variable
    #   The variable to lookup
    # @return [String]
    #   The value of the variable
    def lookup(variable, default_value='')
      try_to_fetch_unless_found_or_end(variable) || default_value
    end

    # Make the binding of the class available
    # @return [Binding]
    def instance_binding
      binding
    end

    private

    def try_to_fetch_unless_found_or_end(variable)
      result = nil

      data_sources.each do |s|
        (result = s.fetch(variable)) && (return result)
      end

      nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
filegen-0.2.1 lib/filegen/data.rb
filegen-0.2.0 lib/filegen/data.rb