shoden 0.1.1 → 0.2.0

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: b7442a9c8dc8cf92f8b21c5f65164c7203997e22
4
- data.tar.gz: 2e8ab617da448d44c007d316424d996055a9d656
3
+ metadata.gz: 47609f0a06edadfafbcc844d6b91d23eca91244e
4
+ data.tar.gz: e7507a40336852be18a4cd30e7c3b78168f4bc3f
5
5
  SHA512:
6
- metadata.gz: f85c95ca70139368f3f28ef288e834677524c259a776461696e64a8e3fee7bb6278952b0d1e137004a64241cff70b6626deb50b2a058cdbcc4bc8334e5c14cff
7
- data.tar.gz: 5282c522a75466b3d1a5566a194fff963627d5d17098f7906c0888b83536f0f8e0c9a949dc2efae5bdf602106837c0a280952cc463b9ffdd40bc452da2c7f3d7
6
+ metadata.gz: aa632c8f3b300e52655d5a3c5ce27777e348e5cea84f8e75d6533c594911bc367c1f765f0bf94633081d192bf112e339a8952528058c878369c734604e840cfb
7
+ data.tar.gz: 2bc95a32031322ca1601cff586950f305fb12623829848c1207d9a9bac383f9c36382fdd137244f45e23a0ab0755b1c2bad604a90a642e5a147d5e0ed67a14f4
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # Shoden
1
+ # Shôden
2
2
 
3
3
  ![Elephant god](http://www.redprintdna.com/wp-content/uploads/2011/09/L-Elephant-Against-Sky.jpg)
4
4
 
5
- Shoden is a persistance library on top of Postgres.
5
+ Shôden is a persistance library on top of Postgres.
6
6
  It is basically an [Ohm](https://github.com/soveran/ohm) clone but using
7
7
  Postgres as a main database.
8
8
 
@@ -36,7 +36,7 @@ module Shoden
36
36
  class Model
37
37
  def initialize(attrs = {})
38
38
  @_id = attrs.delete(:id) if attrs[:id]
39
- @attributes = Sequel::Postgres::HStore.new({})
39
+ @attributes = {}
40
40
  update(attrs)
41
41
  end
42
42
 
@@ -60,9 +60,9 @@ module Shoden
60
60
 
61
61
  def save
62
62
  if defined? @_id
63
- table.where(id: @_id).update data: @attributes
63
+ table.where(id: @_id).update data: sanitized_attrs
64
64
  else
65
- @_id = table.insert data: @attributes
65
+ @_id = table.insert data: sanitized_attrs
66
66
  end
67
67
 
68
68
  self
@@ -78,12 +78,18 @@ module Shoden
78
78
  new(attrs).save
79
79
  end
80
80
 
81
+ def self.attributes
82
+ @attributes ||= []
83
+ end
84
+
81
85
  def self.[](id)
82
86
  new(id: id).load!
83
87
  end
84
88
 
85
- def self.attribute(name)
86
- define_method(name) { @attributes[name] }
89
+ def self.attribute(name, caster = ->(x) { x })
90
+ attributes << name if !attributes.include?(name)
91
+
92
+ define_method(name) { caster[@attributes[name]] }
87
93
  define_method(:"#{name}=") { |value| @attributes[name] = value }
88
94
  end
89
95
 
@@ -98,6 +104,8 @@ module Shoden
98
104
  reader = :"#{name}_id"
99
105
  writer = :"#{name}_id="
100
106
 
107
+ attributes << name if !attributes.include?(name)
108
+
101
109
  define_method(reader) { @attributes[reader] }
102
110
  define_method(writer) { |value| @attributes[reader] = value }
103
111
 
@@ -116,6 +124,17 @@ module Shoden
116
124
  downcase.to_sym
117
125
  end
118
126
 
127
+ def sanitized_attrs
128
+ sanitized = @attributes.map do |k, _|
129
+ val = send(k)
130
+ return if !val
131
+
132
+ [k, val.to_s]
133
+ end.compact
134
+
135
+ Sequel::Postgres::HStore.new(sanitized)
136
+ end
137
+
119
138
  def lookup(id)
120
139
  raise NotFound if !conn.tables.include?(table_name.to_sym)
121
140
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "shoden"
3
- s.version = "0.1.1"
3
+ s.version = "0.2.0"
4
4
  s.summary = "Object hash mapper for postgres"
5
5
  s.description = "Slim postgres models"
6
6
  s.authors = ["elcuervo"]
@@ -67,3 +67,14 @@ test 'deletion' do
67
67
 
68
68
  assert_raise(Shoden::NotFound) { User[id] }
69
69
  end
70
+
71
+ test 'casting' do
72
+ class A < Shoden::Model
73
+ attribute :n, ->(x) { x.to_i }
74
+ end
75
+
76
+ a = A.create(n: 1)
77
+ a_prime = A[a.id]
78
+
79
+ assert_equal a_prime.n, 1
80
+ 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.1
4
+ version: 0.2.0
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-25 00:00:00.000000000 Z
11
+ date: 2014-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel