selfer 0.1.0 → 0.2.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 +37 -17
- data/lib/selfer.rb +8 -3
- data/lib/selfer/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: e94fab23e39c96484ee7e6c08879232aae96d26e
|
4
|
+
data.tar.gz: e3bd31f13858c8e25636a13abee3e6161ec807ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51138d00b79747528981df882ba67ea00f9025448108427bc07749c047d6219d7df693fc965770b5a486ce07678fabab0db2dab74e622edee7466df026176001
|
7
|
+
data.tar.gz: a69a14ca1580c1ec04302522df1aa9aa043615124b42d117166a16b23d91fc2fa9ce91c8353e6466e835407e523546b84f9b60694b386212ffd7e66fcccd193d
|
data/README.md
CHANGED
@@ -1,48 +1,68 @@
|
|
1
1
|
# Selfer
|
2
2
|
|
3
|
+
[![Code Climate](https://codeclimate.com/github/robwilliams/selfer/badges/gpa.svg)](https://codeclimate.com/github/robwilliams/selfer) [![Build Status](https://travis-ci.org/robwilliams/selfer.svg)](https://travis-ci.org/robwilliams/selfer) [![Gem Version](https://badge.fury.io/rb/selfer.svg)](http://badge.fury.io/rb/selfer)
|
4
|
+
|
3
5
|
Stops you having to write class methods that delegate to their respective
|
4
6
|
instance method.
|
5
7
|
|
8
|
+
### Basic Usage
|
9
|
+
|
10
|
+
`VisibleQuery.find(Hotel.all)` looks nicer than
|
11
|
+
`VisibleQuery.new(Hotel.all).find` but the latter is easier to test. This gem
|
12
|
+
allows the best of both worlds.
|
13
|
+
|
6
14
|
Before:
|
7
15
|
|
8
16
|
```ruby
|
9
|
-
class
|
10
|
-
def initialize(
|
11
|
-
@
|
12
|
-
|
17
|
+
class VisibleQuery
|
18
|
+
def initialize(relation)
|
19
|
+
@relation = relation
|
20
|
+
end
|
21
|
+
|
22
|
+
def all
|
23
|
+
@relation.where(hidden: false)
|
13
24
|
end
|
14
25
|
|
15
|
-
def find
|
16
|
-
|
26
|
+
def find(id)
|
27
|
+
all.find(id)
|
17
28
|
end
|
18
29
|
|
19
|
-
def self.
|
20
|
-
new(
|
30
|
+
def self.all(relation)
|
31
|
+
new(relation).all
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.find(relation, id)
|
35
|
+
new(relation).find(id)
|
21
36
|
end
|
22
37
|
end
|
23
38
|
|
24
|
-
|
39
|
+
VisibleQuery.all(Hotel.all)
|
40
|
+
VisibleQuery.find(Hotel.all,1)
|
25
41
|
```
|
26
42
|
|
27
43
|
After:
|
28
44
|
|
29
45
|
```ruby
|
30
|
-
class
|
46
|
+
class VisibleQuery
|
31
47
|
extend Selfer
|
32
48
|
|
33
|
-
selfer :find
|
49
|
+
selfer :all, :find
|
50
|
+
|
51
|
+
def initialize(relation)
|
52
|
+
@relation = relation
|
53
|
+
end
|
34
54
|
|
35
|
-
def
|
36
|
-
@
|
37
|
-
@options = {}
|
55
|
+
def all
|
56
|
+
@relation.where(hidden: false)
|
38
57
|
end
|
39
58
|
|
40
|
-
def find
|
41
|
-
|
59
|
+
def find(id)
|
60
|
+
all.find(id)
|
42
61
|
end
|
43
62
|
end
|
44
63
|
|
45
|
-
|
64
|
+
VisibleQuery.all(Hotel.all)
|
65
|
+
VisibleQuery.find(Hotel.all,1)
|
46
66
|
```
|
47
67
|
|
48
68
|
## Installation
|
data/lib/selfer.rb
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
require "selfer/version"
|
2
2
|
|
3
3
|
module Selfer
|
4
|
-
def selfer(
|
5
|
-
|
6
|
-
|
4
|
+
def selfer(*method_names)
|
5
|
+
method_names.each do |method_name|
|
6
|
+
define_singleton_method(method_name) do |*args|
|
7
|
+
instance = allocate
|
8
|
+
init_args = args.shift(instance.method(:initialize).arity)
|
9
|
+
instance.send(:initialize, *init_args)
|
10
|
+
instance.public_send(method_name, *args)
|
11
|
+
end
|
7
12
|
end
|
8
13
|
end
|
9
14
|
end
|
data/lib/selfer/version.rb
CHANGED