table_store 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in table-store.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'rake'
8
+ gem 'pry'
9
+ gem 'rspec'
10
+ gem 'rspec-given'
11
+ end
12
+
13
+ group :development do
14
+ gem 'guard'
15
+ gem 'guard-rspec'
16
+ gem 'rb-readline'
17
+
18
+ gem 'rb-inotify', :require => false
19
+ gem 'rb-fsevent', :require => false
20
+ gem 'rb-fchange', :require => false
21
+ end
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 rw
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,52 @@
1
+ # TableStore
2
+
3
+ TableStore is an in-memory relational data store. Create tables, and references between them, as if you were using a regular database. By using lazy evaluation, references to other tables will always show the latest values.
4
+
5
+ Manipulating data along different dimensions is useful, but it's problematic to keep your data structure up-to-date. With a language like C, pointers make this trivial. In Ruby, even though we don't have pointers, we can use metaprogramming to defer evaluation until the time a value is accessed. It's an interesting use of metaprogramming: look at the `lib/lazy` class to see a BasicObject used as a lazy wrapper around values.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'table_store'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install table_store
20
+
21
+ ## Usage
22
+
23
+ Create two tables:
24
+
25
+ require 'table_store'
26
+
27
+ things = TableStore::Table.new
28
+ colors = TableStore::Table.new
29
+
30
+ Create an object that references one of those tables. Here, the `authors` table is referenced twice:
31
+
32
+ stop_sign = {:color => colors['red']}
33
+ things['stop_sign'] = stop_sign
34
+
35
+ Because lookup is lazy under the hood, this returns whatever is currently at `colors[:color]`:
36
+
37
+ stop_sign[:color]
38
+ => nil
39
+
40
+ When changing the value of `colors['red']`, `stop_sign` sees the change:
41
+
42
+ colors['red'] = 'a bright color'
43
+ stop_sign[:color]
44
+ => 'a bright color'
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/lazy.rb ADDED
@@ -0,0 +1,18 @@
1
+ # heavily modified from original source:
2
+ # http://www.innig.net/software/ruby/closures-in-ruby
3
+
4
+ # changes: uses BasicObject so that #send gets passed to the reference
5
+
6
+ class Lazy < BasicObject
7
+ def initialize(&ref)
8
+ @__lazy_reference = ref
9
+ end
10
+
11
+ def send(method, *args, &block)
12
+ @__lazy_reference.call.send(method, *args, &block)
13
+ end
14
+
15
+ def method_missing(method, *args, &block)
16
+ @__lazy_reference.call.send(method, *args, &block)
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ module TableStore
2
+ class Table < Hash
3
+ class DuplicateKeyError < RuntimeError; end
4
+
5
+ def []=(key, value)
6
+ super(key, value)
7
+
8
+ resort_keys!
9
+
10
+ value
11
+ end
12
+
13
+ def [](key)
14
+ Lazy.new { super(key) }
15
+ end
16
+
17
+ def keys
18
+ @sorted_keys ||= super.sort
19
+ end
20
+
21
+ def resort_keys!
22
+ @sorted_keys = nil
23
+ keys
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module TableStore
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'table_store/table'
2
+ require 'table_store/version'
3
+ require 'lazy'
4
+
5
+ module TableStore; end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ require 'lib/table_store/table'
3
+
4
+ describe TableStore::Table do
5
+ Given(:klazz) { TableStore::Table }
6
+ Given(:table) { klazz.new }
7
+
8
+ context 'before first insertion' do
9
+ Then { table.empty?.should be_true }
10
+ end
11
+
12
+ context 'inserting first element' do
13
+ Given { table[0] = :foo }
14
+ Then { table.keys.should == [0] }
15
+ Then { table.empty?.should be_false }
16
+ end
17
+
18
+ context 'inserting many elements' do
19
+ Given { table[0] = :foo }
20
+ Given { table[1] = :bar }
21
+
22
+ Then { table.keys.should == [0, 1] }
23
+
24
+ Given { table[0].send(:should) == :foo }
25
+ Given { table[1].send(:should) == :bar }
26
+
27
+ Then { table.empty?.should be_false }
28
+ end
29
+
30
+ context 'out of order insertion' do
31
+ Given { table[1] = :one }
32
+ Given { table[0] = :zero }
33
+
34
+ Then { table.keys.should == [0, 1] }
35
+ end
36
+
37
+ describe 'lazy relational evaluation' do
38
+ Given(:parent_key) { 0 }
39
+ Given(:parent) { {:child => table[1]} }
40
+ Given(:child_key) { 1 }
41
+ Given(:child) { Object.new }
42
+
43
+ before { table[parent_key] = parent }
44
+
45
+ it 'gets current value at the reference' do
46
+ table[parent_key][:child].send(:should) == nil
47
+
48
+ table[child_key] = child
49
+
50
+ table[parent_key][:child].send(:should) == child
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+ require 'lib/table_store'
3
+
4
+ describe TableStore do
5
+ class Book < Hash; end
6
+ class Author < String; end
7
+
8
+ context 'multiple tables referencing each other' do
9
+ Given(:books) { TableStore::Table.new }
10
+ Given(:authors) { TableStore::Table.new }
11
+
12
+ context 'with one reference' do
13
+ Given(:book) { Book.new }
14
+ Given(:author) { Author.new('Suess') }
15
+ before { authors[0] = author }
16
+ before { book['author'] = authors[0] }
17
+
18
+ Then { book['author'].send(:should) == author }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ $LOAD_PATH << '.'
2
+ require 'rspec-given'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # Require this file using `require "spec_helper"` to ensure that it is only
7
+ # loaded once.
8
+ #
9
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'table_store/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "table_store"
8
+ gem.version = TableStore::VERSION
9
+ gem.authors = ["rw"]
10
+ gem.email = ["me@rwinslow.com"]
11
+ gem.description = %q{Tabular in-memory data structure.}
12
+ gem.summary = %q{In-memory tables that use sorted hashes to provide relational access.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'rbtree'
21
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - rw
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rbtree
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Tabular in-memory data structure.
31
+ email:
32
+ - me@rwinslow.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - Guardfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - lib/lazy.rb
45
+ - lib/table_store.rb
46
+ - lib/table_store/table.rb
47
+ - lib/table_store/version.rb
48
+ - spec/lib/table_store/table_spec.rb
49
+ - spec/lib/table_store_spec.rb
50
+ - spec/spec_helper.rb
51
+ - table_store.gemspec
52
+ homepage: ''
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ segments:
65
+ - 0
66
+ hash: 263633794864903711
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ segments:
74
+ - 0
75
+ hash: 263633794864903711
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 1.8.25
79
+ signing_key:
80
+ specification_version: 3
81
+ summary: In-memory tables that use sorted hashes to provide relational access.
82
+ test_files:
83
+ - spec/lib/table_store/table_spec.rb
84
+ - spec/lib/table_store_spec.rb
85
+ - spec/spec_helper.rb