humble 0.0.1374296843 → 0.0.1374368890

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a07fd88ec8a4eb23bc2664ecb4e040b9810b7955
4
- data.tar.gz: 2bc6434c1fe15d55d2319d429a952c6b65f23b13
3
+ metadata.gz: e4d776c0dc39127d48e8304daf498aeb28e6cf3b
4
+ data.tar.gz: 410eae8dedd880bd4f69677258c0718a1543a69e
5
5
  SHA512:
6
- metadata.gz: 6d0cf9e612c4f542c55aa47f837de75629ae6da985a4350f6d4412e7acffaf448639eff0490d98a86f58f6e69955e0705b028670bbf1da770ad2568400fc40fc
7
- data.tar.gz: 607bb277179bed53fa228c8b47e46fd51508e3f4e26fa362107ea4a4b3bd8bde0338de85ff431eeba67aba8ab4b7b7c0ffbdaf4e9a2f318979f520e23b479c35
6
+ metadata.gz: f28e890a7ec4cc0c97086584403406166672f1dba3e3ffa7a8654893594c3d482ea219480d62ea27c6f1d46a916f4483bc1a4f0fbf87507500d0ab67b75a1efb
7
+ data.tar.gz: 2f3a253eab67e2f9cfe80970b44c8b2a9dcc3de1fc5158647c75acfacc777d17aedc7137ba80d7b19a1badca2661c3eef092260a09b219fa6cf4217c2e23f7bb
@@ -1,18 +1,45 @@
1
1
  module Humble
2
2
  class Column
3
- def initialize(attributes)
4
- @attributes = attributes
3
+ def initialize(name)
4
+ @column_name = name
5
5
  end
6
6
 
7
- def prepare_insert(item)
8
- return {} if primary_key?
9
- key = @attributes[:name]
10
- value = item.instance_variable_get("@#{key}")
11
- { key.to_sym => value }
7
+ def prepare(item)
8
+ return {} if primary_key? && has_default_value?
9
+ { column_name.to_sym => item.instance_variable_get("@#{column_name}") }
12
10
  end
13
11
 
14
12
  def primary_key?
15
- @attributes[:primary_key]
13
+ false
14
+ end
15
+
16
+ protected
17
+
18
+ attr_reader :column_name
19
+ end
20
+
21
+ class PrimaryKeyColumn < Column
22
+ attr_reader :default
23
+
24
+ def initialize(name, default)
25
+ super(name)
26
+ @default = default
27
+ end
28
+
29
+ def apply(id, entity)
30
+ entity.instance_variable_set("@#{column_name}", id )
31
+ end
32
+
33
+ def destroy(connection, entity)
34
+ connection.where(column_name.to_sym => entity.id).delete
35
+ end
36
+
37
+ def primary_key?
38
+ true
39
+ end
40
+
41
+ def has_default_value?(item)
42
+ @default == item.id
16
43
  end
17
44
  end
18
45
  end
@@ -17,7 +17,7 @@ module Humble
17
17
 
18
18
  def mapping_for(item)
19
19
  @mappings.find do |mapping|
20
- mapping.is_for?(item)
20
+ mapping.matches?(item)
21
21
  end
22
22
  end
23
23
 
@@ -11,17 +11,39 @@ module Humble
11
11
  end
12
12
 
13
13
  def primary_key(name, default: 0)
14
- @columns << Column.new(:name => name, :default => default, :primary_key => true)
14
+ @primary_key = PrimaryKeyColumn.new(name, default)
15
15
  end
16
16
 
17
17
  def add_column(name)
18
- @columns << Column.new(:name => name)
18
+ @columns << Column.new(name)
19
19
  end
20
20
 
21
- def insert(item)
21
+ def persist(connection, item)
22
+ if @primary_key.has_default_value?(item)
23
+ @primary_key.apply(insert(item, connection[@name]) , item)
24
+ else
25
+ update(item, connection[@name])
26
+ end
27
+ end
28
+
29
+ def destroy(connection, entity)
30
+ @primary_key.destroy(connection[@name], entity)
31
+ end
32
+
33
+ private
34
+
35
+ def prepare_statement_for(item)
22
36
  @columns.inject({}) do |result, column|
23
- result.merge(column.prepare_insert(item))
37
+ result.merge(column.prepare(item))
24
38
  end
25
39
  end
