active_yaml 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +74 -0
- data/Rakefile +1 -0
- data/active_yaml.gemspec +23 -0
- data/lib/active_yaml.rb +126 -0
- data/lib/active_yaml/version.rb +3 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 51109fe33eba19f1a8e90605ba689cea2027e5a1
|
4
|
+
data.tar.gz: aef83b106106e108692b8c3e79fe70aed2204218
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6f673d8082b7d3d44221665c5e8ba0cd8d44b656a1b0f13bead3bc2d47b2bee8941a0293c76e10e1149a1b90afdac9689d22fad37c11e78d46fb2d770a056c2
|
7
|
+
data.tar.gz: a0e793d4b0663ba5c19199d1e1010341c2888e375077ace8aa9d4c963c42037d02ba7f9ecbc2dd4489c4d37a4d5c722fac420e61b16bf3d7465fb286645b3587
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Brunno Gomes
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# ActiveYaml
|
2
|
+
|
3
|
+
A simple key/value storage built on top of YAML::Store with an ActiveRecord like interface
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'active_yaml'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install active_yaml
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
For now, it uses a "db" directory to store the YAML files, so create it first.
|
22
|
+
|
23
|
+
Then just include it in your model classes
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class Post
|
27
|
+
include ActiveYaml
|
28
|
+
|
29
|
+
attr_accessor :title, :body
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
And use it like you use ActiveRecord
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
p = Post.new
|
37
|
+
p.title = "Great post!"
|
38
|
+
p.body = "Lorem ipsum..."
|
39
|
+
p.save
|
40
|
+
|
41
|
+
Post.all # => [#<Post:0x895bb38 @title="Great post!", @body="Lorem ipsum...", @id=1>]
|
42
|
+
|
43
|
+
Post.find(1) # => #<Post:0x954bc69 @title="Great post!", @body="Lorem ipsum...", @id=1>
|
44
|
+
```
|
45
|
+
|
46
|
+
You can use "where" to make "queries" on more than one attribute:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Post.where(author: 'Brunno', visibility: 'public')
|
50
|
+
# => [#<Post:0x895bb38 @author="Brunno", @visibility="public", @id=1>, #<Post:0x457pa36 @author="Brunno", @visibility="public", @id=2>]
|
51
|
+
```
|
52
|
+
|
53
|
+
## Why?
|
54
|
+
|
55
|
+
Right now I'm in the middle of some "reinventing the wheels using standard library" thing and this was a really obvious one.
|
56
|
+
|
57
|
+
But it is actually useful for projects that don't deal with problems like concurrency, overload etc and for those who like to access the data a lot (it's really handy when it's in .yml files).
|
58
|
+
|
59
|
+
So what I mean with this is, please don't use it to build the next Twitter and then bitch about Ruby.
|
60
|
+
|
61
|
+
## TODO
|
62
|
+
|
63
|
+
1. Add tests (ha!)
|
64
|
+
2. Mirror more methods from ActiveRecord::Base
|
65
|
+
3. Maybe try to instantiate relationship objects automagically based on id.
|
66
|
+
4. You tell me.
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
1. Fork it
|
71
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
72
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
73
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
74
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/active_yaml.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_yaml/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_yaml"
|
8
|
+
spec.version = ActiveYaml::VERSION
|
9
|
+
spec.authors = ["Brunno Gomes"]
|
10
|
+
spec.email = ["brunnolgp@gmail.com"]
|
11
|
+
spec.description = %q{A simple key/value storage built on top of YAML::Store with an ActiveRecord like interface}
|
12
|
+
spec.summary = %q{ActiveRecord like YAML key/value store}
|
13
|
+
spec.homepage = "https://github.com/brunnogomes/active_yaml"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/active_yaml.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
require "active_yaml/version"
|
2
|
+
require 'yaml/store'
|
3
|
+
|
4
|
+
module ActiveYaml
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def store
|
11
|
+
@store ||= YAML::Store.new("db/#{self.to_s.downcase}.yml")
|
12
|
+
end
|
13
|
+
|
14
|
+
def add(record)
|
15
|
+
store.transaction do
|
16
|
+
id = (store.roots.max || 0) + 1
|
17
|
+
record.send(:id=, id)
|
18
|
+
store[id] = record
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def update(record)
|
23
|
+
store.transaction do
|
24
|
+
store[record.id] = record
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def destroy(id)
|
29
|
+
store.transaction do
|
30
|
+
store.delete(id)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def first
|
35
|
+
store.transaction(true) do
|
36
|
+
id = store.roots.min
|
37
|
+
store[id]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def last
|
42
|
+
store.transaction(true) do
|
43
|
+
size = store.roots.size
|
44
|
+
store[size]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def all
|
49
|
+
records = []
|
50
|
+
store.transaction(true) do
|
51
|
+
store.roots.each do |id|
|
52
|
+
records << store[id]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
records
|
56
|
+
end
|
57
|
+
|
58
|
+
def count
|
59
|
+
store.transaction(true) do
|
60
|
+
store.roots.size
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def any?
|
65
|
+
store.transaction(true) do
|
66
|
+
store.roots.any?
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def empty?
|
71
|
+
store.transaction(true) do
|
72
|
+
store.roots.empty?
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def find(id)
|
77
|
+
store.transaction(true) do
|
78
|
+
store[id]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def find_by(condition)
|
83
|
+
key, value = condition.first
|
84
|
+
all.each do |record|
|
85
|
+
return record if record[key] == value
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def where(conditions)
|
90
|
+
found = false
|
91
|
+
matches = []
|
92
|
+
all.each do |record|
|
93
|
+
conditions.each do |condition|
|
94
|
+
key, value = condition
|
95
|
+
found = (record[key] == value)
|
96
|
+
end
|
97
|
+
matches << record if found
|
98
|
+
end
|
99
|
+
matches
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
attr_reader :id
|
104
|
+
|
105
|
+
def save
|
106
|
+
if new_record?
|
107
|
+
self.class.add(self)
|
108
|
+
else
|
109
|
+
self.class.update(self)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def destroy
|
114
|
+
self.class.destroy(self.id)
|
115
|
+
end
|
116
|
+
|
117
|
+
def new_record?
|
118
|
+
id.nil?
|
119
|
+
end
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
def id=(new_id)
|
124
|
+
@id = new_id
|
125
|
+
end
|
126
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_yaml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brunno Gomes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A simple key/value storage built on top of YAML::Store with an ActiveRecord
|
42
|
+
like interface
|
43
|
+
email:
|
44
|
+
- brunnolgp@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- active_yaml.gemspec
|
55
|
+
- lib/active_yaml.rb
|
56
|
+
- lib/active_yaml/version.rb
|
57
|
+
homepage: https://github.com/brunnogomes/active_yaml
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.0.0.rc.2
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: ActiveRecord like YAML key/value store
|
81
|
+
test_files: []
|