yaml_adapter 0.0.0 → 0.0.1

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/README.rdoc CHANGED
@@ -8,21 +8,21 @@ Have you ever thought that YAML is better than XML? Have you ever considered mak
8
8
 
9
9
  == Usage
10
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
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
26
 
27
27
  == Contributing to yaml_adapter
28
28
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
data/lib/yaml_adapter.rb CHANGED
@@ -44,18 +44,24 @@ module YAML
44
44
 
45
45
  class Record
46
46
 
47
+ @@before_save_methods = Array.new
48
+ @@after_save_methods = Array.new
49
+
47
50
  def initialize
48
51
  @created_at = Time.now.to_f
49
52
  @modified_at = @created_at
50
53
  end
51
54
 
52
55
  def save!
56
+ @@before_save_methods.each { |m| self.send(m) }
53
57
  @modified_at = Time.now.to_f
54
58
  content = self.to_yaml
55
59
  file = File.new(self.file_name, 'w')
56
60
  file.write(content)
57
61
  file.close
62
+ @@after_save_methods.each { |m| self.send(m) }
58
63
  YAML::Adapter.reload
64
+ self
59
65
  end
60
66
 
61
67
  def destroy!
@@ -101,6 +107,14 @@ module YAML
101
107
  YAML::Adapter.entities.find { |entity| entity.id == id }
102
108
  end
103
109
 
110
+ def self.before_save *methods
111
+ @@before_save_methods = methods if methods.all? { |el| el.class == Symbol }
112
+ end
113
+
114
+ def self.after_save *methods
115
+ @@after_save_methods = methods if methods.all? { |el| el.class == Symbol }
116
+ end
117
+
104
118
  end
105
119
 
106
120
  end
@@ -83,4 +83,40 @@ describe YAML::Record do
83
83
 
84
84
  end
85
85
 
86
+ describe "filters" do
87
+
88
+ before do
89
+ class SomeTest < YAML::Record
90
+ before_save :check
91
+ attr_accessor :field
92
+ def check
93
+ raise "There is nothing in :field" if self.field.nil?
94
+ end
95
+ end
96
+ class SomeOtherTest < YAML::Record
97
+ after_save :check
98
+ attr_accessor :field
99
+ def check
100
+ raise "There is nothing in :field" if self.field.nil?
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ it "should run before saving" do
107
+ @test = SomeTest.new
108
+ modfication_stamp = @test.modified_at
109
+ lambda {@test.save!}.should raise_error
110
+ @test.modified_at.should == modfication_stamp
111
+ end
112
+
113
+ it "should run after saving" do
114
+ @test = SomeOtherTest.new
115
+ modfication_stamp = @test.modified_at
116
+ lambda {@test.save!}.should raise_error
117
+ @test.modified_at.should != modfication_stamp
118
+ end
119
+
120
+ end
121
+
86
122
  end
data/yaml_adapter.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{yaml_adapter}
8
- s.version = "0.0.0"
8
+ s.version = "0.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Pawe\305\202 Placzy\305\204ski"]
12
- s.date = %q{2011-04-21}
12
+ s.date = %q{2011-04-22}
13
13
  s.description = %q{Gem yaml_adapter provides functionality based on ActiveRecord for group of YAML files.}
14
14
  s.email = %q{placek@ragnarson.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_adapter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 0
10
- version: 0.0.0
9
+ - 1
10
+ version: 0.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Pawe\xC5\x82 Placzy\xC5\x84ski"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-21 00:00:00 +02:00
18
+ date: 2011-04-22 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency