Skip to content
Browse files

added add_translator to allow custom translators to be mixed in

  • Loading branch information...
1 parent 979ad18 commit eea9344e72e2e609a4effeade81e582b1ff69051 @cheezy committed Feb 7, 2013
Showing with 38 additions and 0 deletions.
  1. +1 −0 ChangeLog
  2. +4 −0 features/data_magic.feature
  3. +16 −0 features/step_definitions/data_magic_steps.rb
  4. +3 −0 features/yaml/example.yml
  5. +14 −0 lib/data_magic.rb
View
1 ChangeLog
@@ -1,5 +1,6 @@
=== Version 0.13
* Enhancements
+ * Added add_translator method to DataMagic to allow custom translators to be added
* Added months translator
* Added dm_* aliases for all generators
View
4 features/data_magic.feature
@@ -94,3 +94,7 @@ Feature: Functionality of the data_magic gem
Scenario: Getting a random month name
Then the value for "some_month" should be a valid month
+
+ Scenario: It should allow one to add new translator methods
+ When I add the blah translator
+ Then the value for "blah" should be "foobar"
View
16 features/step_definitions/data_magic_steps.rb
@@ -102,3 +102,19 @@ class TestClass
months = %w[January February March April May June July August September October November December]
months.should include @data[key]
end
+
+
+When /^I add the blah translator$/ do
+ module Blah
+ def blah
+ 'foobar'
+ end
+ end
+ DataMagic.add_translator(Blah)
+
+ class TranslatorAdded
+ include DataMagic
+ end
+ ta = TranslatorAdded.new
+ @data = ta.data_for 'dynamic'
+end
View
3 features/yaml/example.yml
@@ -41,6 +41,9 @@ dm:
5daysago: ~5.days_ago
some_month: ~month
+dynamic:
+ blah: ~blah
+
other:
name: Cheezy
address: 123 Main Street
View
14 lib/data_magic.rb
@@ -12,6 +12,12 @@ module DataMagic
include DateTranslation
extend YmlReader
+ def self.included(cls)
+ translators.each do |translator|
+ cls.send :include, translator
+ end
+ end
+
def data_for(key, additional={})
DataMagic.load('default.yml') unless DataMagic.yml
data = DataMagic.yml[key.to_s]
@@ -36,6 +42,14 @@ class << self
def default_directory
'config/data'
end
+
+ def add_translator(translator)
+ translators << translator
+ end
+
+ def translators
+ @translators ||= []
+ end
end
end

0 comments on commit eea9344

Please sign in to comment.
Something went wrong with that request. Please try again.