40
+
41
+ def insert(item, dataset)
42
+ dataset.insert(prepare_statement_for(item))
43
+ end
44
+
45
+ def update(item, dataset)
46
+ dataset.update(prepare_statement_for(item))
47
+ end
26
48
  end
27
49
  end
@@ -9,13 +9,16 @@ module Humble
9
9
  ResultSet.new(connection[@table.name], mapper)
10
10
  end
11
11
 
12
- def save_using(connection, item)
13
- id = connection[@table.name].insert(@table.insert(item))
14
- item.instance_variable_set('@id', id)
12
+ def save_using(connection, entity)
13
+ @table.persist(connection, entity)
15
14
  end
16
15
 
17
- def is_for?(item)
18
- item == @attributes[:type] || item.is_a?(@attributes[:type])
16
+ def delete_using(connection, entity)
17
+ @table.destroy(connection, entity)
18
+ end
19
+
20
+ def matches?(item)
21
+ self[:type] == item || item.is_a?(self[:type])
19
22
  end
20
23
 
21
24
  def [](key)
@@ -25,8 +28,7 @@ module Humble
25
28
  private
26
29
 
27
30
  def mapper
28
- @attributes[:mapper] || DefaultDataRowMapper.new(self)
31
+ self[:mapper] || DefaultDataRowMapper.new(self)
29
32
  end
30
33
  end
31
-
32
34
  end
@@ -15,7 +15,7 @@ module Humble
15
15
 
16
16
  def include?(item)
17
17
  self.find do |x|
18
- x.id == item.id
18
+ x == item
19
19
  end
20
20
  end
21
21
 
@@ -11,14 +11,18 @@ module Humble
11
11
  end
12
12
  end
13
13
 
14
- def save(item)
15
- mapping_for(item).save_using(create_connection, item)
14
+ def save(entity)
15
+ mapping_for(entity).save_using(create_connection, entity)
16
16
  end
17
17
 
18
18
  def find_all(clazz)
19
19
  mapping_for(clazz).find_all_using(create_connection)
20
20
  end
21
21
 
22
+ def delete(entity)
23
+ mapping_for(entity).delete_using(create_connection, entity)
24
+ end
25
+
22
26
  private
23
27
 
24
28
  attr_reader :connection_factory, :mapper_registry
@@ -27,8 +31,8 @@ module Humble
27
31
  @connection ||= connection_factory.create_connection
28
32
  end
29
33
 
30
- def mapping_for(item)
31
- mapper_registry.mapping_for(item)
34
+ def mapping_for(entity)
35
+ mapper_registry.mapping_for(entity)
32
36
  end
33
37
  end
34
38
  end
@@ -0,0 +1,17 @@
1
+ require "integration_helper"
2
+
3
+ describe "deletion" do
4
+ include_context "orm"
5
+
6
+ context "when deleting a record" do
7
+ before :each do
8
+ connection[:movies].insert(:name => 'mo money')
9
+ movie = session.find_all(Movie).first
10
+ session.delete(movie)
11
+ end
12
+
13
+ it "should remove it from the database" do
14
+ connection[:movies].all.count.should == 0
15
+ end
16
+ end
17
+ end
@@ -5,6 +5,17 @@ class Movie
5
5
  @id = attributes[:id] || -1
6
6
  @name = attributes[:name]
7
7
  end
8
+
9
+ def name=(new_name)
10
+ @name = new_name
11
+ end
12
+
13
+ def ==(other)
14
+ return false unless other
15
+ return false if other.id == -1
16
+ return false if @id == -1
17
+ @id == other.id
18
+ end
8
19
  end
9
20
 
10
21
  class MovieMapping < Humble::DatabaseMapping
