shoden 0.1.0 → 0.1.1

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: ffad286c05b142a30dd2c56bcc1767d0811fdbe3
4
- data.tar.gz: b30190789b1c0c31bd2f47bb4bfca193762d9f80
3
+ metadata.gz: b7442a9c8dc8cf92f8b21c5f65164c7203997e22
4
+ data.tar.gz: 2e8ab617da448d44c007d316424d996055a9d656
5
5
  SHA512:
6
- metadata.gz: 1aa71426212fa8d9bac34f40ea4ccd54002e1853711c40d991b0107b1f902f1ef20b2cf04ff0bd862280ad73de83148bad8bd979ebb1f294936249e0c57a4aa4
7
- data.tar.gz: 1859a5eb54cb8efb7ba572e138eca892c44d7dbf30e99d1e06985ed75e63e01056fe54962c0550d3daa48885c1c44d5ab597cdd89026106bec87820500c46e8f
6
+ metadata.gz: f85c95ca70139368f3f28ef288e834677524c259a776461696e64a8e3fee7bb6278952b0d1e137004a64241cff70b6626deb50b2a058cdbcc4bc8334e5c14cff
7
+ data.tar.gz: 5282c522a75466b3d1a5566a194fff963627d5d17098f7906c0888b83536f0f8e0c9a949dc2efae5bdf602106837c0a280952cc463b9ffdd40bc452da2c7f3d7
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Shoden
2
2
 
3
+ ![Elephant god](http://www.redprintdna.com/wp-content/uploads/2011/09/L-Elephant-Against-Sky.jpg)
4
+
3
5
  Shoden is a persistance library on top of Postgres.
4
6
  It is basically an [Ohm](https://github.com/soveran/ohm) clone but using
5
7
  Postgres as a main database.
@@ -4,6 +4,7 @@ Sequel.extension :pg_hstore, :pg_hstore_ops
4
4
 
5
5
  module Shoden
6
6
  MissingID = Class.new(StandardError)
7
+ NotFound = Class.new(StandardError)
7
8
 
8
9
  Proxy = Struct.new(:klass, :parent) do
9
10
  def create(args = {})
@@ -26,6 +27,12 @@ module Shoden
26
27
  @_connection ||= Sequel.connect(url)
27
28
  end
28
29
 
30
+ def self.destroy_all
31
+ connection.tables.select do |t|
32
+ connection.drop_table(t) if t.to_s.start_with?('Shoden::')
33
+ end
34
+ end
35
+
29
36
  class Model
30
37
  def initialize(attrs = {})
31
38
  @_id = attrs.delete(:id) if attrs[:id]
@@ -38,6 +45,10 @@ module Shoden
38
45
  @_id.to_i
39
46
  end
40
47
 
48
+ def destroy
49
+ lookup(id).delete
50
+ end
51
+
41
52
  def update(attrs = {})
42
53
  attrs.each { |name, value| send(:"#{name}=", value) }
43
54
  end
@@ -48,12 +59,6 @@ module Shoden
48
59
  end
49
60
 
50
61
  def save
51
- conn.execute("CREATE EXTENSION IF NOT EXISTS hstore")
52
- conn.create_table? table_name do
53
- primary_key :id
54
- hstore :data
55
- end
56
-
57
62
  if defined? @_id
58
63
  table.where(id: @_id).update data: @attributes
59
64
  else
@@ -64,7 +69,7 @@ module Shoden
64
69
  end
65
70
 
66
71
  def load!
67
- ret = table.where(id: @_id)
72
+ ret = lookup(@_id)
68
73
  update(ret.to_a.first[:data])
69
74
  self
70
75
  end
@@ -97,7 +102,7 @@ module Shoden
97
102
  define_method(writer) { |value| @attributes[reader] = value }
98
103
 
99
104
  define_method(name) do
100
- klass = Kernel.const_get(model)
105
+ klass = Kernel.const_get("Shoden::#{model}")
101
106
  klass[send(reader)]
102
107
  end
103
108
  end
@@ -111,6 +116,23 @@ module Shoden
111
116
  downcase.to_sym
112
117
  end
113
118
 
119
+ def lookup(id)
120
+ raise NotFound if !conn.tables.include?(table_name.to_sym)
121
+
122
+ row = table.where(id: id)
123
+ raise NotFound if !row.any?
124
+
125
+ row
126
+ end
127
+
128
+ def setup
129
+ Shoden.connection.execute("CREATE EXTENSION IF NOT EXISTS hstore")
130
+ Shoden.connection.create_table? table_name do
131
+ primary_key :id
132
+ hstore :data
133
+ end
134
+ end
135
+
114
136
  def table_name
115
137
  :"Shoden::#{self.class.name}"
116
138
  end
@@ -120,7 +142,9 @@ module Shoden
120
142
  end
121
143
 
122
144
  def conn
123
- Shoden.connection
145
+ c = Shoden.connection
146
+ @created ||= setup
147
+ c
124
148
  end
125
149
  end
126
150
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "shoden"
3
- s.version = "0.1.0"
3
+ s.version = "0.1.1"
4
4
  s.summary = "Object hash mapper for postgres"
5
5
  s.description = "Slim postgres models"
6
6
  s.authors = ["elcuervo"]
@@ -7,6 +7,10 @@ class User < Shoden::Model
7
7
  attribute :name
8
8
  end
9
9
 
10
+ setup do
11
+ Shoden.destroy_all
12
+ end
13
+
10
14
  test 'model' do
11
15
  model = Model.create
12
16
  assert_equal model.id.class, Fixnum
@@ -54,3 +58,12 @@ test 'relations' do
54
58
  assert sprout.is_a?(Sprout)
55
59
  assert_equal sprout.tree.id, tree.id
56
60
  end
61
+
62
+ test 'deletion' do
63
+ user = User.create(name: 'Damian')
64
+ id = user.id
65
+
66
+ user.destroy
67
+
68
+ assert_raise(Shoden::NotFound) { User[id] }
69
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shoden
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - elcuervo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-24 00:00:00.000000000 Z
11
+ date: 2014-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel