hash_db 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ - ruby-head
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ script: bundle exec rspec spec --profile
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # HashDB
1
+ # HashDB [![Build Status](https://travis-ci.org/utkarshkukreti/hash_db.png)](https://travis-ci.org/utkarshkukreti/hash_db)
2
2
 
3
3
  HashDB is a minimal, in-memory, ActiveRecord like database library,
4
4
  backed by a Ruby Hash.
@@ -23,6 +23,26 @@ Or install it yourself as:
23
23
  $ bundle install
24
24
  $ rake install
25
25
 
26
+ ## Quickstart
27
+
28
+ (Complete usage instructions are below.)
29
+
30
+ require 'hash_db'
31
+
32
+ class Score
33
+ include HashDB::Model
34
+ keys :name, :points
35
+ end
36
+
37
+ score = Score.new name: "A", points: 10
38
+ score.save #= score.id == 1
39
+ Score.create name: "B", points: 11 #= id == 2
40
+ Score.create name: "C", points: 6 #= id == 3
41
+
42
+ Score.where(name: "B", points: 11).count #= 1
43
+ Score.where([:points, :>, 4], [:name, :=~, /a|b/i]).count #= 2
44
+ Score.find_by(:points, :>, 8).id #= 1
45
+
26
46
  ## Usage
27
47
 
28
48
  First, require HashDB.
@@ -72,7 +92,7 @@ on the class.
72
92
  HashDB currently has just 2 query methods, `.where`, which can filter objects
73
93
  using key(s) = value(s), and also using custom method call, like `:<` and `:>`.
74
94
 
75
- Let's [HN](http://news.ycombinator.com) as an example. :)
95
+ Let's take [HN](http://news.ycombinator.com) as an example. :)
76
96
 
77
97
  class Item
78
98
  include HashDB::Model
data/hash_db.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.description = "A minimal, in-memory, ActiveRecord like database, " +
12
12
  "backed by a Ruby Hash."
13
13
  gem.summary = gem.description
14
- gem.homepage = ""
14
+ gem.homepage = "http://utkarshkukreti.github.com/hash_db"
15
15
  gem.license = "MIT"
16
16
 
17
17
  gem.files = `git ls-files`.split($/)
@@ -6,6 +6,7 @@ module HashDB
6
6
  klass.extend ClassMethods
7
7
  klass.all = {}
8
8
  klass.keys :id
9
+ klass.primary_key :id
9
10
  end
10
11
 
11
12
  def initialize(args = {})
@@ -54,6 +55,14 @@ module HashDB
54
55
  object.save
55
56
  end
56
57
  end
58
+
59
+ def primary_key(key = nil)
60
+ if key
61
+ @primary_key = key
62
+ else
63
+ @primary_key
64
+ end
65
+ end
57
66
  end
58
67
  end
59
68
  end
@@ -24,6 +24,11 @@ module HashDB
24
24
  # Not efficient, and I know it.
25
25
  where(*args).first
26
26
  end
27
+
28
+ def find(value)
29
+ find_by @primary_key => value
30
+ end
31
+ alias_method :[], :find
27
32
  end
28
33
  end
29
34
  end
@@ -1,3 +1,3 @@
1
1
  module HashDB
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -27,6 +27,17 @@ describe HashDB::Model do
27
27
  end
28
28
  end
29
29
 
30
+ context ".primary_key" do
31
+ it "should have :id as primary key by default" do
32
+ model.primary_key.should eq :id
33
+ end
34
+
35
+ it "should save primary key" do
36
+ model.primary_key :name
37
+ model.primary_key.should eq :name
38
+ end
39
+ end
40
+
30
41
  context ".new" do
31
42
  it "should assign passed args into keys" do
32
43
  model.keys :integer, :string
@@ -42,5 +42,19 @@ describe HashDB::Model do
42
42
  model.find_by(:integer, :<, 10).should eq nil
43
43
  end
44
44
  end
45
+
46
+ context ".find" do
47
+ it "should delegate to find_by with key = model.primary_key" do
48
+ model.should_receive(:find_by).with(name: "A").and_return(:works)
49
+ model.primary_key :name
50
+ model.find("A").should eq :works
51
+ end
52
+ end
53
+
54
+ context ".[]" do
55
+ it "should be an alias to .find" do
56
+ model.method(:[]).should eq model.method(:find)
57
+ end
58
+ end
45
59
  end
46
60
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -68,6 +68,7 @@ extra_rdoc_files: []
68
68
  files:
69
69
  - .gitignore
70
70
  - .rspec
71
+ - .travis.yml
71
72
  - Gemfile
72
73
  - Guardfile
73
74
  - LICENSE.txt
@@ -84,7 +85,7 @@ files:
84
85
  - spec/hash_db/model/query_spec.rb
85
86
  - spec/hash_db_spec.rb
86
87
  - spec/spec_helper.rb
87
- homepage: ''
88
+ homepage: http://utkarshkukreti.github.com/hash_db
88
89
  licenses:
89
90
  - MIT
90
91
  post_install_message:
@@ -99,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
99
100
  version: '0'
100
101
  segments:
101
102
  - 0
102
- hash: 4016006914544296771
103
+ hash: -3250930581944786811
103
104
  required_rubygems_version: !ruby/object:Gem::Requirement
104
105
  none: false
105
106
  requirements:
@@ -108,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
109
  version: '0'
109
110
  segments:
110
111
  - 0
111
- hash: 4016006914544296771
112
+ hash: -3250930581944786811
112
113
  requirements: []
113
114
  rubyforge_project:
114
115
  rubygems_version: 1.8.24