Sha256: 025fea47706232380f03a83aab577dd356e995e892f05d99df7e3cf30922fc46

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'data_magic/core_ext/string'
require 'data_magic/core_ext/fixnum'
require "data_magic/version"
require "data_magic/translation"
require 'data_magic/date_translation'
require 'data_magic/standard_translation'
require 'yml_reader'
require 'faker'

module DataMagic
  extend YmlReader
  extend StandardTranslation
  extend DateTranslation

  attr_reader :parent

  I18n.enforce_available_locales = false if I18n.respond_to? :enforce_available_locales

  def self.locale=(value)
    Faker::Config.locale = value
  end
  
  def self.included(cls)
    @parent = cls
    translators.each do |translator|
      Translation.send :include, translator
    end
  end

  def data_for(key, additional={})
    if key.is_a?(String) && key.match(%r{/})
      filename, record = key.split('/')
      DataMagic.load("#{filename}.yml")
    else
      record = key.to_s
      DataMagic.load(the_file) unless DataMagic.yml
    end
    data = DataMagic.yml[record]
    raise ArgumentError, "Undefined key #{key}" unless data
    prep_data data.merge(additional).clone
  end

  private

  def the_file
    ENV['DATA_MAGIC_FILE'] ? ENV['DATA_MAGIC_FILE'] :  'default.yml'
  end

  def prep_data(data)
    data.each do |key, value|
      unless value.nil?
        next if !value.respond_to?('[]') || value.is_a?(Numeric)
        next if value.is_a?(Hash)
        data[key] = translate(value[1..-1]) if value[0,1] == "~"
      end
    end
    data
  end

  def translate(value)
    translation.send :process, value
  end

  def translation
    @translation ||= Translation.new parent
  end

  class << self
    attr_accessor :yml
  
    def default_directory
      'config/data'
    end

    def add_translator(translator)
      translators << translator
    end

    def translators
      @translators ||= []
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
data_magic-0.21 lib/data_magic.rb