ascheink-active_yaml 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.5
data/active_yaml.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{active_yaml}
5
- s.version = "0.0.3"
5
+ s.version = "0.0.5"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Andrei Scheinkman"]
@@ -23,7 +23,9 @@ Gem::Specification.new do |s|
23
23
  "lib/active_yaml.rb",
24
24
  "test/active_yaml_test.rb",
25
25
  "test/app/models/city.rb",
26
+ "test/app/models/state.rb",
26
27
  "test/db/yaml/city.yml",
28
+ "test/db/yaml/state.yml",
27
29
  "test/test_helper.rb"
28
30
  ]
29
31
  s.has_rdoc = true
@@ -35,6 +37,7 @@ Gem::Specification.new do |s|
35
37
  s.test_files = [
36
38
  "test/active_yaml_test.rb",
37
39
  "test/app/models/city.rb",
40
+ "test/app/models/state.rb",
38
41
  "test/test_helper.rb"
39
42
  ]
40
43
 
data/lib/active_yaml.rb CHANGED
@@ -8,6 +8,7 @@ class ActiveYAML
8
8
 
9
9
  def self.inherited(p)
10
10
  data = YAML.load_file("#{RAILS_ROOT}/db/yaml/#{p.name.underscore}.yml")
11
+ p.extend(Enumerable)
11
12
  p.class_eval do
12
13
  @instances = []
13
14
  fields = []
@@ -26,6 +27,7 @@ class ActiveYAML
26
27
  class_eval "def self.find(id); find_by_id(id) or raise ActiveRecord::RecordNotFound; end"
27
28
  class_eval "def self.first; @instances.first; end"
28
29
  class_eval "def self.last; @instances.last; end"
30
+ class_eval "def self.each; @instances.each { |b| yield b }; end"
29
31
  s = "#{p.to_s}(#{fields.join(', ')})"
30
32
  class_eval "def self.to_s; \"#{s}\"; end"
31
33
  def self.find_all
@@ -4,6 +4,7 @@ class ActiveYamlTest < Test::Unit::TestCase
4
4
 
5
5
  ActiveYAML::RAILS_ROOT = 'test'
6
6
  require 'app/models/city.rb'
7
+ require 'app/models/state.rb'
7
8
 
8
9
  context "class" do
9
10
  should "create find_all" do
@@ -27,6 +28,14 @@ class ActiveYamlTest < Test::Unit::TestCase
27
28
  assert_respond_to City, :find_all_by_population
28
29
  end
29
30
 
31
+ should "support each" do
32
+ assert_respond_to City, :each
33
+ end
34
+
35
+ should "be Enumerable" do
36
+ assert_respond_to City, :any?
37
+ end
38
+
30
39
  should "print out fields in to_s" do
31
40
  assert_equal "City(id, population, display_name)", City.to_s
32
41
  end
@@ -42,7 +51,24 @@ class ActiveYamlTest < Test::Unit::TestCase
42
51
  assert_respond_to @i, :display_name
43
52
  assert_respond_to @i, :population
44
53
  end
45
-
46
54
  end
47
-
55
+
56
+ context "data" do
57
+ should "load all records" do
58
+ assert_equal 3, City.find_all.size
59
+ end
60
+ should "load fields" do
61
+ nyc = City.find('NYC')
62
+ assert_equal "New York", nyc.display_name
63
+ assert_equal 8274527, nyc.population
64
+ end
65
+ should "not confuse records from different models" do
66
+ assert_equal 3, City.find_all.size
67
+ assert_equal 2, State.find_all.size
68
+ assert_equal 3, City.find_all.size
69
+ end
70
+ should "support enumerable" do
71
+ assert_equal 14951385, City.inject(0) { |a, c| a + c.population }
72
+ end
73
+ end
48
74
  end
@@ -0,0 +1,4 @@
1
+ require 'lib/active_yaml'
2
+
3
+ class State < ActiveYAML
4
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ascheink-active_yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Scheinkman
@@ -73,4 +73,5 @@ summary: ActiveYAML
73
73
  test_files:
74
74
  - test/active_yaml_test.rb
75
75
  - test/app/models/city.rb
76
+ - test/app/models/state.rb
76
77
  - test/test_helper.rb