Permalink
Browse files

Add support for loading yaml files that are organized as hashes

  • Loading branch information...
1 parent b480d9e commit 270b7509c83f0d76fda408346979ff43dd5b10e0 @zilkey committed Oct 14, 2009
Showing with 52 additions and 5 deletions.
  1. +1 −0 CHANGELOG
  2. +20 −1 README.md
  3. +5 −1 lib/active_yaml/base.rb
  4. +22 −3 spec/active_yaml/base_spec.rb
  5. +4 −0 spec/fixtures/array_rows.yml
View
@@ -2,6 +2,7 @@
- added ActiveHash::Base.has_many, which works with ActiveRecord or ActiveHash classes (thanks to baldwindavid)
- added ActiveHash::Base.belongs_to, which works with ActiveRecord or ActiveHash classes (thanks to baldwindavid)
- added .delete_all method that clears the in-memory array
+ - added support for Hash-style yaml (think, Rails fixtures)
2009-10-12
- auto-assign fields after calling data= instead of after calling .all
View
@@ -218,7 +218,26 @@ The above example will look for the file "/u/data/sample.yml".
ActiveYaml, as well as ActiveFile, check the mtime of the file you specified, and reloads the data if the mtime has changed. So you can replace the data in the files even if your app is running in production mode in rails.
-Since ActiveYaml just creates a hash from the YAML file, you will have all fields specified in YAML auto-defined for you once you call all.
+Since ActiveYaml just creates a hash from the YAML file, you will have all fields specified in YAML auto-defined for you once you call all. You can format your YAML as an array, or as a hash:
+
+ # array style
+ - id: 1
+ name: US
+ - id: 2
+ name: Canada
+ - id: 3
+ name: Mexico
+
+ # hash style
+ us:
+ id: 1
+ name: US
+ canada:
+ id: 2
+ name: Canada
+ mexico:
+ id: 3
+ name: Mexico
## ActiveFile
View
@@ -3,7 +3,11 @@ module ActiveYaml
class Base < ActiveFile::Base
class << self
def load_file
- YAML.load_file(full_path)
+ if (data = raw_data).is_a?(Array)
+ data
+ else
+ data.values
+ end
end
def raw_data
@@ -5,7 +5,7 @@
before do
ActiveYaml::Base.set_root_path File.expand_path(File.dirname(__FILE__) + "/../fixtures")
- class Country < ActiveYaml::Base
+ class ArrayRow < ActiveYaml::Base
end
class City < ActiveYaml::Base
@@ -16,7 +16,7 @@ class State < ActiveYaml::Base
end
after do
- Object.send :remove_const, :Country
+ Object.send :remove_const, :ArrayRow
Object.send :remove_const, :City
Object.send :remove_const, :State
end
@@ -29,7 +29,26 @@ class State < ActiveYaml::Base
end
it "returns the raw array data loaded from yaml array-formatted files" do
- Country.raw_data.should be_kind_of(Array)
+ ArrayRow.raw_data.should be_kind_of(Array)
+ end
+
+ end
+
+ describe ".load_file" do
+
+ describe "with array data" do
+ it "returns an array of hashes" do
+ ArrayRow.load_file.should be_kind_of(Array)
+ ArrayRow.load_file.should include({"name" => "Row 1", "id" => 1})
+ end
+ end
+
+ describe "with hash data" do
+ it "returns an array of hashes" do
+ City.load_file.should be_kind_of(Array)
+ City.load_file.should include({"state" => :new_york, "name" => "Albany", "id" => 1})
+ City.all.should include( City.new(:id => 1) )
+ end
end
end
@@ -0,0 +1,4 @@
+- id: 1
+ name: Row 1
+- id: 2
+ name: Row 2

0 comments on commit 270b750

Please sign in to comment.