humble 0.0.1 → 0.0.1369313534

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99f0c16e389f39eb60cb925e39012f71442a64ca
4
- data.tar.gz: accf8b6c54011457d43049538d2e6e7561e01653
3
+ metadata.gz: bb370818a8538c204b75957aab9a054d19b437c2
4
+ data.tar.gz: b91eb088e167f8c94df5e03e8217075986674e0d
5
5
  SHA512:
6
- metadata.gz: a28bf2917c0d81a692bc0dd4176d014edc0830a865e35500598ef764cfc80973f9fc342f7585ad18e562831749efb65b3ee2ae341c73b6943dc496586b34fdf3
7
- data.tar.gz: d06d48f09db1d22f6c61e25541e4e69051fafe5cc09e080c4a39456318fa6e1b4130a4f282c8be5721d0002507318da8294423eede07c8faa5eac56b86239c3a
6
+ metadata.gz: fe6cef272f2f15098ef34c5d5c938ebe4082670f7807de1aa3ac15acc90d5c70ec0650bad0ce7204149d30ce5973a0b5fa20341aa8d92bffc22b04c7c4ad4c2e
7
+ data.tar.gz: 8f0e2bde46256c0e6c8d34c146a98d2168f1726c796f2d0e9c50ab4e4e05e2b6e5c2aac117cb4f75c05e8d1115099691836c516f852f3f05fa4e5f7e4e45fd2c
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- humble (0.0.1)
4
+ humble (0.0.1369313534)
5
5
  sequel
6
6
 
7
7
  GEM
data/Rakefile CHANGED
@@ -1 +1,20 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ task :default => :spec
4
+
5
+ task :spec do
6
+ system 'bundle exec rspec spec'
7
+ end
8
+
9
+ task :clean do
10
+ system 'rm *.gem'
11
+ system 'rm -fr pkg'
12
+ end
13
+
14
+ task :build => :clean do
15
+ system 'gem build humble.gemspec'
16
+ end
17
+
18
+ task :publish => :build do
19
+ system 'gem push *.gem'
20
+ end
@@ -0,0 +1,18 @@
1
+ module Humble
2
+ class Column
3
+ def initialize(attributes)
4
+ @attributes = attributes
5
+ end
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 }
12
+ end
13
+
14
+ def primary_key?
15
+ @attributes[:primary_key]
16
+ end
17
+ end
18
+ end
@@ -8,7 +8,7 @@ module Humble
8
8
  end
9
9
 
10
10
  def add(mapping)
11
- @mappings.push(mapping)
11
+ @mappings.push(prepare(mapping))
12
12
  end
13
13
 
14
14
  def build_session_factory
@@ -20,5 +20,12 @@ module Humble
20
20
  mapping.is_for?(item)
21
21
  end
22
22
  end
23
+
24
+ private
25
+
26
+ def prepare(mapping, builder = MappingConfigurationBuilder.new)
27
+ mapping.run(builder)
28
+ builder.build
29
+ end
23
30
  end
24
31
  end
@@ -0,0 +1,4 @@
1
+ module Humble
2
+ class DatabaseMapping
3
+ end
4
+ end
@@ -0,0 +1,27 @@
1
+ module Humble
2
+ class DatabaseTable
3
+ attr_reader :name
4
+
5
+ def initialize
6
+ @columns = []
7
+ end
8
+
9
+ def named(name)
10
+ @name = name
11
+ end
12
+
13
+ def primary_key(name, default: 0)
14
+ @columns << Column.new(:name => name, :default => default, :primary_key => true)
15
+ end
16
+
17
+ def add_column(name)
18
+ @columns << Column.new(:name => name)
19
+ end
20
+
21
+ def insert(item)
22
+ @columns.inject({}) do |result, column|
23
+ result.merge(column.prepare_insert(item))
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,11 @@
1
+ module Humble
2
+ class DefaultDataRowMapper
3
+ def initialize(configuration)
4
+ @configuration = configuration
5
+ end
6
+
7
+ def map_from(row)
8
+ @configuration[:type].new(row)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ module Humble
2
+ class MappingConfiguration
3
+ def initialize(attributes, table)
4
+ @attributes = attributes
5
+ @table = table
6
+ end
7
+
8
+ def find_all_using(connection)
9
+ ResultSet.new(connection[@table.name], mapper)
10
+ end
11
+
12
+ def save_using(connection, item)
13
+ connection[@table.name].insert(@table.insert(item))
14
+ end
15
+
16
+ def is_for?(item)
17
+ item == @attributes[:type] || item.is_a?(@attributes[:type])
18
+ end
19
+
20
+ def [](key)
21
+ @attributes[key]
22
+ end
23
+
24
+ private
25
+
26
+ def mapper
27
+ @attributes[:mapper] || DefaultDataRowMapper.new(self)
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,30 @@
1
+ module Humble
2
+ class MappingConfigurationBuilder
3
+ def initialize(attributes = {}, table = DatabaseTable.new)
4
+ @attributes = attributes
5
+ @table = table
6
+ end
7
+
8
+ def table(name)
9
+ @attributes[:table] = name
10
+ @table.named(name)
11
+ end
12
+
13
+ def type(name)
14
+ @attributes[:type] = name
15
+ end
16
+
17
+ def primary_key(name, default: 0)
18
+ @attributes[:primary_key] = name
19
+ @table.primary_key(name, default: default)
20
+ end
21
+
22
+ def column(name)
23
+ @table.add_column(name)
24
+ end
25
+
26
+ def build
27
+ MappingConfiguration.new(@attributes, @table)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ module Humble
2
+ class ResultSet
3
+ include Enumerable
4
+
5
+ def initialize(rows, mapper)
6
+ @rows = rows
7
+ @mapper = mapper
8
+ end
9
+
10
+ def each(&block)
11
+ @rows.each do |row|
12
+ block.call(@mapper.map_from(row))
13
+ end
14
+ end
15
+ end
16
+ end
@@ -16,7 +16,7 @@ module Humble
16
16
  end
