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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c06ea1e37cd59ed7e8a51303d2e802e67c8fd7f4
4
- data.tar.gz: bf051d0857eba4272f9cc22f4c261a36a0ad6430
3
+ metadata.gz: e94fab23e39c96484ee7e6c08879232aae96d26e
4
+ data.tar.gz: e3bd31f13858c8e25636a13abee3e6161ec807ae
5
5
  SHA512:
6
- metadata.gz: 21598138ec8fd9b472bbaab763525c5b047d31543f6795da0b0098287b9b94aa7e5e2cabc0b648416666642e6c234486bb4aedd8e12353f44e5999c5f6ea090b
7
- data.tar.gz: 54969ae296c8e9cd65704315122723d7a00989544485810bac756db9b09b8de7788f61e9b2a31d25aab3c5d2ef2acadd7a17f694e680bfc0743c3d15a004ccea
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 TreeFinder
10
- def initialize(id, options={})
11
- @id = id
12
- @options = {}
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.find(id, options={})
20
- new(id, options).find
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
- TreeFinder.find(1, readonly: true)
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 TreeFinder
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 initialize(id, options={})
36
- @id = id
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
- TreeFinder.find(1, readonly: true)
64
+ VisibleQuery.all(Hotel.all)
65
+ VisibleQuery.find(Hotel.all,1)
46
66
  ```
47
67
 
48
68
  ## Installation
@@ -1,9 +1,14 @@
1
1
  require "selfer/version"
2
2
 
3
3
  module Selfer
4
- def selfer(method)
5
- define_singleton_method(method) do |*args|
6
- new(*args).send(method)
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
@@ -1,3 +1,3 @@
1
1
  module Selfer
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selfer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Williams