rlet 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Ho-Sheng Hsiao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ ---------------------------------
25
+
26
+ Work derives from Rails ActiveSupport::Concern and Rspec Let. Their respective licenses can be found at:
27
+
28
+ Rails License: https://github.com/rails/rails/blob/6794e92b204572d75a07bd6413bdae6ae22d5a82/activesupport/MIT-LICENSE
29
+ Rspec License: https://github.com/rspec/rspec-core/blob/07be957b7f69447bf59ffe3ede9530436e6267ee/License.txt
@@ -0,0 +1,41 @@
1
+ rlet
2
+ ====
3
+
4
+ Ruby let(), class-based lexical scoping
5
+
6
+ Unlike ruby-let, or what is proposed here http://www.opensourcery.com/blog/zack-hobson/objectlet-ruby-0
7
+ this does not actually mimic the let of functional programming. Instead, it controls scope idiomatic
8
+ to Ruby -- "everything is an object".
9
+
10
+ Here, let() defines a method in the class. The values are memoized. This allows for both
11
+ lazy-evaluation and class-based scoping.
12
+
13
+ This is based on RSpec let(). Tutorials and documentation are forthcoming.
14
+
15
+ The gems contain two modules, Let and Concern. You can use them like so:
16
+
17
+ require 'rlet'
18
+
19
+ class ContactsController
20
+ include Let
21
+ include RestfulResource
22
+
23
+ let(:model) { Contact }
24
+
25
+ def show
26
+ respond_with resource
27
+ end
28
+ end
29
+
30
+ module RestfulResource
31
+ extend Concern
32
+
33
+ included do
34
+ let(:resource) { model.find(id) }
35
+ let(:id) { params[:id]
36
+ end
37
+ end
38
+
39
+
40
+ Concern is embedded from ActiveSupport. If ActiveSupport::Concern is loaded, it will use that. This
41
+ allows one to use concerns without having ActiveSupport as a dependency.
@@ -0,0 +1 @@
1
+ require 'rlet/let'
@@ -0,0 +1,42 @@
1
+ # This is ActiveSupport::Concern, taken from
2
+ # https://github.com/rails/rails/blob/6794e92b204572d75a07bd6413bdae6ae22d5a82/activesupport/lib/active_support/concern.rb
3
+ #
4
+ # If ActiveSupport::Concern is loaded, it will use that instead
5
+ #
6
+ # Rails license:
7
+ # https://github.com/rails/rails/blob/6794e92b204572d75a07bd6413bdae6ae22d5a82/activesupport/MIT-LICENSE
8
+
9
+ module RLet
10
+ module Concern
11
+ def self.extended(base) #:nodoc:
12
+ base.instance_variable_set("@_dependencies", [])
13
+ end
14
+
15
+ def append_features(base)
16
+ if base.instance_variable_defined?("@_dependencies")
17
+ base.instance_variable_get("@_dependencies") << self
18
+ return false
19
+ else
20
+ return false if base < self
21
+ @_dependencies.each { |dep| base.send(:include, dep) }
22
+ super
23
+ base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
24
+ base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
25
+ end
26
+ end
27
+
28
+ def included(base = nil, &block)
29
+ if base.nil?
30
+ @_included_block = block
31
+ else
32
+ super
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ Concern = if defined?(ActiveSupport) and defined?(ActiveSupport::Concern)
39
+ ActiveSupport::Concern
40
+ else
41
+ RLet::Concern
42
+ end
@@ -0,0 +1,24 @@
1
+ require 'rlet/concern'
2
+
3
+ module Let
4
+ extend Concern
5
+
6
+ module ClassMethods
7
+ def let(name, &block)
8
+ define_method(name) do
9
+ __memoized[name] ||= instance_eval(&block)
10
+ end
11
+ end
12
+ end
13
+
14
+ # Implementation based on Rspec let()
15
+ # https://github.com/rspec/rspec-core/blob/07be957b7f69447bf59ffe3ede9530436e6267ee/lib/rspec/core/let.rb
16
+ # License of RSpec:
17
+ # https://github.com/rspec/rspec-core/blob/07be957b7f69447bf59ffe3ede9530436e6267ee/License.txt
18
+
19
+ private
20
+
21
+ def __memoized # :nodoc:
22
+ @__memoized ||= {}
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rlet
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.5'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ho-Sheng Hsiao
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-09 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Class-based scoping with let(). Use rspec's let() outside of rspec
15
+ email:
16
+ - hosh@opscode.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/rlet/concern.rb
22
+ - lib/rlet/let.rb
23
+ - lib/rlet.rb
24
+ - LICENSE.txt
25
+ - README.md
26
+ homepage: http://github.com/hosh/rlet
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.6
44
+ requirements: []
45
+ rubyforge_project: rlet
46
+ rubygems_version: 1.8.10
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Class-based scoping with let()
50
+ test_files: []