17
17
 
18
18
  def find_all(clazz)
19
- mapping_for(clazz).find_all_using(create_connection, clazz)
19
+ mapping_for(clazz).find_all_using(create_connection)
20
20
  end
21
21
 
22
22
  private
@@ -1,3 +1,3 @@
1
1
  module Humble
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.#{Time.now.to_i}"
3
3
  end
data/lib/humble.rb CHANGED
@@ -2,6 +2,13 @@ require "humble/version"
2
2
  require "humble/session_factory"
3
3
  require "humble/session"
4
4
  require "humble/configuration"
5
+ require "humble/database_mapping"
6
+ require "humble/default_data_row_mapper"
7
+ require "humble/result_set"
8
+ require "humble/mapping_configuration"
9
+ require "humble/mapping_configuration_builder"
10
+ require "humble/database_table"
11
+ require "humble/column"
5
12
 
6
13
  module Humble
7
14
 
@@ -15,9 +15,6 @@ describe "orm" do
15
15
  end
16
16
 
17
17
  configuration.add(MovieMapping.new)
18
- session.begin_transaction do |session|
19
- session.save(Movie.new(:name => 'monsters inc'))
20
- end
21
18
  end
22
19
 
23
20
  after :each do
@@ -25,6 +22,10 @@ describe "orm" do
25
22
  end
26
23
 
27
24
  context "when fetching all items" do
25
+ before :each do
26
+ connection[:movies].insert(:name => 'monsters inc')
27
+ end
28
+
28
29
  let(:results) { session.find_all Movie }
29
30
 
30
31
  it "should return the correct number of movies" do
@@ -40,4 +41,27 @@ describe "orm" do
40
41
  end
41
42
  end
42
43
 
44
+ context "when inserting a new record" do
45
+ before :each do
46
+ session.begin_transaction do |session|
47
+ session.save(Movie.new(:name => 'oop'))
48
+ end
49
+ end
50
+
51
+ let(:results) { connection[:movies].all }
52
+
53
+ it "should insert the correct number of records" do
54
+ results.count.should == 1
55
+ end
56
+
57
+ it "should insert the record with the a new id" do
58
+ results.first[:id].should_not == -1
59
+ results.first[:id].should > 0
60
+ end
61
+
62
+ it "should insert the name" do
63
+ results.first[:name].should == 'oop'
64
+ end
65
+ end
66
+
43
67
  end
@@ -1,28 +1,17 @@
1
1
  class Movie
2
- attr_reader :name
2
+ attr_reader :id, :name
3
3
 
4
4
  def initialize(attributes)
5
+ @id = attributes[:name] || -1
5
6
  @name = attributes[:name]
6
7
  end
7
8
  end
8
9
 
9
- class MovieMapping
10
- def is_for?(item)
11
- item == Movie || item.is_a?(Movie)
12
- end
13
-
14
- def save_using(connection, item)
15
- connection[:movies].insert(:name => item.name)
16
- end
17
-
18
- def find_all_using(connection, clazz)
19
- connection[:movies].map do |row|
20
- Movie.new(row)
21
- end
22
- end
23
-
10
+ class MovieMapping < Humble::DatabaseMapping
24
11
  def run(map)
25
12
  map.table :movies
13
+ map.type Movie
14
+ map.primary_key(:id, default: -1)
15
+ map.column :name
26
16
  end
27
17
  end
28
-
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.1
4
+ version: 0.0.1369313534
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-05-22 00:00:00.000000000 Z
11
+ date: 2013-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -118,6 +118,7 @@ files:
118
118
  - .gitignore
119
119
  - .ruby-gemset
120
120
  - .ruby-version
121
+ - .travis.yml
121
122
  - Gemfile
122
123
  - Gemfile.lock
123
124
  - LICENSE.txt
@@ -125,7 +126,14 @@ files:
125
126
  - Rakefile
126
127
  - humble.gemspec
127
128
  - lib/humble.rb
129
+ - lib/humble/column.rb
128
130
  - lib/humble/configuration.rb
131
+ - lib/humble/database_mapping.rb
132
+ - lib/humble/database_table.rb
133
+ - lib/humble/default_data_row_mapper.rb
134
+ - lib/humble/mapping_configuration.rb
135
+ - lib/humble/mapping_configuration_builder.rb
136
+ - lib/humble/result_set.rb
129
137
  - lib/humble/session.rb
130
138
  - lib/humble/session_factory.rb
131
139
  - lib/humble/version.rb