gom-filesystem-adapter 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -52,3 +52,8 @@ Development has been done test-driven and the code follows at most the Clean Cod
52
52
  removed by using the reek[http://github.com/kevinrutherford/reek] code smell detector.
53
53
 
54
54
  This project is still experimental and under development. Any bug report and contribution is welcome!
55
+
56
+ == Support
57
+
58
+ Apart from contribution, support via Flattr[http://flattr.com/thing/108996/Generic-Object-Mapper-Filesystem-adapter]
59
+ is welcome.
@@ -1,4 +1,4 @@
1
1
  require 'gom'
2
- require File.join(File.dirname(__FILE__), "gom", "storage")
2
+ require File.join(File.dirname(__FILE__), "storage")
3
3
 
4
4
  GOM::Storage::Adapter.register :filesystem, GOM::Storage::Filesystem::Adapter
data/lib/gom/storage.rb CHANGED
@@ -1,10 +1,6 @@
1
1
 
2
- module GOM
2
+ module GOM::Storage
3
3
 
4
- module Storage
5
-
6
- autoload :Filesystem, File.join(File.dirname(__FILE__), "storage", "filesystem")
7
-
8
- end
4
+ autoload :Filesystem, File.join(File.dirname(__FILE__), "storage", "filesystem")
9
5
 
10
6
  end
@@ -1,16 +1,8 @@
1
1
 
2
- module GOM
2
+ module GOM::Storage::Filesystem
3
3
 
4
- module Storage
5
-
6
- module Filesystem
7
-
8
- autoload :Adapter, File.join(File.dirname(__FILE__), "filesystem", "adapter")
9
- autoload :Collection, File.join(File.dirname(__FILE__), "filesystem", "collection")
10
- autoload :Loader, File.join(File.dirname(__FILE__), "filesystem", "loader")
11
-
12
- end
13
-
14
- end
4
+ autoload :Adapter, File.join(File.dirname(__FILE__), "filesystem", "adapter")
5
+ autoload :Collection, File.join(File.dirname(__FILE__), "filesystem", "collection")
6
+ autoload :Loader, File.join(File.dirname(__FILE__), "filesystem", "loader")
15
7
 
16
8
  end
@@ -1,50 +1,52 @@
1
1
 
2
- module GOM
2
+ # The filesystem storage adapter
3
+ class GOM::Storage::Filesystem::Adapter < GOM::Storage::Adapter
3
4
 
4
- module Storage
5
+ attr_reader :drafts
5
6
 
6
- module Filesystem
7
-
8
- # The filesystem storage adapter
9
- class Adapter < GOM::Storage::Adapter
10
-
11
- def setup
12
- load_drafts
13
- end
14
-
15
- def fetch(id)
16
- @drafts[id]
17
- end
7
+ def setup
8
+ load_drafts
9
+ end
18
10
 
19
- def store(*arguments)
20
- read_only_error
21
- end
11
+ def teardown
12
+ @drafts = nil
13
+ end
22
14
 
23
- def remove(*arguments)
24
- read_only_error
25
- end
15
+ def fetch(id)
16
+ check_setup
17
+ @drafts[id]
18
+ end
26
19
 
27
- def collection(view_name, options = { })
28
- view = configuration.views[view_name.to_sym]
29
- raise ViewNotFoundError, "there are no view with the name #{view_name}" unless view
30
- fetcher = Collection::Fetcher.new @drafts, view
31
- GOM::Object::Collection.new fetcher
32
- end
20
+ def store(*arguments)
21
+ check_setup
22
+ read_only_error
23
+ end
33
24
 
34
- private
25
+ def remove(*arguments)
26
+ check_setup
27
+ read_only_error
28
+ end
35
29
 
36
- def load_drafts
37
- @drafts = GOM::Storage::Filesystem::Loader.new(configuration[:directory]).drafts
38
- end
30
+ def collection(view_name, options = { })
31
+ check_setup
32
+ view = configuration.views[view_name.to_sym]
33
+ raise ViewNotFoundError, "there are no view with the name #{view_name}" unless view
34
+ fetcher = GOM::Storage::Filesystem::Collection::Fetcher.new @drafts, view
35
+ GOM::Object::Collection.new fetcher
36
+ end
39
37
 
40
- def read_only_error
41
- raise GOM::Storage::ReadOnlyError, "The adapter doesn't provide write methods"
42
- end
38
+ private
43
39
 
44
- end
40
+ def check_setup
41
+ raise GOM::Storage::Adapter::NoSetupError unless @drafts
42
+ end
45
43
 
46
- end
44
+ def load_drafts
45
+ @drafts = GOM::Storage::Filesystem::Loader.new(configuration[:files]).drafts
46
+ end
47
47
 
48
+ def read_only_error
49
+ raise GOM::Storage::ReadOnlyError, "The adapter doesn't provide write methods"
48
50
  end
49
51
 
50
52
  end
@@ -1,18 +1,6 @@
1
1
 
2
- module GOM
2
+ module GOM::Storage::Filesystem::Collection
3
3
 
4
- module Storage
5
-
6
- module Filesystem
7
-
8
- module Collection
9
-
10
- autoload :Fetcher, File.join(File.dirname(__FILE__), "collection", "fetcher")
11
-
12
- end
13
-
14
- end
15
-
16
- end
4
+ autoload :Fetcher, File.join(File.dirname(__FILE__), "collection", "fetcher")
17
5
 
18
6
  end
@@ -1,32 +1,16 @@
1
1
 
2
- module GOM
2
+ # A class collection fetcher for the filesystem adapter.
3
+ class GOM::Storage::Filesystem::Collection::Fetcher
3
4
 
4
- module Storage
5
+ attr_accessor :drafts
6
+ attr_accessor :view
5
7
 
6
- module Filesystem
7
-
8
- module Collection
9
-
10
- # A class collection fetcher for the filesystem adapter.
11
- class Fetcher
12
-
13
- attr_accessor :drafts
14
- attr_accessor :view
15
-
16
- def initialize(drafts, view)
17
- @drafts, @view = drafts, view
18
- end
19
-
20
- def drafts
21
- @drafts.values.select{ |draft| draft.class_name == @view.class_name }
22
- end
23
-
24
- end
25
-
26
- end
27
-
28
- end
8
+ def initialize(drafts, view)
9
+ @drafts, @view = drafts, view
10
+ end
29
11
 
12
+ def drafts
13
+ @drafts.values.select{ |draft| draft.class_name == @view.class_name }
30
14
  end
31
15
 
32
16
  end
@@ -1,86 +1,73 @@
1
1
  require 'yaml'
2
2
 
3
- module GOM
3
+ # The loader for the filesystem data that is provided by the storage adapter.
4
+ class GOM::Storage::Filesystem::Loader
4
5
 
5
- module Storage
6
+ attr_accessor :files
6
7
 
7
- module Filesystem
8
-
9
- # The loader for the filesystem data that is provided by the storage adapter.
10
- class Loader
11
-
12
- attr_accessor :directory
13
-
14
- def initialize(directory)
15
- @directory = directory
16
- end
17
-
18
- def drafts
19
- @drafts = { }
20
- load_yml_files
21
- @drafts
22
- end
23
-
24
- private
25
-
26
- def load_yml_files
27
- Dir[File.join(@directory, "*.yml")].each do |filename|
28
- load_yml_file filename
29
- end
30
- end
8
+ def initialize(files)
9
+ @files = files
10
+ end
31
11
 
32
- def load_yml_file(filename)
33
- classname = File.basename(filename).sub(/\..*$/, "")
34
- load_file_hash classname, YAML.load_file(filename)
35
- end
12
+ def drafts
13
+ @drafts = { }
14
+ load_yml_files
15
+ @drafts
16
+ end
36
17
 
37
- def load_file_hash(classname, file_hash)
38
- file_hash.each do |id, hash|
39
- @drafts[id] = Builder.new(classname, hash).draft
40
- end
41
- end
18
+ private
42
19
 
43
- # Builds a draft out of the file hashes.
44
- class Builder
20
+ def load_yml_files
21
+ Dir[@files].each do |filename|
22
+ load_yml_file filename
23
+ end
24
+ end
45
25
 
46
- def initialize(class_name, hash)
47
- @class_name, @hash = class_name, hash
48
- end
26
+ def load_yml_file(filename)
27
+ load_file_hash YAML.load_file(filename)
28
+ end
49
29
 
50
- def draft
51
- @draft = GOM::Object::Draft.new
52
- set_class_name
53
- set_properties
54
- set_relations
55
- @draft
56
- end
30
+ def load_file_hash(file_hash)
31
+ file_hash.each do |id, hash|
32
+ @drafts[id] = Builder.new(hash).draft
33
+ end
34
+ end
57
35
 
58
- private
36
+ # Builds a draft out of the file hashes.
37
+ class Builder
59
38
 
60
- def set_class_name
61
- @draft.class_name = @class_name
62
- end
39
+ def initialize(hash)
40
+ @hash = hash
41
+ end
63
42
 
64
- def set_properties
65
- properties = { }
66
- (@hash["properties"] || { }).each do |key, value|
67
- properties[key] = value
68
- end
69
- @draft.properties = properties unless properties.empty?
70
- end
43
+ def draft
44
+ @draft = GOM::Object::Draft.new
45
+ set_class_name
46
+ set_properties
47
+ set_relations
48
+ @draft
49
+ end
71
50
 
72
- def set_relations
73
- relations = { }
74
- (@hash["relations"] || { }).each do |key, value|
75
- relations[key] = GOM::Object::Proxy.new GOM::Object::Id.new(value)
76
- end
77
- @draft.relations = relations unless relations.empty?
78
- end
51
+ private
79
52
 
80
- end
53
+ def set_class_name
54
+ @draft.class_name = @hash["class"] || "Object"
55
+ end
81
56
 
57
+ def set_properties
58
+ properties = { }
59
+ (@hash["properties"] || { }).each do |key, value|
60
+ properties[key] = value
82
61
  end
62
+ @draft.properties = properties unless properties.empty?
63
+ end
83
64
 
65
+ def set_relations
66
+ relations = { }
67
+ (@hash["relations"] || { }).each do |key, value|
68
+ relations[key] = GOM::Object::Proxy.new GOM::Object::Id.new(value)
69
+ end
70
+ @draft.relations = relations unless relations.empty?
84
71
  end
85
72
 
86
73
  end
@@ -1,10 +1,27 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib", "filesystem"))
3
2
 
4
3
  GOM::Storage::Configuration.read File.join(File.dirname(__FILE__), "..", "storage.configuration")
5
4
 
6
5
  describe "filesystem adapter" do
7
6
 
7
+ it_should_behave_like "an adapter that needs setup"
8
+
8
9
  it_should_behave_like "a read-only adapter connected to a stateless storage"
9
10
 
11
+ describe "setup without any data files" do
12
+
13
+ before :each do
14
+ GOM::Storage.teardown
15
+ GOM::Storage::Configuration[:test_storage].hash[:files] = "invalid"
16
+ end
17
+
18
+ it "should raise no exception" do
19
+ lambda do
20
+ GOM::Storage.setup
21
+ GOM::Storage.fetch "test_storage:object_1"
22
+ end.should_not raise_error
23
+ end
24
+
25
+ end
26
+
10
27
  end
@@ -1,10 +1,12 @@
1
1
 
2
2
  object_1:
3
+ class: GOM::Spec::Object
3
4
  properties:
4
5
  number: 5
5
6
  relations:
6
7
  related_object: test_storage:object_2
7
8
 
8
9
  object_2:
10
+ class: GOM::Spec::Object
9
11
  properties:
10
- test: test value
12
+ number: 7
@@ -10,7 +10,7 @@ describe GOM::Storage::Filesystem::Adapter do
10
10
  GOM::Storage::Filesystem::Loader.stub(:new).and_return(@loader)
11
11
 
12
12
  @configuration = mock GOM::Storage::Configuration
13
- @configuration.stub(:[]).with(:directory).and_return("test_directory")
13
+ @configuration.stub(:[]).with(:files).and_return("test_files")
14
14
  @adapter = described_class.new @configuration
15
15
  end
16
16
 
@@ -21,7 +21,7 @@ describe GOM::Storage::Filesystem::Adapter do
21
21
  describe "setup" do
22
22
 
23
23
  it "should initialize a file system loader" do
24
- GOM::Storage::Filesystem::Loader.should_receive(:new).with("test_directory").and_return(@loader)
24
+ GOM::Storage::Filesystem::Loader.should_receive(:new).with("test_files").and_return(@loader)
25
25
  @adapter.setup
26
26
  end
27
27
 
@@ -32,6 +32,20 @@ describe GOM::Storage::Filesystem::Adapter do
32
32
 
33
33
  end
34
34
 
35
+ describe "teardown" do
36
+
37
+ before :each do
38
+ @adapter.setup
39
+ end
40
+
41
+ it "should clear the loaded drafts" do
42
+ lambda do
43
+ @adapter.teardown
44
+ end.should change(@adapter, :drafts).from(@drafts).to(nil)
45
+ end
46
+
47
+ end
48
+
35
49
  describe "fetch" do
36
50
 
37
51
  before :each do
@@ -42,26 +56,55 @@ describe GOM::Storage::Filesystem::Adapter do
42
56
  @adapter.fetch("object_1").should == @draft
43
57
  end
44
58
 
59
+ it "should raise a #{GOM::Storage::Adapter::NoSetupError} if no drafts has been loaded" do
60
+ @adapter.teardown
61
+ lambda do
62
+ @adapter.fetch "object_1"
63
+ end.should raise_error(GOM::Storage::Adapter::NoSetupError)
64
+ end
65
+
45
66
  end
46
67
 
47
68
  describe "store" do
48
69
 
70
+ before :each do
71
+ @adapter.setup
72
+ end
73
+
49
74
  it "should raise a GOM::Storage::ReadOnlyError" do
50
75
  lambda do
51
76
  @adapter.store Object.new, "test_storage"
52
77
  end.should raise_error(GOM::Storage::ReadOnlyError)
53
78
  end
54
79
 
80
+ it "should raise a #{GOM::Storage::Adapter::NoSetupError} if no drafts has been loaded" do
81
+ @adapter.teardown
82
+ lambda do
83
+ @adapter.store Object.new, "test_storage"
84
+ end.should raise_error(GOM::Storage::Adapter::NoSetupError)
85
+ end
86
+
55
87
  end
56
88
 
57
89
  describe "remove" do
58
90
 
91
+ before :each do
92
+ @adapter.setup
93
+ end
94
+
59
95
  it "should raise a GOM::Storage::ReadOnlyError" do
60
96
  lambda do
61
97
  @adapter.remove Object.new
62
98
  end.should raise_error(GOM::Storage::ReadOnlyError)
63
99
  end
64
100
 
101
+ it "should raise a #{GOM::Storage::Adapter::NoSetupError} if no drafts has been loaded" do
102
+ @adapter.teardown
103
+ lambda do
104
+ @adapter.remove Object.new
105
+ end.should raise_error(GOM::Storage::Adapter::NoSetupError)
106
+ end
107
+
65
108
  end
66
109
 
67
110
  describe "collection" do
@@ -107,6 +150,13 @@ describe GOM::Storage::Filesystem::Adapter do
107
150
  collection.should == @collection
108
151
  end
109
152
 
153
+ it "should raise a #{GOM::Storage::Adapter::NoSetupError} if no drafts has been loaded" do
154
+ @adapter.teardown
155
+ lambda do
156
+ @adapter.collection :test_object_class_view
157
+ end.should raise_error(GOM::Storage::Adapter::NoSetupError)
158
+ end
159
+
110
160
  end
111
161
 
112
162
  end
@@ -3,14 +3,14 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "sp
3
3
  describe GOM::Storage::Filesystem::Loader do
4
4
 
5
5
  before :each do
6
- @directory = "directory"
7
- @loader = GOM::Storage::Filesystem::Loader.new @directory
6
+ @files = "files"
7
+ @loader = GOM::Storage::Filesystem::Loader.new @files
8
8
  end
9
9
 
10
10
  describe "initialize" do
11
11
 
12
12
  it "should set the directory" do
13
- @loader.directory.should == @directory
13
+ @loader.files.should == @files
14
14
  end
15
15
 
16
16
  end
@@ -27,6 +27,7 @@ describe GOM::Storage::Filesystem::Loader do
27
27
 
28
28
  @file_hash = {
29
29
  "object_1" => {
30
+ "class" => "Test::Model",
30
31
  "properties" => {
31
32
  "test" => "test value"
32
33
  },
@@ -43,8 +44,8 @@ describe GOM::Storage::Filesystem::Loader do
43
44
  YAML.stub(:load_file).and_return(@file_hash)
44
45
  end
45
46
 
46
- it "should searches all .yml file in the directory" do
47
- Dir.should_receive(:[]).with("directory/*.yml").and_return(@filenames)
47
+ it "should searches all files that matches the pattern" do
48
+ Dir.should_receive(:[]).with("files").and_return(@filenames)
48
49
  @loader.drafts
49
50
  end
50
51
 
@@ -60,7 +61,7 @@ describe GOM::Storage::Filesystem::Loader do
60
61
 
61
62
  it "should convert the file hash into drafts" do
62
63
  draft_one = @loader.drafts["object_1"]
63
- draft_one.class_name.should == "Object"
64
+ draft_one.class_name.should == "Test::Model"
64
65
  draft_one.properties.should == { "test" => "test value" }
65
66
  draft_one.relations.should == { "related_object" => @proxy }
66
67
 
data/spec/spec_helper.rb CHANGED
@@ -2,5 +2,5 @@ require 'rubygems'
2
2
  gem 'rspec', '>= 2'
3
3
  require 'rspec'
4
4
 
5
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "filesystem"))
5
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "gom", "filesystem-adapter"))
6
6
  require 'gom/spec'