@@ -0,0 +1,34 @@
1
+ require "integration_helper"
2
+
3
+ describe "orm" do
4
+ include_context "orm"
5
+
6
+ context "when inserting a new record" do
7
+ let(:movie) { Movie.new(:name => 'oop') }
8
+
9
+ before :each do
10
+ session.begin_transaction do |session|
11
+ session.save(movie)
12
+ end
13
+ end
14
+
15
+ let(:results) { connection[:movies].all }
16
+
17
+ it "should insert the correct number of records" do
18
+ results.count.should == 1
19
+ end
20
+
21
+ it "should insert the record with the a new id" do
22
+ results.first[:id].should_not == -1
23
+ results.first[:id].should > 0
24
+ end
25
+
26
+ it "should insert the name" do
27
+ results.first[:name].should == 'oop'
28
+ end
29
+
30
+ it "should update the new item with the new id" do
31
+ movie.id.should_not == -1
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ require "integration_helper"
2
+
3
+ describe "select items" do
4
+ include_context "orm"
5
+
6
+ context "when fetching all items" do
7
+ before :each do
8
+ @id = connection[:movies].insert(:name => 'monsters inc')
9
+ end
10
+
11
+ let(:results) { session.find_all Movie }
12
+
13
+ it "should return the correct number of movies" do
14
+ results.count.should == 1
15
+ end
16
+
17
+ it "should return each movie with its name" do
18
+ results.first.name.should == 'monsters inc'
19
+ end
20
+
21
+ it "should return instances of the target type" do
22
+ results.first.should be_instance_of(Movie)
23
+ end
24
+
25
+ it "should include the saved movie" do
26
+ results.should include(Movie.new(:id => @id, :name => 'monsters inc'))
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,29 @@
1
+ require "integration_helper"
2
+
3
+ describe "updating a record" do
4
+ include_context "orm"
5
+
6
+ context "when updating a record" do
7
+ let(:movie) { Movie.new(:name => "old name") }
8
+
9
+ before :each do
10
+ session.begin_transaction do |session|
11
+ session.save(movie)
12
+ end
13
+ movie.name="new name"
14
+ session.begin_transaction do |session|
15
+ session.save(movie)
16
+ end
17
+ end
18
+
19
+ let(:results) { connection[:movies].all }
20
+
21
+ it "should save the changes" do
22
+ results.first[:name].should == 'new name'
23
+ end
24
+
25
+ it "should not create any new records" do
26
+ results.count.should == 1
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+ require_relative 'integration/fixtures/movie_mapping.rb'
3
+
4
+ shared_context "orm" do
5
+ let(:connection) { Sequel.connect(connection_string) }
6
+ let(:connection_string) { 'sqlite://test.db' }
7
+ let(:configuration) { Humble::Configuration.new(connection_string) }
8
+ let(:session_factory) { configuration.build_session_factory }
9
+ let(:session) { session_factory.create_session }
10
+
11
+ before :each do
12
+ connection.create_table :movies do
13
+ primary_key :id
14
+ String :name
15
+ end
16
+
17
+ configuration.add(MovieMapping.new)
18
+ end
19
+
20
+ after :each do
21
+ connection.drop_table :movies
22
+ end
23
+
24
+ end
@@ -1,4 +1,5 @@
1
- require "rubygems"
1
+ require 'rubygems'
2
2
  require 'sequel'
3
- require "sqlite3"
3
+ require 'sqlite3'
4
4
  require 'humble'
5
+ require 'rspec-fakes'
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+ class Book
3
+ attr_reader :id, :name
4
+
5
+ def initialize(attributes)
6
+ @id = attributes[:id]
7
+ @name = attributes[:name]
8
+ end
9
+ end
10
+
11
+ describe Humble::DefaultDataRowMapper do
12
+ let(:sut) { Humble::DefaultDataRowMapper.new(configuration) }
13
+ let(:configuration) { { :type => Book } }
14
+
15
+ let(:result) { sut.map_from({:id => 1, :name => 'blah'}) }
16
+
17
+ it "should map the id" do
18
+ result.id.should == 1
19
+ end
20
+
21
+ it "should map the name" do
22
+ result.name.should == "blah"
23
+ end
24
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe Humble::ResultSet do
4
+ let(:sut) { Humble::ResultSet.new(rows, mapper) }
5
+ let(:mapper) { fake }
6
+ let(:rows) { [{:id => 1}, {id: 2}] }
7
+
8
+ before :each do
9
+ mapper.stub(:map_from).with({:id => 1}).and_return(1)
10
+ mapper.stub(:map_from).with({:id => 2}).and_return(2)
11
+ end
12
+
13
+ describe :inspect do
14
+ let(:result) { sut.inspect }
15
+
16
+ it "should display each row" do
17
+ result.should == "[1, 2]"
18
+ end
19
+ end
20
+
21
+ describe :each do
22
+ it "should visit each mapped item" do
23
+ collect = []
24
+ sut.each { |item| collect << item }
25
+ collect.first.should == 1
26
+ collect.last.should == 2
27
+ end
28
+ end
29
+
30
+ describe :include? do
31
+ it "should return true" do
32
+ sut.include?(1).should be_true
33
+ end
34
+
35
+ it "should return false" do
36
+ sut.include?(0).should be_false
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: humble
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1374296843
4
+ version: 0.0.1374368890
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo khan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-20 00:00:00.000000000 Z
11
+ date: 2013-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -120,7 +120,6 @@ files:
120
120
  - .ruby-version
121
121
  - .travis.yml
122
122
  - Gemfile
123
- - Gemfile.lock
124
123
  - LICENSE.txt
125
124
  - README.md
126
125
  - Rakefile
@@ -137,9 +136,15 @@ files:
137
136
  - lib/humble/session.rb
138
137
  - lib/humble/session_factory.rb
139
138
  - lib/humble/version.rb
140
- - spec/integration/example_spec.rb
139
+ - spec/integration/delete_spec.rb
141
140
  - spec/integration/fixtures/movie_mapping.rb
141
+ - spec/integration/insert_spec.rb
142
+ - spec/integration/select_spec.rb
143
+ - spec/integration/update_spec.rb
144
+ - spec/integration_helper.rb
142
145
  - spec/spec_helper.rb
146
+ - spec/unit/default_data_row_mapper_spec.rb
147
+ - spec/unit/result_set_spec.rb
143
148
  homepage: ''
144
149
  licenses:
145
150
  - MIT
@@ -165,6 +170,12 @@ signing_key:
165
170
  specification_version: 4
166
171
  summary: an nhibernate like orm for ruby.
167
172
  test_files:
168
- - spec/integration/example_spec.rb
173
+ - spec/integration/delete_spec.rb
169
174
  - spec/integration/fixtures/movie_mapping.rb
175
+ - spec/integration/insert_spec.rb
176
+ - spec/integration/select_spec.rb
177
+ - spec/integration/update_spec.rb
178
+ - spec/integration_helper.rb
170
179
  - spec/spec_helper.rb
180
+ - spec/unit/default_data_row_mapper_spec.rb
181
+ - spec/unit/result_set_spec.rb
@@ -1,77 +0,0 @@
1
- require "spec_helper"
2
- require_relative 'fixtures/movie_mapping.rb'
3
-
4
- describe "orm" do
5
- let(:connection) { Sequel.connect(connection_string) }
6
- let(:connection_string) { 'sqlite://test.db' }
7
- let(:configuration) { Humble::Configuration.new(connection_string) }
8
- let(:session_factory) { configuration.build_session_factory }
9
- let(:session) { session_factory.create_session }
10
-
11
- before :each do
12
- connection.create_table :movies do
13
- primary_key :id
14
- String :name
15
- end
16
-
17
- configuration.add(MovieMapping.new)
18
- end
19
-
20
- after :each do
21
- connection.drop_table :movies
22
- end
23
-
24
- context "when fetching all items" do
25
- before :each do
26
- @id = connection[:movies].insert(:name => 'monsters inc')
27
- end
28
-
29
- let(:results) { session.find_all Movie }
30
-
31
- it "should return the correct number of movies" do
32
- results.count.should == 1
33
- end
34
-
35
- it "should return each movie with its name" do
36
- results.first.name.should == 'monsters inc'
37
- end
38
-
39
- it "should return instances of the target type" do
40
- results.first.should be_instance_of(Movie)
41
- end
42
-
43
- it "should include the saved movie" do
44
- results.should include(Movie.new(:id => @id, :name => 'monsters inc'))
45
- end
46
- end
47
-
48
- context "when inserting a new record" do
49
- let(:movie) { Movie.new(:name => 'oop') }
50
-
51
- before :each do
52
- session.begin_transaction do |session|
53
- session.save(movie)
54
- end
55
- end
56
-
57
- let(:results) { connection[:movies].all }
58
-
59
- it "should insert the correct number of records" do
60
- results.count.should == 1
61
- end
62
-
63
- it "should insert the record with the a new id" do
64
- results.first[:id].should_not == -1
65
- results.first[:id].should > 0
66
- end
67
-
68
- it "should insert the name" do
69
- results.first[:name].should == 'oop'
70
- end
71
-
72
- it "should update the new item with the new id" do
73
- movie.id.should_not == -1
74
- end
75
- end
76
-
77
- end