rbdb 0.2.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c13c3bc8a03de85db353677f6ee82e23a6f68c12
4
+ data.tar.gz: b16ebb27dab5f231d780ff9783026a3dbac766fe
5
+ SHA512:
6
+ metadata.gz: c7efb12dae8e8c1139d800253b85ce5ba56541c21db3aee6a1a46e55cd6ba1e12612843c10c0ddd3502b3f79ecdc1627c2dcf37e93fec4dfd9e12ca7f1b5a954
7
+ data.tar.gz: 7c42fb1c4bcce49d7cb5025b07c189481e5b29132834e09574bba62419852490d15e154fff9f343b7c37b11162c3ecbecaab7030fed6b33f7919311dce555df8
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rbdb.gemspec
4
+ gemspec
@@ -0,0 +1,39 @@
1
+ # Rbdb
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rbdb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rbdb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rbdb
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/rbdb/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,40 @@
1
+ require "rbdb/table"
2
+ require "rbdb/util"
3
+ require "rbdb/version"
4
+ require "rbdb/db"
5
+
6
+ module Rbdb
7
+ @@dbs = Hash.new
8
+
9
+ class << self
10
+ def create_db(name, options = {})
11
+ name = name.to_s
12
+ Rbdb::Util.check_existence_and_force(@@dbs.keys, name, options) do
13
+ @@dbs[name] = DB.new(name)
14
+ @@dbs[name]
15
+ end
16
+ end
17
+
18
+ def add_db(db, options = {})
19
+ Rbdb::Util.check_existence_and_force(@@dbs.keys, db.name, options) { @@dbs[db.name] = db }
20
+ end
21
+
22
+ def databases
23
+ @@dbs
24
+ end
25
+
26
+ def find_db(name)
27
+ name = name.to_s
28
+ @@dbs[name] || nil
29
+ end
30
+
31
+ def delete_db(name)
32
+ name = name.to_s
33
+ @@dbs[name] = nil
34
+ end
35
+
36
+ def flush(options = {})
37
+ @@dbs = Hash.new if options[:test] || options[:debug]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ module Rbdb
2
+ class DB
3
+ attr_reader :name, :tables
4
+
5
+ def initialize(name, options = {})
6
+ @name = name.to_s
7
+ @options = options
8
+ @tables = Hash.new
9
+ end
10
+
11
+ def table_count
12
+ @tables.length
13
+ end
14
+
15
+ def create_table(name, options = {})
16
+ Rbdb::Util.check_existence_and_force(@tables.keys, name, options) do
17
+ table = Rbdb::Table.new(name)
18
+ @tables[name] = table
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,109 @@
1
+ module Rbdb
2
+ class ReservedKeyError < StandardError; end
3
+
4
+ class Table
5
+ ALLOWED_TYPES = [:string, :integer, :float, :boolean]
6
+
7
+ attr_reader :name, :options, :fields, :records
8
+
9
+ def initialize(name, options = {})
10
+ @name = name
11
+ @options = options
12
+ @fields = {}
13
+ @records = {}
14
+ end
15
+
16
+ def fields=(fields)
17
+ check_field_types(fields)
18
+ @fields = fields
19
+ end
20
+
21
+ def insert(record)
22
+ check_record(record) do
23
+ id = records.length + 1
24
+ @records[id.to_s] = record.merge!({:id => id})
25
+ return record.merge({:id => id})
26
+ end
27
+ end
28
+
29
+ def find_by(kv_pair)
30
+ return find_by_id(kv_pair.values.first) if kv_pair.keys.first.to_s == 'id'
31
+ find(kv_pair) do |record|
32
+ return record
33
+ end
34
+ end
35
+
36
+ def find_by_id(id)
37
+ return @records["#{id}"]
38
+ end
39
+
40
+ def find_all(kv_pair)
41
+ return [find_by_id(kv_pair.values.first)] if kv_pair.keys.first == 'id'
42
+ found = []
43
+ find(kv_pair) do |record|
44
+ found << record
45
+ end
46
+ found
47
+ end
48
+
49
+ def update(record)
50
+ id = record[:id]
51
+ if find_by_id(record[:id])
52
+ record.delete(:id)
53
+ check_record(record) do
54
+ @records["#{id}"] = record.merge!({:id => id})
55
+ return record
56
+ end
57
+ end
58
+ end
59
+
60
+ def delete(record_id)
61
+ @records.delete("#{record_id}")
62
+ end
63
+
64
+ private
65
+
66
+ def find(kv_pair, &block)
67
+ @records.each do |key, v|
68
+ yield v if v[kv_pair.keys.first] == kv_pair.values.first
69
+ end
70
+ end
71
+
72
+ def check_field_types(fields)
73
+ fields.each do |k, v|
74
+ raise "invalid type: #{v}" unless ALLOWED_TYPES.include?(v.to_sym)
75
+ raise ReservedKeyError, "id cannot be used as a field key" if k.to_s == 'id'
76
+ end
77
+ end
78
+
79
+ def check_record(record, &block)
80
+ raise "invalid record: #{record}" unless record.keys.length == @fields.keys.length
81
+ new_record = {}
82
+
83
+ class_check_block = -> (klass, record, new_record, k) do
84
+ if record[k].class == klass
85
+ new_record[k] = record[k]
86
+ else
87
+ raise "Invalid record: #{record}"
88
+ end
89
+ end
90
+
91
+ fields.each do |k, v|
92
+ case v
93
+ when :string then class_check_block.call(String, record, new_record, k)
94
+ when :integer then class_check_block.call(Fixnum, record, new_record, k)
95
+ when :float then class_check_block.call(Float, record, new_record, k)
96
+ when :boolean
97
+ if k.class == TrueClass
98
+ class_check_block(TrueClass, record, new_record, k)
99
+ elsif k.class == FalseClass
100
+ class_check_block(FalseClass, record, new_record, k)
101
+ end
102
+ else
103
+ raise "Invalid record: #{record}"
104
+ end
105
+ end
106
+ block.call
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,14 @@
1
+ module Rbdb
2
+ module Util
3
+ class << self
4
+ def check_existence_and_force(collection, name, options, &block)
5
+ raise ArgumentError, "method requires a block" unless block_given?
6
+ if collection.include?(name) && !options[:force]
7
+ raise 'Entity already exists. Use the force option to overwrite'
8
+ else
9
+ block.call
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module Rbdb
2
+ VERSION = "0.2.0"
3
+ end
Binary file
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rbdb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rbdb"
8
+ spec.version = Rbdb::VERSION
9
+ spec.authors = ["dibenso"]
10
+ spec.email = ["dibenso93@gmail.com"]
11
+
12
+ spec.summary = %q{Small ruby library for persistent data}
13
+ spec.description = %q{Small ruby library for persistent data}
14
+ spec.homepage = %q{https://github.com/dibenso/rbdb}
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").delete_if { |file| file =~ /bin/ }.reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.files += Dir["lib/**/*.rb"]
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.9"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - dibenso
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-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.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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Small ruby library for persistent data
56
+ email:
57
+ - dibenso93@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - lib/rbdb.rb
69
+ - lib/rbdb/db.rb
70
+ - lib/rbdb/table.rb
71
+ - lib/rbdb/util.rb
72
+ - lib/rbdb/version.rb
73
+ - rbdb-0.1.0.gem
74
+ - rbdb.gemspec
75
+ homepage: https://github.com/dibenso/rbdb
76
+ licenses: []
77
+ metadata:
78
+ allowed_push_host: https://rubygems.org
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.5
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Small ruby library for persistent data
99
+ test_files: []