yaml_record 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -20,6 +20,8 @@ Or add gem to your Gemfile:
20
20
 
21
21
  # Gemfile
22
22
  gem 'yaml_record'
23
+ # OR if you're using Rails 3.1
24
+ gem 'yaml_record' :git => "git@github.com:Nico-Taing/yaml_record.git", :branch => "rails31"
23
25
 
24
26
  ## Usage ##
25
27
 
@@ -103,6 +105,22 @@ class Submission < YamlRecord::Base
103
105
  end
104
106
  ```
105
107
 
108
+ ## Storage Adapters ##
109
+
110
+ YAMLRecord support pluggable storage adapters that change the storage engine for the YAML data. By default, the adapter used is the
111
+ 'local' store which writes a file (specified by `source` path) to the local system. There are currently two available adapters: `Local` and `Redis`.
112
+
113
+ To configure the adapter, you can simply declare within the object:
114
+
115
+ ```ruby
116
+ class Submission < YamlRecord::Base
117
+ adapter :redis, $redis # Second parameter is the redis client instance
118
+ source "contacts" # stores yaml namespaced as 'yaml_record:contacts' in redis
119
+ end
120
+ ```
121
+
122
+ Feel free to create additional adapters and send them to us via a pull request.
123
+
106
124
  ## Example ##
107
125
 
108
126
  Imagine a simple contact form that accepts a name and email from a user along with a body:
@@ -112,6 +130,9 @@ class Submission < YamlRecord::Base
112
130
  # Declare your properties
113
131
  properties :name, :email, :body
114
132
 
133
+ # Choose your adapter (local by default)
134
+ adapter :local # or :redis
135
+
115
136
  # Declare source file path (config/contact.yml)
116
137
  source Rails.root.join("config/contact")
117
138
  end
@@ -169,6 +190,8 @@ And that's all! Each record will be persisted to the source file for easy access
169
190
 
170
191
  Created at Miso by Nico Taing and Nathan Esquenazi
171
192
 
193
+ Special thanks to [Vaudoc](https://github.com/vaudoc)
194
+
172
195
  Contributors and patches are welcome! Please send a pull request!
173
196
 
174
197
  ## Notes ##
data/lib/yaml_record.rb CHANGED
@@ -7,4 +7,6 @@ require 'yaml'
7
7
 
8
8
  module YamlRecord
9
9
  require File.dirname(__FILE__) + "/yaml_record/base"
10
+ require File.dirname(__FILE__) + "/yaml_record/adapters/redis_store"
11
+ require File.dirname(__FILE__) + "/yaml_record/adapters/local_store"
10
12
  end
@@ -0,0 +1,28 @@
1
+ # YAML Adapter for using a local file store
2
+
3
+ module YamlRecord
4
+ module Adapters
5
+ class LocalStore
6
+
7
+ # Returns YAML File as ruby collection
8
+ #
9
+ # === Example:
10
+ #
11
+ # @adapter.read("foo") => [{...}, {...}]
12
+ #
13
+ def read(source)
14
+ YAML.load_file(source)
15
+ end
16
+
17
+ # Writes ruby collection as YAML File
18
+ #
19
+ # === Example:
20
+ #
21
+ # @adapter.write("foo", [{...}, {...}]) => "<yaml data>"
22
+ #
23
+ def write(source, collection)
24
+ File.open(source, 'w') {|f| f.write(collection.to_yaml) }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,42 @@
1
+ # YAML Adapter for using a redis client store
2
+ # YamlRecord::Adapters::RedisStore.new(@redis)
3
+
4
+ module YamlRecord
5
+ module Adapters
6
+ class RedisStore
7
+ attr_reader :client
8
+
9
+ # client is an instantiated redis client (i.e Redis::Client.new(...))
10
+ def initialize(client)
11
+ raise "Please specify a Redis client" unless client
12
+ @client = client
13
+ end
14
+
15
+ # Returns Redis YML data as ruby collection
16
+ #
17
+ # === Example:
18
+ #
19
+ # @adapter.read("foo") => [{...}, {...}]
20
+ #
21
+ def read(source)
22
+ YAML.load(@client.get(redis_key(source)).to_s)
23
+ end
24
+
25
+ # Writes ruby collection as Redis YML data
26
+ #
27
+ # === Example:
28
+ #
29
+ # @adapter.write("foo", [{...}, {...}]) => "<yaml data>"
30
+ #
31
+ def write(source, collection)
32
+ @client.set(redis_key(source), collection.to_yaml)
33
+ end
34
+
35
+ private
36
+
37
+ def redis_key(source)
38
+ "yaml_record:" + source
39
+ end
40
+ end
41
+ end
42
+ end
@@ -264,7 +264,7 @@ module YamlRecord
264
264
  # Post.all(true) => (...force reload...)
265
265
  #
266
266
  def self.all
267
- raw_items = YAML.load_file(source) || []
267
+ raw_items = self.adapter.read(self.source) || []
268
268
  raw_items.map { |item| self.new(item.merge(:persisted => true)) }
269
269
  end
270
270
 
@@ -327,6 +327,20 @@ module YamlRecord
327
327
  end
328
328
  end
329
329
 
330
+ # Declares or retrieves adapter for Yaml storage
331
+ # Returns an instance of an adapter
332
+ #
333
+ # === Example:
334
+ #
335
+ # class Post < YamlRecord::Base
336
+ # adapter :redis, @redis # => YamlRecord::Adapters::RedisAdapter
337
+ # end
338
+ #
339
+ def self.adapter(kind=nil, *options)
340
+ kind.nil? ? @_adapter_kind ||= :local : @_adapter_kind = kind
341
+ @_adapter ||= eval("YamlRecord::Adapters::#{@_adapter_kind.to_s.capitalize}Store").new(*options)
342
+ end
343
+
330
344
  # Declares source file for YamlRecord class
331
345
  #
332
346
  # === Example:
@@ -362,7 +376,7 @@ module YamlRecord
362
376
  # Post.write_content([{ :foo => "bar"}, { :foo => "baz"}, ...]) # writes to source file
363
377
  #
364
378
  def self.write_contents(raw_data)
365
- File.open(self.source, 'w') {|f| f.write(raw_data.to_yaml) }
379
+ self.adapter.write(self.source, raw_data)
366
380
  @records = nil
367
381
  end
368
382
 
@@ -1,3 +1,3 @@
1
1
  module YamlRecord
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/test/base_test.rb CHANGED
@@ -177,6 +177,29 @@ class BaseTest < Test::Unit::TestCase
177
177
  should("set @fs_not_created is_created field to false"){ assert_false @fs_not_created.is_created }
178
178
  end
179
179
 
180
+ context "for self.adapter method" do
181
+ should("return local adapter") do
182
+ class YamlOtherObject < YamlRecord::Base; end
183
+ assert_kind_of YamlRecord::Adapters::LocalStore, YamlOtherObject.adapter
184
+ end
185
+ should("support explicit local adapter") do
186
+ YamlObject.adapter(:local)
187
+ assert_kind_of YamlRecord::Adapters::LocalStore, YamlObject.adapter
188
+ end
189
+ should("support changing adapter") do
190
+ class YamlRedisObject < YamlRecord::Base; end
191
+ @object = Object.new
192
+ YamlRedisObject.adapter(:redis, @object)
193
+ assert_kind_of YamlRecord::Adapters::RedisStore, YamlRedisObject.adapter
194
+ assert_equal @object, YamlRedisObject.adapter.client
195
+ class YamlOtherObject < YamlRecord::Base; end
196
+ assert_kind_of YamlRecord::Adapters::LocalStore, YamlOtherObject.adapter
197
+ end
198
+ should("not support invalid adapter") do
199
+ assert_raise(NameError) { class YamlFakeObject < YamlRecord::Base; adapter(:fake); end }
200
+ end
201
+ end
202
+
180
203
  context "for set_id!" do
181
204
  setup do
182
205
  @fs_no_id = YamlObject.new(@attr)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_record
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nico Taing
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-07-15 00:00:00 -07:00
19
+ date: 2011-08-18 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -81,6 +81,8 @@ files:
81
81
  - README.md
82
82
  - Rakefile
83
83
  - lib/yaml_record.rb
84
+ - lib/yaml_record/adapters/local_store.rb
85
+ - lib/yaml_record/adapters/redis_store.rb
84
86
  - lib/yaml_record/base.rb
85
87
  - lib/yaml_record/version.rb
86
88
  - test/base_test.rb