yamlet 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c14bd6ad734910c22346d831a4b48e2748696d27
4
+ data.tar.gz: 1d732ca97ab906be3746e00523e6f1659c8855be
5
+ SHA512:
6
+ metadata.gz: 98ccb512e83c59fbfae23b12a29c8e6c951bc1873aee373d36cdf59dd1e63ad0cd23de2719d7922bae4bd649b8b720482f9871cd5fca685c9dcaa3ca2d8cca15
7
+ data.tar.gz: 060becb5ec15352c24fdb180633808339cefc5e0d6e03f2e148b561e9b72de33ce1d6952b72bef269ca7c6c537743a58f6f81ed2ed62297cc5feffe472ac7208
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yamlet.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # Yamlet
2
+
3
+ Yamlet is a tiny library (< 100 lines of code) which injects CRUD
4
+ functionalities to your Plain Old Ruby Objects and store data on a YAML file using
5
+ [YAML::Store].
6
+
7
+ Perfect for creating small applications **(for demo/prototyping purposes)**
8
+ where you only need a single YAML file to store everything and removes the
9
+ constraint of setting up a database.
10
+
11
+ Installation
12
+ ------------
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'yamlet'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install yamlet
27
+
28
+ Usage
29
+ --------
30
+
31
+ ### Configuration
32
+
33
+ First, create a YAML file somewhere then tell Yamlet where to find that file which we'll be using for storing data.
34
+
35
+ ```ruby
36
+ Yamlet.repository_file = "/path/to/repository.yml"
37
+ ```
38
+
39
+ **NOTE: A `RepositoryNotFound` error will be raised when Yamlet can't locate the YAML file.**
40
+
41
+ ### Using Yamlet with Classes
42
+
43
+ ```ruby
44
+ class User
45
+ include Yamlet.model
46
+
47
+ #...
48
+ end
49
+ ```
50
+
51
+ After including `Yamlet.model` in your class, notice that the YAML file will
52
+ automatically be updated.
53
+
54
+ ```yaml
55
+ # /path/to/repository.yml
56
+
57
+ ---
58
+ user: []
59
+ ```
60
+
61
+ ### Methods
62
+
63
+
64
+ #### `.all`
65
+
66
+
67
+ ```ruby
68
+ User.all #=> []
69
+ User.create(name: "Grumpy Kid")
70
+
71
+ User.all #=> [{"id"=>1, "name"=>"Grumpy Kid"}]
72
+ ```
73
+
74
+ #### `.find`
75
+
76
+ ```ruby
77
+ User.find(1) #=> {"id"=>1, "name"=>"Grumpy Kid"}
78
+ ```
79
+
80
+ #### `.create`
81
+
82
+ ```ruby
83
+ User.create(name: "Grumpy Kid")
84
+ #=> {"id"=>1, "name"=>"Grumpy Kid"}
85
+ ```
86
+
87
+ #### `.update`
88
+
89
+ ```ruby
90
+ User.update(1, name: "Grumpy Dad")
91
+ #=> {"id"=>1, "name"=>"Grumpy Dad"}
92
+ ```
93
+
94
+ #### `.destroy`
95
+
96
+ ```ruby
97
+ User.destroy(1)
98
+
99
+ User.all #=> []
100
+ ```
101
+
102
+ #### `.destroy_all`
103
+
104
+ ```ruby
105
+ 5.times { |i| User.create(name: "Grumpy #{i}") }
106
+
107
+ User.destroy_all #=> []
108
+ ```
109
+
110
+ ## Contributing
111
+
112
+ 1. Fork it ( https://github.com/[my-github-username]/yamlet/fork )
113
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
114
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
115
+ 4. Push to the branch (`git push origin my-new-feature`)
116
+ 5. Create a new Pull Request
117
+
118
+ [YAML::Store]: http://ruby-doc.org/stdlib-2.1.0/libdoc/yaml/rdoc/YAML/Store.html
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module Yamlet
2
+ VERSION = "0.1.0"
3
+ end
data/lib/yamlet.rb ADDED
@@ -0,0 +1,92 @@
1
+ require "yaml/store"
2
+ require "yamlet/version"
3
+
4
+ module Yamlet
5
+ RepositoryNotFound = Class.new(StandardError)
6
+
7
+ @@repository_file = nil
8
+
9
+ def self.repository_file
10
+ @@repository_file
11
+ end
12
+
13
+ def self.repository_file=(file)
14
+ @@repository_file = file
15
+ end
16
+
17
+ module Repository
18
+
19
+ def self.store
20
+ @store ||= YAML::Store.new Yamlet.repository_file
21
+ rescue
22
+ raise Yamlet::RepositoryNotFound.new("YAML file can't be located")
23
+ end
24
+
25
+ end
26
+
27
+ module Model
28
+ Error = Class.new(StandardError)
29
+
30
+ def self.included(klass)
31
+ klass.define_singleton_method :resource_name, -> {
32
+ name = klass.to_s.downcase
33
+ name.gsub!("::", "_") if name.include? "::"
34
+
35
+ return name
36
+ }
37
+
38
+ klass.define_singleton_method :store, -> { Yamlet::Repository.store }
39
+
40
+ klass.store.transaction { klass.store[klass.resource_name] ||= [] }
41
+
42
+ klass.define_singleton_method :all, -> {
43
+ store.transaction { store[resource_name] }
44
+ }
45
+
46
+ klass.define_singleton_method :find, ->(id) {
47
+ store.transaction { store[resource_name].select { |s| s["id"] == id }[0] }
48
+ }
49
+
50
+ klass.define_singleton_method :create, ->(attributes = {}) {
51
+ # Auto increment ids
52
+ ids = all.empty? ? [0] : all.map { |s| s["id"] }
53
+ id = ids[-1].zero? ? 1 : ids[-1].succ
54
+
55
+ if attributes.empty?
56
+ store.transaction { store[resource_name].push({ "id" => id }) }
57
+ else
58
+ store.transaction {
59
+ store[resource_name].push(
60
+ {}.tap do |hash|
61
+ hash["id"] = id
62
+ attributes.each_pair { |k, v| hash[k.to_s] = v }
63
+ end
64
+ )
65
+ }
66
+ end
67
+ }
68
+
69
+ klass.define_singleton_method :update, ->(id, attributes) {
70
+ store.transaction {
71
+ store[resource_name].select { |s| s["id"] == id }[0].tap do |record|
72
+ attributes.each_pair { |k, v| record[k.to_s] = v } unless record.nil?
73
+ end
74
+ }
75
+ }
76
+
77
+ klass.define_singleton_method :destroy, ->(id) {
78
+ store.transaction { store[resource_name].delete_if { |h| h["id"] == id } }
79
+ }
80
+
81
+ klass.define_singleton_method :destroy_all, -> {
82
+ store.transaction { store[resource_name] = [] }
83
+ }
84
+
85
+ end
86
+ end
87
+
88
+ def self.model
89
+ Yamlet::Model
90
+ end
91
+
92
+ end
data/yamlet.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 'yamlet/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "yamlet"
8
+ spec.version = Yamlet::VERSION
9
+ spec.authors = ["Robbie Marcelo"]
10
+ spec.email = ["rbmrclo@hotmail.com"]
11
+ spec.licenses = ['MIT']
12
+
13
+ spec.summary = %q{Inject CRUD functionalities to your Plain Old Ruby Objects and store data in a YAML file}
14
+ spec.description = %q{A small library which injects CRUD functionalities to your Plain Old Ruby Objects and store data in a YAML file}
15
+ spec.homepage = "https://github.com/rbmrclo/yamlet"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.9"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", '~> 3.0'
23
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamlet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robbie Marcelo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-05 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: A small library which injects CRUD functionalities to your Plain Old
56
+ Ruby Objects and store data in a YAML file
57
+ email:
58
+ - rbmrclo@hotmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - lib/yamlet.rb
69
+ - lib/yamlet/version.rb
70
+ - yamlet.gemspec
71
+ homepage: https://github.com/rbmrclo/yamlet
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.4.6
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Inject CRUD functionalities to your Plain Old Ruby Objects and store data
95
+ in a YAML file
96
+ test_files: []