Sha256: b698b1dcb3cd24fede66047ffcef442354b51a33098a60bfb13f2bb98c2c7e36

Contents?: true

Size: 992 Bytes

Versions: 1

Compression:

Stored size: 992 Bytes

Contents

require "yaml"

module DataSet
  module DataAccessor
    def method_missing(*args, &block)
      load_data_source unless @data_contents
      key = args.first
      value = @data_contents[key.to_s]
      value = args[1] if value.nil?
      value = yield(key.to_s) if value.nil? && block
      super if value.nil?
      value = DataSet::DataElement.new(value) unless type_known?(value)
      value
    end

    def respond_to_missing?(*args)
      super
    end

    def load_data_source
      @data_contents = nil
      path = "#{data_path}/#{ENV['DATA_SET_FILE']}"

      if ENV['DATA_SET_FILE']
        @data_contents = ::YAML.safe_load(ERB.new(
          File.read(path)
        ).result(binding))
      end

      DataSet.load('default.yml') if @data_contents.nil?
    end

    private

    def type_known?(value)
      known_types = [String, Integer, TrueClass, FalseClass,
                     Symbol, Float, Array]
      known_types.any? { |type| value.is_a?(type) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
data_set-2.0.0 lib/data_set/data_accessor.rb