Module: DataMagic

Extended by:
DateTranslation, StandardTranslation, YmlReader
Defined in:
lib/data_magic.rb,
lib/data_magic/version.rb,
lib/data_magic/translation.rb,
lib/data_magic/date_translation.rb,
lib/data_magic/standard_translation.rb

Defined Under Namespace

Modules: DateTranslation, StandardTranslation Classes: Translation

Constant Summary

VERSION =
"1.2"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DateTranslation

day_of_week, day_of_week_abbr, month, month_abbr, today, tomorrow, yesterday

Methods included from StandardTranslation

catch_phrase, cell_phone, characters, city, company_name, country, credit_card_number, credit_card_type, domain_name, email_address, first_name, full_name, last_name, mask, name_prefix, name_suffix, paragraphs, phone_number, randomize, secondary_address, sentence, sentences, sequential, state, state_abbr, street_address, title, url, user_name, words, zip_code

Class Attribute Details

.ymlObject

Returns the value of attribute yml



96
97
98
# File 'lib/data_magic.rb', line 96

def yml
  @yml
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent



16
17
18
# File 'lib/data_magic.rb', line 16

def parent
  @parent
end

Class Method Details

.add_translator(translator) ⇒ Object



102
103
104
# File 'lib/data_magic.rb', line 102

def add_translator(translator)
  translators << translator
end

.default_directoryObject



98
99
100
# File 'lib/data_magic.rb', line 98

def default_directory
  'config/data'
end

.included(cls) ⇒ Object



24
25
26
27
28
29
# File 'lib/data_magic.rb', line 24

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

.load_for_scenario(scenario, fixture_folder = DataMagic.yml_directory) ⇒ Object

Given a scenario, load any fixture it needs. Fixture tags should be in the form of @datamagic_FIXTUREFILE



46
47
48
49
50
51
52
53
# File 'lib/data_magic.rb', line 46

def self.load_for_scenario(scenario, fixture_folder = DataMagic.yml_directory)
  orig_yml_directory = DataMagic.yml_directory
  DataMagic.yml_directory = fixture_folder
  fixture_files = fixture_files_on(scenario)

  DataMagic.load "#{fixture_files.last}.yml" if fixture_files.count > 0
  DataMagic.yml_directory = orig_yml_directory
end

.locale=(value) ⇒ Object



20
21
22
# File 'lib/data_magic.rb', line 20

def self.locale=(value)
  Faker::Config.locale = value
end

.translatorsObject



106
107
108
# File 'lib/data_magic.rb', line 106

def translators
  @translators ||= []
end

Instance Method Details

#data_for(key, additional = {}) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/data_magic.rb', line 31

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.key?(record) ? additional[record] : additional).deep_copy)
end