structure 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -15,3 +15,7 @@
15
15
  # 0.11
16
16
 
17
17
  * .key now emulates the DataMapper.property method.
18
+
19
+ # 0.12
20
+
21
+ * Add basic support for static models.
data/README.md CHANGED
@@ -79,7 +79,7 @@ Talk JSON:
79
79
  require 'structure/json'
80
80
 
81
81
  json = p1.to_json
82
- => {"json_class":"Person","name":"John","age":28,"friends":[{"json_class":"Person","name":"John Doe","age":null,"friends":[]}],"partner":null}
82
+ => {"json_class":"Person","name":"Gilles","age":28,"friends":[{"json_class":"Person","name":"John Doe","age":null,"friends":[]}],"partner":null}
83
83
 
84
84
  person = JSON.parse(json)
85
85
  person.friends.first
@@ -0,0 +1,52 @@
1
+ require 'yaml'
2
+
3
+ class Structure
4
+
5
+ # An enumurable static structure, sourced from a yaml file.
6
+ module Static
7
+ def self.included(base)
8
+ base.key(:id, Integer)
9
+ base.extend(ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+ include Enumerable
14
+
15
+ attr_accessor :data_path
16
+
17
+ def all
18
+ @all ||= data.map do |record|
19
+ record["id"] ||= increment_id
20
+ new(record)
21
+ end
22
+ end
23
+
24
+ def each(&block)
25
+ all.each { |record| block.call(record) }
26
+ end
27
+
28
+ def find(id)
29
+ detect { |record| record.id == id }
30
+ end
31
+
32
+ def set_data_path(data_path)
33
+ @data_path = data_path
34
+ end
35
+
36
+ private
37
+
38
+ def data
39
+ YAML.load_file(data_path)
40
+ end
41
+
42
+ # Overwrite this method with an opiniated location to dry if necessary.
43
+ def data_path
44
+ @data_path
45
+ end
46
+
47
+ def increment_id
48
+ @increment_id = @increment_id.to_i + 1
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,3 +1,3 @@
1
1
  class Structure
2
- VERSION = '0.11.0'
2
+ VERSION = '0.12.0'
3
3
  end
@@ -0,0 +1,5 @@
1
+ ---
2
+ - id: 1
3
+ name: New York
4
+ - id: 2
5
+ name: Tokyo
@@ -0,0 +1,4 @@
1
+ ---
2
+ - name: New York
3
+ - name: Tokyo
4
+ - name: Paris
@@ -0,0 +1,9 @@
1
+ require 'structure/static'
2
+
3
+ class City < Structure
4
+ include Static
5
+
6
+ set_data_path File.expand_path("../../fixtures/cities.yml", __FILE__)
7
+
8
+ key :name
9
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ class Structure
4
+ describe "A static structure" do
5
+ it "is enumerable" do
6
+ City.should be_an Enumerable
7
+ end
8
+
9
+ describe ".all" do
10
+ it "returns all records" do
11
+ City.all.should have(2).cities
12
+ end
13
+ end
14
+
15
+ describe ".find" do
16
+ it "finds a record by its id" do
17
+ City.find(1).should be_a City
18
+ end
19
+
20
+ it "returns nil if the record does not exist" do
21
+ City.find(4).should be_nil
22
+ end
23
+ end
24
+
25
+ describe ".set_data_path" do
26
+ it "sets the data path" do
27
+ City.set_data_path("foo")
28
+ City.send(:data_path).should eql "foo"
29
+ end
30
+ end
31
+
32
+ context "when sourced data does not include ids" do
33
+ before(:all) do
34
+ City.instance_variable_set(:@all, nil)
35
+ fixture = File.expand_path("../../fixtures/cities_without_id.yml", __FILE__)
36
+ City.set_data_path(fixture)
37
+ end
38
+
39
+ it "should auto-increment the ids of loaded records" do
40
+ City.map(&:id).should =~ [1, 2, 3]
41
+ end
42
+
43
+ describe ".increment_id" do
44
+ before do
45
+ City.instance_variable_set(:@increment_id, nil)
46
+ end
47
+
48
+ it "starts from 1" do
49
+ City.send(:increment_id).should eql 1
50
+ end
51
+
52
+ it "auto-increments" do
53
+ City.send(:increment_id)
54
+ City.send(:increment_id).should eql 2
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 11
7
+ - 12
8
8
  - 0
9
- version: 0.11.0
9
+ version: 0.12.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Paper Cavalier
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-07-05 00:00:00 +01:00
17
+ date: 2011-07-07 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -80,11 +80,16 @@ files:
80
80
  - benchmark.rb
81
81
  - lib/structure.rb
82
82
  - lib/structure/json.rb
83
+ - lib/structure/static.rb
83
84
  - lib/structure/version.rb
85
+ - spec/fixtures/cities.yml
86
+ - spec/fixtures/cities_without_id.yml
84
87
  - spec/models/book.rb
88
+ - spec/models/city.rb
85
89
  - spec/models/person.rb
86
90
  - spec/spec_helper.rb
87
91
  - spec/structure/json_spec.rb
92
+ - spec/structure/static_spec.rb
88
93
  - spec/structure_spec.rb
89
94
  - structure.gemspec
90
95
  has_rdoc: true
@@ -120,8 +125,12 @@ signing_key:
120
125
  specification_version: 3
121
126
  summary: A key/value container for modeling ephemeral data
122
127
  test_files:
128
+ - spec/fixtures/cities.yml
129
+ - spec/fixtures/cities_without_id.yml
123
130
  - spec/models/book.rb
131
+ - spec/models/city.rb
124
132
  - spec/models/person.rb
125
133
  - spec/spec_helper.rb
126
134
  - spec/structure/json_spec.rb
135
+ - spec/structure/static_spec.rb
127
136
  - spec/structure_spec.rb