Sha256: 9da57f3a50bf7191276639d8c328f071a41ca1a6e3db1f2f5f244a3e12275af5

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

#DataMagic Requires
require 'fig_magic/core_ext/string'
require 'fig_magic/core_ext/fixnum'
require 'fig_magic/translation'
require 'fig_magic/date_translation'
require 'fig_magic/standard_translation'
require 'faker'

#FigNewton Requires
require 'fig_magic/node'
require 'fig_magic/missing'

#Shared Requires
require 'yml_reader'
require 'fig_magic/version'


module FigMagic
  extend YmlReader
  extend StandardTranslation
  extend DateTranslation
  extend FigMagic::Missing

  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('/')
      FigMagic.load("#{filename}.yml")
    else
      record = key.to_s
      FigMagic.load(the_file) unless FigMagic.yml
    end
    data = FigMagic.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/'
    end

    def add_translator(translator)
      translators << translator
    end

    def translators
      @translators ||= []
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
fig_magic-0.5 lib/fig_magic.rb
fig_magic-0.4 lib/fig_magic.rb
fig_magic-0.3 lib/fig_magic.rb
fig_magic-0.2 lib/fig_magic.rb
fig_magic-0.1 lib/fig_magic.rb