rlet 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +29 -1
  3. data/lib/rlet/expose.rb +62 -0
  4. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8109f3f222f16dbf36e7da3292ed7987f00a5932
4
- data.tar.gz: 17310f476fb99e9f82a8ce6147b63c2663908f80
3
+ metadata.gz: 3d8831d96794b32700eb7377a96e984db93826ca
4
+ data.tar.gz: 38aafb34b173433c9d7878623c7d4b8a4fe05a6e
5
5
  SHA512:
6
- metadata.gz: 9c15981e58772ccf331396163b264b1a1b6f1506ccc5f9c1d45a21a3c0e36636200879f519b12e2a81df18c9032a4fc080e01ad40fecc1164a4620680e3ce1e7
7
- data.tar.gz: 91b670103ba95aad945548791632b239df8287cf2e4ecc24d155cc8f3561392188022bc3f526fdcb84e5524608b5ae2207465fabc5d9c6dc1222c69d42b0ccda
6
+ metadata.gz: 092ffe94952422d38a1c2a61c6e97cd0a0a5e8f521545b19abe8699311b038d7df49947c19d5a36fbe93c21a12fa7ad34f9bd4287e4b0129f2ad42f71991b0d2
7
+ data.tar.gz: 27cc34e82f63d86c55a89ea976ba98121427c09c5f54f6475229a3d5f06aa3e4f66d1a589d8c256fe191f4167d0afcd9c00dd36f86cc15235aae3472cd560a05
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  This library contains a library for RSpec-style let() declartions for lazy evaluation,
4
4
  concerns, and a few functional operators.
5
5
 
6
- Unlike ruby-let, or what is proposed here http://www.opensourcery.com/blog/zack-hobson/objectlet-ruby-0
6
+ Unlike ruby-let, or what is proposed here [https://www.opensourcery.com/blog/zack-hobson/objectlet-ruby](https://www.opensourcery.com/blog/zack-hobson/objectlet-ruby)
7
7
  this does not actually mimic the let of functional programming.
8
8
 
9
9
  Here, `let()` defines a method in the class. The values are memoized. This allows for both
@@ -53,6 +53,34 @@ The gems contain two modules, Let and Concern. You can use them like so:
53
53
  Concern is embedded from ActiveSupport. If ActiveSupport::Concern is loaded, it will use that. This
54
54
  allows one to use concerns without having ActiveSupport as a dependency.
55
55
 
56
+ Additionally, to expose `let()` with instance variables for use in templates, you can use `expose`
57
+
58
+ require 'rlet'
59
+ require 'rlet/expose'
60
+
61
+ class ContactsController
62
+ include Let
63
+ include RLet::Expose
64
+ include RestfulResource
65
+
66
+ let(:model) { Contact }
67
+
68
+ expose :resource, only: :show
69
+
70
+ def show
71
+ respond_with resource
72
+ end
73
+ end
74
+
75
+ module RestfulResource
76
+ extend Concern
77
+
78
+ included do
79
+ let(:resource) { model.find(id) }
80
+ let(:id) { params[:id] }
81
+ end
82
+ end
83
+
56
84
  ### LazyOptions
57
85
 
58
86
  One pattern that comes up is creating a class which takes an option as an initializer, and then
@@ -0,0 +1,62 @@
1
+ # Include this module if you want to expose methods as instance
2
+ # variables for Rails templates
3
+ #
4
+ # Since let variables are not exposed to the view,
5
+ # this allows ou to define instance variables to pass
6
+ # to the view.
7
+ #
8
+ # Example:
9
+ # class UsersController < ActiveController::Base
10
+ # include RLet::Expose
11
+ # include RLet::Let
12
+ #
13
+ # let(:user) { current_user }
14
+ # expose :user # Assigns @user = self.user
15
+ #
16
+ # end
17
+ #
18
+ # Other examples:
19
+ #
20
+ # expose :user, :options
21
+ # expose :user, only: [ :create, :show ]
22
+ # expose :user, :options, except: :destroy
23
+ #
24
+ # You can also use this in ApplicationController:
25
+ #
26
+ # require 'rlet/let'
27
+ # require 'rlet/expose'
28
+ #
29
+ # class ApplicationController < ActionController::Base
30
+ # include RLet::Let
31
+ # include RLet::Expose
32
+ # end
33
+ #
34
+ #
35
+ # Considerations:
36
+ #
37
+ # This is implemented using a before_filter. So when you expose an
38
+ # rlet, it will -always- be evaluated. Be careful of what you expose
39
+ # and which controller actions to expose it to.
40
+ module RLet
41
+ module Expose
42
+ extend Concern
43
+
44
+ module ClassMethods
45
+ # expose :user
46
+ # expose :user, :options
47
+ # expose :user, only: [ :create, :show ]
48
+ # expose :user, :options, except: :destroy
49
+ def expose(*lvars)
50
+ options = lvars.last.is_a?(Hash) ? lvars.pop : {}
51
+ lvars.each do |lvar|
52
+ self.before_filter(options) do |controller|
53
+ controller.instance_eval do
54
+ self.instance_variable_set("@#{lvar}", self.send(lvar))
55
+ end
56
+ end
57
+ end
58
+ end # expose
59
+
60
+ end # ClassMethods
61
+ end # Expose
62
+ end # RLet
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rlet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ho-Sheng Hsiao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-27 00:00:00.000000000 Z
11
+ date: 2015-05-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Use rspec's let() outside of rspec with concerns and functional operators
14
14
  email:
@@ -21,6 +21,7 @@ files:
21
21
  - README.md
22
22
  - lib/rlet.rb
23
23
  - lib/rlet/concern.rb
24
+ - lib/rlet/expose.rb
24
25
  - lib/rlet/functional.rb
25
26
  - lib/rlet/lazy_options.rb
26
27
  - lib/rlet/let.rb