yaml_adapter 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "shoulda", ">= 0"
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.5.2"
7
+ gem "rcov", ">= 0"
8
+ gem "rspec", ">= 2.0.0"
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ rspec (2.5.0)
13
+ rspec-core (~> 2.5.0)
14
+ rspec-expectations (~> 2.5.0)
15
+ rspec-mocks (~> 2.5.0)
16
+ rspec-core (2.5.1)
17
+ rspec-expectations (2.5.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.5.0)
20
+ shoulda (2.11.3)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ jeweler (~> 1.5.2)
28
+ rcov
29
+ rspec (>= 2.0.0)
30
+ shoulda
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Paweł Placzyński
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,40 @@
1
+ = yaml_adapter
2
+
3
+ A YAML files manager providing basic functionality of ActiveRecord.
4
+
5
+ == Introduction
6
+
7
+ Have you ever thought that YAML is better than XML? Have you ever considered making a database with yaml files? Now you can - the usage is simple as in ActiveRecord.
8
+
9
+ == Usage
10
+
11
+ YAML::Adapter.initiate :directory => 'path_to_directory # sets a path to directory with *.yml files
12
+ YAML::Adapter.entities # returns an array of all objects
13
+
14
+ class Test < YAML::Record
15
+ end
16
+
17
+ test = Test.new
18
+ test.id # SHA1 id of an object
19
+ test.file_name # name of the file that object can be saved to - "#{test.id}.yml"
20
+ test.save! # serialises object to yaml format and saves in directory
21
+ test.destroy! # removes file with an object from directory
22
+ Test.create # creates a new object
23
+ Test.create! # creates a new object and saves it in directory
24
+ Test.all # returns an array of objects - instances of class Test
25
+ Test.find(id) # returns an object with an id
26
+
27
+ == Contributing to yaml_adapter
28
+
29
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
30
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
31
+ * Fork the project
32
+ * Start a feature/bugfix branch
33
+ * Commit and push until you are happy with your contribution
34
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
35
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2011 Paweł Placzyński. See LICENSE for further details.
40
+
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = "yaml_adapter"
15
+ gem.homepage = "http://github.com/placek/yaml_adapter"
16
+ gem.license = "MIT"
17
+ gem.summary = %Q{A YAML files manager providing basic functionality of ActiveRecord.}
18
+ gem.description = %Q{Gem yaml_adapter provides functionality based on ActiveRecord for group of YAML files.}
19
+ gem.email = "placek@ragnarson.com"
20
+ gem.authors = ["Paweł Placzyński"]
21
+ gem.add_development_dependency 'rspec'
22
+ end
23
+ Jeweler::RubygemsDotOrgTasks.new
24
+
25
+ require 'rspec/core/rake_task'
26
+ require 'rake/testtask'
27
+ RSpec::Core::RakeTask.new(:spec) do |spec|
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rspec_opts = ['-cfs']
30
+ end
31
+
32
+ RSpec::Core::RakeTask.new(:rcov_rspec) do |spec|
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ end
43
+
44
+ task :default => :spec
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "yaml_adapter #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,106 @@
1
+ require 'yaml'
2
+ require 'fileutils'
3
+ require 'digest/sha1'
4
+
5
+ module YAML
6
+
7
+ class Adapter
8
+
9
+ @@directory = Dir.open(Dir.pwd)
10
+ @@entities = Array.new
11
+
12
+ def self.initiate options = {}
13
+ @@directory = Dir.open(options[:directory] || Dir.pwd)
14
+ YAML::Adapter.load
15
+ end
16
+
17
+ def self.reload
18
+ YAML::Adapter.load
19
+ end
20
+
21
+ def self.entities
22
+ @@entities
23
+ end
24
+
25
+ def self.directory
26
+ @@directory.clone
27
+ end
28
+
29
+ private
30
+
31
+ def self.load
32
+ @@entities.clear
33
+ @@directory.each do |object|
34
+ if File.file?(object) && File.extname(object) == '.yml'
35
+ file_path = File.expand_path(object)
36
+ @@entities.push YAML.load(File.open(file_path).read)
37
+ end
38
+ end
39
+ @@entities
40
+ end
41
+
42
+ end
43
+
44
+
45
+ class Record
46
+
47
+ def initialize
48
+ @created_at = Time.now.to_f
49
+ @modified_at = @created_at
50
+ end
51
+
52
+ def save!
53
+ @modified_at = Time.now.to_f
54
+ content = self.to_yaml
55
+ file = File.new(self.file_name, 'w')
56
+ file.write(content)
57
+ file.close
58
+ YAML::Adapter.reload
59
+ end
60
+
61
+ def destroy!
62
+ FileUtils.rm(self.file_name)
63
+ YAML::Adapter.entities.delete(self)
64
+ end
65
+
66
+ def id
67
+ Digest::SHA1.hexdigest("#{self.class.name}_#{@created_at}")
68
+ end
69
+
70
+ def file_name
71
+ [self.id, '.yml'].join
72
+ end
73
+
74
+ def created_at
75
+ Time.at(@created_at)
76
+ end
77
+
78
+ def modified_at
79
+ Time.at(@modified_at)
80
+ end
81
+
82
+ def self.create options = {}
83
+ object = self.new
84
+ options.keys.each do |key|
85
+ object.instance_variable_set("@#{key.to_s}", options[key])
86
+ end
87
+ object
88
+ end
89
+
90
+ def self.create! options = {}
91
+ object = self.create(options)
92
+ object.save!
93
+ object
94
+ end
95
+
96
+ def self.all
97
+ YAML::Adapter.entities.find_all { |entity| entity.class == self }
98
+ end
99
+
100
+ def self.find id
101
+ YAML::Adapter.entities.find { |entity| entity.id == id }
102
+ end
103
+
104
+ end
105
+
106
+ end
@@ -0,0 +1,86 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/yaml_adapter.rb')
2
+
3
+ describe YAML::Record do
4
+
5
+ before(:all) do
6
+ class Test < YAML::Record
7
+ end
8
+ class OtherTest < YAML::Record
9
+ end
10
+ end
11
+
12
+ describe "new created object" do
13
+
14
+ before do
15
+ @test = Test.new
16
+ end
17
+
18
+ it "should have timestamps" do
19
+ @test.created_at.should_not be_nil
20
+ @test.modified_at.should_not be_nil
21
+ end
22
+
23
+ it "should have ID" do
24
+ @test.id.should_not be_nil
25
+ @test.id.should == Digest::SHA1.hexdigest("#{@test.class.name}_#{@test.created_at.to_f}")
26
+ end
27
+
28
+ it "should generate proper file name" do
29
+ @test.file_name.should_not be_nil
30
+ @test.file_name.should == @test.id + ".yml"
31
+ end
32
+
33
+ it "should be succesfully saved into a file" do
34
+ @test.save!
35
+ Dir.new(Dir.pwd).to_a.should include(@test.file_name)
36
+ FileUtils.rm Dir.glob('*.yml')
37
+ end
38
+
39
+ it "should be succesfully deleted" do
40
+ @test.save!
41
+ @test.destroy!
42
+ Dir.new(Dir.pwd).to_a.should_not include(@test.file_name)
43
+ end
44
+
45
+ end
46
+
47
+ describe "creation" do
48
+
49
+ it "should generate object with given attributes" do
50
+ object = Test.create :attr1 => 'value1', :attr2 => 'value2'
51
+ object.class.should == Test
52
+ object.instance_variable_get('@attr1').should == 'value1'
53
+ object.instance_variable_get('@attr2').should == 'value2'
54
+ end
55
+
56
+ it "should generate object with given attributes and saves it into a file" do
57
+ object = Test.create! :attr1 => 'value1', :attr2 => 'value2'
58
+ object.class.should == Test
59
+ object.instance_variable_get('@attr1').should == 'value1'
60
+ object.instance_variable_get('@attr2').should == 'value2'
61
+ Dir.new(Dir.pwd).to_a.should include(object.file_name)
62
+ FileUtils.rm Dir.glob('*.yml')
63
+ end
64
+
65
+ end
66
+
67
+ describe "queries" do
68
+
69
+ it "should seperate objects of one class from others" do
70
+ 3.times { Test.create! }
71
+ 4.times { OtherTest.create! }
72
+ Test.all.count.should == 3
73
+ OtherTest.all.count.should == 4
74
+ FileUtils.rm Dir.glob('*.yml')
75
+ end
76
+
77
+ it "should seperate object with specific ID" do
78
+ object = Test.create!
79
+ 3.times { Test.create! }
80
+ Test.find(@test.id).should == @test
81
+ FileUtils.rm Dir.glob('*.yml')
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{yaml_adapter}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Pawe\305\202 Placzy\305\204ski"]
12
+ s.date = %q{2011-04-21}
13
+ s.description = %q{Gem yaml_adapter provides functionality based on ActiveRecord for group of YAML files.}
14
+ s.email = %q{placek@ragnarson.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/yaml_adapter.rb",
28
+ "spec/yaml_adapter_spec.rb",
29
+ "yaml_adapter.gemspec"
30
+ ]
31
+ s.homepage = %q{http://github.com/placek/yaml_adapter}
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.6.2}
35
+ s.summary = %q{A YAML files manager providing basic functionality of ActiveRecord.}
36
+ s.test_files = [
37
+ "spec/yaml_adapter_spec.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
45
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
47
+ s.add_development_dependency(%q<rcov>, [">= 0"])
48
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
49
+ s.add_development_dependency(%q<rspec>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<shoulda>, [">= 0"])
52
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
54
+ s.add_dependency(%q<rcov>, [">= 0"])
55
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
56
+ s.add_dependency(%q<rspec>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<shoulda>, [">= 0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ s.add_dependency(%q<rspec>, [">= 2.0.0"])
64
+ s.add_dependency(%q<rspec>, [">= 0"])
65
+ end
66
+ end
67
+
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml_adapter
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - "Pawe\xC5\x82 Placzy\xC5\x84ski"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-21 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :development
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ name: shoulda
33
+ version_requirements: *id001
34
+ prerelease: false
35
+ - !ruby/object:Gem::Dependency
36
+ type: :development
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 23
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 0
47
+ version: 1.0.0
48
+ name: bundler
49
+ version_requirements: *id002
50
+ prerelease: false
51
+ - !ruby/object:Gem::Dependency
52
+ type: :development
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 1
61
+ - 5
62
+ - 2
63
+ version: 1.5.2
64
+ name: jeweler
65
+ version_requirements: *id003
66
+ prerelease: false
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ name: rcov
79
+ version_requirements: *id004
80
+ prerelease: false
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 15
89
+ segments:
90
+ - 2
91
+ - 0
92
+ - 0
93
+ version: 2.0.0
94
+ name: rspec
95
+ version_requirements: *id005
96
+ prerelease: false
97
+ - !ruby/object:Gem::Dependency
98
+ type: :development
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ name: rspec
109
+ version_requirements: *id006
110
+ prerelease: false
111
+ description: Gem yaml_adapter provides functionality based on ActiveRecord for group of YAML files.
112
+ email: placek@ragnarson.com
113
+ executables: []
114
+
115
+ extensions: []
116
+
117
+ extra_rdoc_files:
118
+ - LICENSE
119
+ - README.rdoc
120
+ files:
121
+ - .document
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE
125
+ - README.rdoc
126
+ - Rakefile
127
+ - VERSION
128
+ - lib/yaml_adapter.rb
129
+ - spec/yaml_adapter_spec.rb
130
+ - yaml_adapter.gemspec
131
+ has_rdoc: true
132
+ homepage: http://github.com/placek/yaml_adapter
133
+ licenses:
134
+ - MIT
135
+ post_install_message:
136
+ rdoc_options: []
137
+
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ hash: 3
146
+ segments:
147
+ - 0
148
+ version: "0"
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ hash: 3
155
+ segments:
156
+ - 0
157
+ version: "0"
158
+ requirements: []
159
+
160
+ rubyforge_project:
161
+ rubygems_version: 1.6.2
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: A YAML files manager providing basic functionality of ActiveRecord.
165
+ test_files:
166
+ - spec/yaml_adapter_spec.rb