Permalink
Browse files

Merge pull request #12 from bak/namespaced_data

Support namespaced keys
  • Loading branch information...
cheezy committed Aug 31, 2013
2 parents f49dc8f + 6afde96 commit a7ee9ce379f39fca9503cd8979ee3278aa168d78
Showing with 32 additions and 4 deletions.
  1. +9 −2 README.md
  2. +3 −0 config/data/user.yml
  3. +9 −2 lib/data_magic.rb
  4. +11 −0 spec/lib/data_magic_spec.rb
View
@@ -15,14 +15,13 @@ DataMagic.yml_directory = 'data/yml'
If you do not specify a directory the gem will default to using a directory named _config/data_.
-
After setting the directory you must load a file. This can be accomplished by calling the _load_ method.
````ruby
DataMagic.load 'filename.yml'
````
-If you do not specify a filename the gem will attempt to use a file named _default.yml_. If you are using this for testing you will more than likely want to call load before each test to load the proper data for the spcific test.
+If you do not specify a filename the gem will attempt to use a file named _default.yml_. If you are using this for testing you will more than likely want to call load before each test to load the proper data for the specific test, or use the namespaced keys method, detailed below.
The final thing to do is use the data. The gem has a _data_for_ method that will return the data for a specific key. The most common way to use this is to include the _DataMagic_ module in a [page-object](https://github.com/cheezy/page-object) and then populate a page with the data. Here's an example:
@@ -41,6 +40,14 @@ end
Notice that I am including the module on line 3. On lin 8 I am calling the _data_for_ method passing the key _:my_page_. The _populate_page_with_ method is a part of the page-object gem.
+To organize your data into namespaces, and load that data just in time for testing, use namespaced keys instead:
+
+````ruby
+ page.populate_page_with data_for "user_form/valid"
+````
+
+This will load `user_form.yml`, and populate the page with the `valid:` record therein.
+
Your data might look something like this:
my_page:
View
@@ -0,0 +1,3 @@
+valid:
+ name: Testy McTesterson
+ job: Tester
View
@@ -4,6 +4,7 @@
require "data_magic/translation"
require 'yml_reader'
require 'faker'
+ require 'pry'
module DataMagic
extend YmlReader
@@ -18,8 +19,14 @@ def self.included(cls)
end
def data_for(key, additional={})
- DataMagic.load('default.yml') unless DataMagic.yml
- data = DataMagic.yml[key.to_s]
+ if key.is_a?(String) && key.match(%r{/})
+ filename, record = key.split('/')
+ DataMagic.load("#{filename}.yml")
+ else
+ record = key.to_s
+ DataMagic.load('default.yml') unless DataMagic.yml
+ end
+ data = DataMagic.yml[record]
raise ArgumentError, "Undefined key #{key}" unless data
prep_data data.merge(additional).clone
end
@@ -23,4 +23,15 @@
DataMagic.load("fname")
end
end
+
+ context "namespaced keys" do
+ it "loads correct file and retrieves data" do
+ DataMagic.yml_directory = 'config/data'
+ class UserPage
+ include DataMagic
+ end
+ data = UserPage.new.data_for "user/valid"
+ expect(data.keys.sort).to eq(['job','name'])
+ end
+ end
end

0 comments on commit a7ee9ce

Please sign in to comment.