epiphy 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +66 -13
- data/lib/epiphy/adapter/rethinkdb.rb +3 -3
- data/lib/epiphy/entity.rb +10 -3
- data/lib/epiphy/repository.rb +7 -7
- data/lib/epiphy/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebef4094fb32036a44c51f7bcb2ba81ca4ce5fee
|
4
|
+
data.tar.gz: 66714f2f59108768bd92ab0f40e9a945a53b0cb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07371cbf80b7c37a5011502b09aad72ca0b638fdf8c9159f9b12f08e2ec87ae95787c8f4363249f9f176e5bb10d6bee7b0ed3f99ed4b2e008d6e1c6da7e6317d
|
7
|
+
data.tar.gz: 1ea6865c035fb28c652e2ff622b156db0ba41203acef3f5465deef1d384e9449c6d5e87c40317bced46c8c138bda3f975b68f748a7bd3e7d803842cb35310dcf
|
data/README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# Epiphy
|
2
2
|
|
3
|
-
A persistence framework for [RethinkDB](http://rethinkdb.com). The library is used on [phim365.today](http://phim365.today). Its API is
|
3
|
+
A persistence framework for [RethinkDB](http://rethinkdb.com). The library is used on [phim365.today](http://phim365.today). Its API is inspired by Lotus::Model.
|
4
|
+
|
5
|
+
`Epiphy` is name after `Epiphyllum`, my wife's name.
|
4
6
|
|
5
7
|
# Status
|
6
8
|
|
7
9
|
[![wercker status](https://app.wercker.com/status/63dd458158948712a03a00d69a96f67b/m "wercker status")](https://app.wercker.com/project/bykey/63dd458158948712a03a00d69a96f67b)
|
8
10
|
|
9
|
-
# Book [Simply RethinkDB](http://leanpub.com/
|
11
|
+
# Book [Simply RethinkDB](http://leanpub.com/simplyrethinkdb)
|
10
12
|
|
11
13
|
I also write this book to practice RethinkDB. Please consider buying a
|
12
14
|
copy if you want to support the author.
|
@@ -18,8 +20,21 @@ to learn more Ruby and reinvent the wheel because I didn't know how the
|
|
18
20
|
wheel was created. More than that, my bad code will not be able to make
|
19
21
|
it to Lotus::Model.
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
+
# Philosophy
|
24
|
+
|
25
|
+
Does basic thing well and leave complex query to RethinkDB.
|
26
|
+
|
27
|
+
RethinkDB query is very good. By wrapping an ORM around it, we can
|
28
|
+
destroy the joy of using ReQL. I only want to do basic thing with
|
29
|
+
RethinkDB, the complex query should be done use ReQL. The result of
|
30
|
+
query is converted back to an entity of an array of entity when
|
31
|
+
possible.
|
32
|
+
|
33
|
+
# API
|
34
|
+
|
35
|
+
RethinkDB delivers a convenient public API to execute queries and commands
|
36
|
+
against a database. The architecture eases keeping the business logic
|
37
|
+
(entities) separated from details such as persistence or validations.
|
23
38
|
|
24
39
|
It implements the following concepts:
|
25
40
|
|
@@ -28,8 +43,6 @@ It implements the following concepts:
|
|
28
43
|
* [Adapter](#adapter) – A database adapter.
|
29
44
|
* [Query](#query) - An object that represents a database query.
|
30
45
|
|
31
|
-
`Epiphy` is name after `Epiphyllum`, my spouse's name.
|
32
|
-
|
33
46
|
# Install
|
34
47
|
|
35
48
|
```
|
@@ -59,12 +72,15 @@ $ rake test
|
|
59
72
|
```
|
60
73
|
|
61
74
|
A testing database will be created during the testing. The testing data
|
62
|
-
will hit your RethinkDB. Depend on your storge system, test can fast or
|
75
|
+
will hit your RethinkDB. Depend on your storge system, test can be fast or
|
63
76
|
slow.
|
64
77
|
|
65
78
|
# Usage
|
66
79
|
|
67
|
-
|
80
|
+
Checkout [Examples.md](blob/master/EXAMPLE.md) for detail guide and [examples](tree/master/examples) folder for real
|
81
|
+
code.
|
82
|
+
|
83
|
+
## Entities
|
68
84
|
|
69
85
|
An object that is defined by its identity.
|
70
86
|
|
@@ -74,7 +90,8 @@ It deals with one and only one responsibility that is pertinent to the domain of
|
|
74
90
|
|
75
91
|
This simplicity of design allows developers to focus on behaviors, or message passing if you will, which is the quintessence of Object Oriented Programming.
|
76
92
|
|
77
|
-
|
93
|
+
|
94
|
+
## Setup
|
78
95
|
|
79
96
|
```ruby
|
80
97
|
connection = Epiphy::Connection.create
|
@@ -82,27 +99,63 @@ adapter = Epiphy::Adapter::RethinkDB.new connection
|
|
82
99
|
RethinkDB::Repository.configure do |r|
|
83
100
|
r.adapter = adapter
|
84
101
|
end
|
102
|
+
```
|
85
103
|
|
104
|
+
## Define your entity
|
105
|
+
|
106
|
+
```ruby
|
86
107
|
class Movie
|
87
108
|
include Epiphy::Entity
|
88
|
-
include Epiphy::Entity::Timestamp
|
89
109
|
|
90
|
-
attributes :title, :url
|
110
|
+
self.attributes= :title, :url, :type
|
91
111
|
end
|
112
|
+
```
|
113
|
+
|
114
|
+
## Define your repository
|
92
115
|
|
116
|
+
```ruby
|
93
117
|
class MovieRepository
|
94
118
|
include Epiphy::Repository
|
95
119
|
end
|
120
|
+
```
|
121
|
+
|
122
|
+
## Query
|
123
|
+
|
124
|
+
```ruby
|
96
125
|
|
97
126
|
movie = MovieRepository.find id # Find by id
|
98
127
|
|
99
|
-
movie = MovieRepository.first
|
100
|
-
movie = MovieRepository.last
|
128
|
+
movie = MovieRepository.first :created_at # Find first entity, order by field :date
|
129
|
+
movie = MovieRepository.last :created_at # Find first entity, order by field :date
|
101
130
|
|
102
131
|
movie = Movie.new
|
103
132
|
movie.title = "A movie"
|
104
133
|
MovieRepository.create movie
|
134
|
+
puts movie.id # return the ID of inserted movie
|
135
|
+
|
136
|
+
movie.title = "A new title"
|
105
137
|
MovieRepository.update movie
|
138
|
+
|
139
|
+
movie = Movie.new title: 'Another one', url: "http://youtube.com/foo", type: 'anime'
|
140
|
+
movie.id = Time.now.to_i #Manually assign an id
|
141
|
+
MovieRepository.create movie
|
142
|
+
|
143
|
+
```
|
144
|
+
|
145
|
+
## Custom query
|
146
|
+
|
147
|
+
From inside a Repository, we can call `query` method and pass in a
|
148
|
+
block. The method expose two object
|
149
|
+
|
150
|
+
* Current ReQL command to play
|
151
|
+
* Global top name space `r`
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
class MovieRepository
|
155
|
+
|
156
|
+
def lop
|
157
|
+
end
|
158
|
+
end
|
106
159
|
```
|
107
160
|
|
108
161
|
# Contributing to epiphy
|
data/lib/epiphy/entity.rb
CHANGED
@@ -107,10 +107,12 @@ module Epiphy
|
|
107
107
|
|
108
108
|
class_eval %{
|
109
109
|
def initialize(attributes = {})
|
110
|
-
|
110
|
+
attributes.keys.each do |key|
|
111
|
+
attributes[(key.to_sym rescue key) || key] = attributes.delete(key)
|
112
|
+
end
|
113
|
+
#{ @attributes.map {|a| "@#{a}" }.join(', ') }, = *attributes.values_at(#{ @attributes.map {|a| ":#{a}"}.join(', ') })
|
111
114
|
end
|
112
115
|
}
|
113
|
-
|
114
116
|
attr_accessor *@attributes
|
115
117
|
end
|
116
118
|
|
@@ -132,7 +134,12 @@ module Epiphy
|
|
132
134
|
# @see .attributes
|
133
135
|
def initialize(attributes = {})
|
134
136
|
attributes.each do |k, v|
|
135
|
-
|
137
|
+
case k
|
138
|
+
when Symbol
|
139
|
+
public_send("#{ k }=", v)
|
140
|
+
when String
|
141
|
+
public_send("#{ k.to_sym }=", v)
|
142
|
+
end
|
136
143
|
end
|
137
144
|
end
|
138
145
|
|
data/lib/epiphy/repository.rb
CHANGED
@@ -491,18 +491,18 @@ module Epiphy
|
|
491
491
|
#
|
492
492
|
# ArticleRepository.find(9) # => raises Epiphy::Model::EntityNotFound
|
493
493
|
def find(id)
|
494
|
-
entity_id =
|
494
|
+
entity_id = nil
|
495
495
|
if id.is_a? Epiphy::Entity
|
496
496
|
raise TypeError, "Expecting an string, primitve value"
|
497
497
|
end
|
498
498
|
|
499
|
-
if
|
500
|
-
|
501
|
-
|
499
|
+
if id.is_a?(String) || id.is_a?(Integer)
|
500
|
+
entity_id = id
|
501
|
+
else
|
502
|
+
entity_id = id.id if id.respond_to? :id
|
502
503
|
end
|
503
|
-
|
504
|
-
|
505
|
-
#end
|
504
|
+
|
505
|
+
raise Epiphy::Model::EntityIdNotFound, "Missing entity id" if entity_id.nil?
|
506
506
|
result = @adapter.find(collection, entity_id).tap do |record|
|
507
507
|
raise Epiphy::Model::EntityNotFound.new unless record
|
508
508
|
end
|
data/lib/epiphy/version.rb
CHANGED