@@ -1,7 +1,7 @@
1
1
 
2
2
  test_storage:
3
3
  adapter: filesystem
4
- directory: /home/phifty/projects/gom-filesystem-adapter/spec/acceptance/data
4
+ files: /home/phifty/projects/gom-filesystem-adapter/spec/acceptance/data/*.yml
5
5
  views:
6
6
  test_object_class_view:
7
7
  type: class
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Philipp Br\xC3\xBCll"
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-20 00:00:00 +01:00
17
+ date: 2011-02-17 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -27,9 +27,8 @@ dependencies:
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
29
  - 0
30
- - 2
31
- - 0
32
- version: 0.2.0
30
+ - 3
31
+ version: "0.3"
33
32
  type: :runtime
34
33
  version_requirements: *id001
35
34
  - !ruby/object:Gem::Dependency
@@ -71,20 +70,20 @@ files:
71
70
  - README.rdoc
72
71
  - LICENSE
73
72
  - Rakefile
74
- - lib/gom/storage/filesystem/adapter.rb
75
- - lib/gom/storage/filesystem/loader.rb
73
+ - lib/gom/storage.rb
74
+ - lib/gom/filesystem-adapter.rb
76
75
  - lib/gom/storage/filesystem/collection/fetcher.rb
77
76
  - lib/gom/storage/filesystem/collection.rb
77
+ - lib/gom/storage/filesystem/adapter.rb
78
+ - lib/gom/storage/filesystem/loader.rb
78
79
  - lib/gom/storage/filesystem.rb
79
- - lib/gom/storage.rb
80
- - lib/filesystem.rb
80
+ - spec/storage.configuration
81
+ - spec/gom/storage/filesystem/loader_spec.rb
81
82
  - spec/gom/storage/filesystem/collection/fetcher_spec.rb
82
83
  - spec/gom/storage/filesystem/adapter_spec.rb
83
- - spec/gom/storage/filesystem/loader_spec.rb
84
- - spec/storage.configuration
85
- - spec/spec_helper.rb
86
- - spec/acceptance/data/Object.yml
87
84
  - spec/acceptance/adapter_spec.rb
85
+ - spec/acceptance/data/objects.yml
86
+ - spec/spec_helper.rb
88
87
  has_rdoc: true
89
88
  homepage: http://github.com/phifty/gom-filesystem-adapter
90
89
  licenses: []
@@ -118,7 +117,7 @@ signing_key:
118
117
  specification_version: 3
119
118
  summary: Filesystem storage adapter for the General Object Mapper.
120
119
  test_files:
120
+ - spec/gom/storage/filesystem/loader_spec.rb
121
121
  - spec/gom/storage/filesystem/collection/fetcher_spec.rb
122
122
  - spec/gom/storage/filesystem/adapter_spec.rb
123
- - spec/gom/storage/filesystem/loader_spec.rb
124
123
  - spec/acceptance/adapter_spec.rb