sticky_blox 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/README.rdoc +19 -0
- data/README.textile +91 -0
- data/TODO +1 -0
- data/VERSION +1 -0
- data/lib/sticky_blox/behavior.rb +34 -0
- data/lib/sticky_blox/proc_extension.rb +19 -0
- data/lib/sticky_blox.rb +1 -0
- data/spec/helper.rb +7 -0
- data/spec/spearmint.rb +19 -0
- data/spec/sticky_blox_spec.rb +48 -0
- data/sticky_blox.gemspec +59 -0
- metadata +91 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Jason Rogers
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= sticky_blox
|
2
|
+
|
3
|
+
StickyBlox is a late binding traits-approach to Ruby meta-programming. It allows you to define
|
4
|
+
functionality within a class or object and apply that functionality to instances of other objects
|
5
|
+
without having to know anything about how those other objects are created.
|
6
|
+
|
7
|
+
== Note on Patches/Pull Requests
|
8
|
+
|
9
|
+
* Fork the project.
|
10
|
+
* Make your feature addition or bug fix.
|
11
|
+
* Add tests for it. This is important so I don't break it in a
|
12
|
+
future version unintentionally.
|
13
|
+
* Commit, do not mess with rakefile, version, or history.
|
14
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
15
|
+
* Send me a pull request. Bonus points for topic branches.
|
16
|
+
|
17
|
+
== Copyright
|
18
|
+
|
19
|
+
Copyright (c) 2010 Jason Rogers. See LICENSE for details.
|
data/README.textile
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
h2. What is StickyBlox?
|
2
|
+
|
3
|
+
StickyBlox is a late binding traits-approach to Ruby meta-programming. It allows you to define
|
4
|
+
functionality within a class or object and apply that functionality to instances of other objects
|
5
|
+
without having to know anything about how those other objects are created.
|
6
|
+
|
7
|
+
h2. Why?
|
8
|
+
|
9
|
+
If you're thinking to yourself, "I could just open a class and directly add the methods I want, and
|
10
|
+
get the same behavior, or I could create a module and extend or include it where ever I want..." you
|
11
|
+
would be right. So, why use StickyBlox? It's really a matter of taste. Sometimes, I don't like opening
|
12
|
+
other classes to add my application specific behavior, and I would rather have somewhere else in which
|
13
|
+
to encapsulate that behavior. I would also like to be able to reuse that behavior with any object
|
14
|
+
without having to add the methods to every class in the system. StickyBlox allows you have the behavior
|
15
|
+
shoe-horned into classes such that the functionalities (sticks) become instance methods or just have
|
16
|
+
the stick available to be bound to anything, at anytime.
|
17
|
+
|
18
|
+
h2. Example
|
19
|
+
|
20
|
+
With the later releases of Sinatra, your code is not reload-able in development. You either have to
|
21
|
+
restart your server (which can be very burndensome, especially if you have a long start up time) or
|
22
|
+
you have to run your application through some Shotgun or something similar.
|
23
|
+
|
24
|
+
Here we define a reloader for your route implementations. With this you can pull the logic or your
|
25
|
+
routes out to well-defined components and then reload those components at run-time to get new behavior.
|
26
|
+
|
27
|
+
h3. app.rb
|
28
|
+
|
29
|
+
<pre><code>
|
30
|
+
require 'reloading'
|
31
|
+
|
32
|
+
ReloadableRoutes.stick_to Sinatra::Application
|
33
|
+
|
34
|
+
get "/reload/:file/?" do
|
35
|
+
reload
|
36
|
+
end
|
37
|
+
|
38
|
+
get "/touch_current_user/?" do
|
39
|
+
touch_current_user
|
40
|
+
end
|
41
|
+
|
42
|
+
put "/run_as_someone_else/?" do
|
43
|
+
# is the current user allowed to masquerade?
|
44
|
+
current_user = session[:current_user]
|
45
|
+
raise "unauthorized" unless current_user.allowed_to_masquerade_as_someone_else?
|
46
|
+
|
47
|
+
# let's track the fact that our current user is masquerading
|
48
|
+
touch_current_user
|
49
|
+
|
50
|
+
# do some other marvelous stuff, but let's not argue about the use-cases of such a route
|
51
|
+
|
52
|
+
# finally let's reset the application state for the current user
|
53
|
+
session[:current_user] = current_user
|
54
|
+
end
|
55
|
+
|
56
|
+
['table1', 'table2'].each do |table_name|
|
57
|
+
get table_name do
|
58
|
+
load_records_for(table_name)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
</code></pre>
|
62
|
+
|
63
|
+
h3. reloading.rb
|
64
|
+
|
65
|
+
<pre><code>
|
66
|
+
require 'sticky_blox'
|
67
|
+
|
68
|
+
class ReloadableRoutes
|
69
|
+
include StickyBlox
|
70
|
+
|
71
|
+
stick :reload do
|
72
|
+
# params comes from the instance of the Sinatra application into
|
73
|
+
# which this stick is bound
|
74
|
+
raise "no file specified" unless params[:file]
|
75
|
+
load params[:file]
|
76
|
+
"Success"
|
77
|
+
end
|
78
|
+
|
79
|
+
stick :touch_current_user do
|
80
|
+
# use the instance variables typically available to an instance of a
|
81
|
+
# Sinatra::Application, or subclass, to update some aspect of the system
|
82
|
+
session[:current_user].touch
|
83
|
+
end
|
84
|
+
|
85
|
+
stick :load_records_for {|some_table_name|
|
86
|
+
# again, params comes from the instance of the Sinatra application
|
87
|
+
# into which this stick is bound
|
88
|
+
model_for(some_table_name).filter(params[some_table_name]).all
|
89
|
+
}
|
90
|
+
end
|
91
|
+
</code></pre>
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), 'proc_extension' )
|
2
|
+
|
3
|
+
module StickyBlox
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def stick_to binding
|
10
|
+
(class << self; self; end).instance_eval do
|
11
|
+
clz = self
|
12
|
+
@stuck_methods.each do |route_name|
|
13
|
+
binding.class_eval do
|
14
|
+
define_method route_name do | *args |
|
15
|
+
clz.send(route_name).bind(self).call(*args)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def stick name, &block
|
23
|
+
(class << self; self; end).instance_eval do
|
24
|
+
@stuck_methods ||= []
|
25
|
+
@stuck_methods << name unless @stuck_methods.include?(name)
|
26
|
+
end
|
27
|
+
self.class.class_eval do
|
28
|
+
define_method name do
|
29
|
+
block
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
begin
|
2
|
+
Proc.instance_method(:bind)
|
3
|
+
rescue NameError
|
4
|
+
# Proc instance don't respond to :bind, so let's define it
|
5
|
+
class Proc
|
6
|
+
# this was lifted from the facets project (facets.rubyforge.org)
|
7
|
+
# which in turn was lifted from Rails
|
8
|
+
def bind(object)
|
9
|
+
block, time = self, Time.now
|
10
|
+
(class << object; self; end).class_eval do
|
11
|
+
method_name = "__bind_#{time.to_i}_#{time.usec}"
|
12
|
+
define_method(method_name, &block)
|
13
|
+
method = instance_method(method_name)
|
14
|
+
remove_method(method_name)
|
15
|
+
method
|
16
|
+
end.bind(object)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/sticky_blox.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), 'sticky_blox', 'behavior' )
|
data/spec/helper.rb
ADDED
data/spec/spearmint.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'sticky_blox')
|
2
|
+
|
3
|
+
class Spearmint
|
4
|
+
include StickyBlox
|
5
|
+
|
6
|
+
stick :chew do
|
7
|
+
raise "ah, ah, ah ... I ain't no reverse-able object. I'm a #{self.class}" unless self.respond_to?(:reverse)
|
8
|
+
self.reverse
|
9
|
+
end
|
10
|
+
|
11
|
+
stick :chew_it_up_and_spit_it_out do
|
12
|
+
raise "ah, ah, ah ... I ain't no reverse-able object. I'm a #{self.class}" unless self.respond_to?(:reverse!)
|
13
|
+
self.reverse!
|
14
|
+
end
|
15
|
+
|
16
|
+
stick :open do |how_many_pieces|
|
17
|
+
(1..(how_many_pieces || self).to_i).collect
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe "Instance-based approaches" do
|
4
|
+
class Doublemint < Spearmint
|
5
|
+
end
|
6
|
+
|
7
|
+
it "Doublemint shouldn't have sticks to use" do
|
8
|
+
proc{Doublemint.stick_to Object}.should raise_error(NameError)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should be bound to any string' do
|
12
|
+
Spearmint.stick_to String
|
13
|
+
s = "abc"
|
14
|
+
s1 = s.chew
|
15
|
+
s1.should == s.reverse
|
16
|
+
|
17
|
+
s = "Spearmint"
|
18
|
+
s2 = "tnimraepS"
|
19
|
+
s1 = s.chew_it_up_and_spit_it_out
|
20
|
+
s1.should == s2
|
21
|
+
s1.should === s
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should turn the String into an Integer and return an array of numbers" do
|
25
|
+
thing = "2"
|
26
|
+
thing.open.should == [1, 2]
|
27
|
+
thing.open(5).should == [1, 2, 3, 4, 5]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "Class-based approaches" do
|
32
|
+
it 'should be able to bind to the instance of any object' do
|
33
|
+
s = %w{a b c}
|
34
|
+
s1 = Spearmint.chew.bind(s).call
|
35
|
+
s1.should == %w{c b a}
|
36
|
+
|
37
|
+
s = %w{a b c}
|
38
|
+
s2 = %w{c b a}
|
39
|
+
s1 = Spearmint.chew_it_up_and_spit_it_out.bind(s).call
|
40
|
+
s1.should == s2
|
41
|
+
s1.should == s
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should throw an error when binding to something other than a String" do
|
45
|
+
thing = Object.new
|
46
|
+
proc {Spearmint.chew.bind(thing).call}.should raise_error
|
47
|
+
end
|
48
|
+
end
|
data/sticky_blox.gemspec
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{sticky_blox}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Jason Rogers"]
|
12
|
+
s.date = %q{2010-04-12}
|
13
|
+
s.description = %q{see http://github.com/jacaetevha/sticky_blox for more description. I'm lazy}
|
14
|
+
s.email = %q{jacaetevha@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc",
|
18
|
+
"README.textile",
|
19
|
+
"TODO"
|
20
|
+
]
|
21
|
+
s.files = [
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"README.textile",
|
25
|
+
"TODO",
|
26
|
+
"VERSION",
|
27
|
+
"lib/sticky_blox.rb",
|
28
|
+
"lib/sticky_blox/behavior.rb",
|
29
|
+
"lib/sticky_blox/proc_extension.rb",
|
30
|
+
"spec/helper.rb",
|
31
|
+
"spec/spearmint.rb",
|
32
|
+
"spec/sticky_blox_spec.rb",
|
33
|
+
"sticky_blox.gemspec"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/jacaetevha/sticky_blox}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.6}
|
39
|
+
s.summary = %q{Late-binding traits for Ruby}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/helper.rb",
|
42
|
+
"spec/spearmint.rb",
|
43
|
+
"spec/sticky_blox_spec.rb"
|
44
|
+
]
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
54
|
+
end
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sticky_blox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jason Rogers
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-12 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
version: 1.3.0
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: see http://github.com/jacaetevha/sticky_blox for more description. I'm lazy
|
35
|
+
email: jacaetevha@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
- README.textile
|
44
|
+
- TODO
|
45
|
+
files:
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- README.textile
|
49
|
+
- TODO
|
50
|
+
- VERSION
|
51
|
+
- lib/sticky_blox.rb
|
52
|
+
- lib/sticky_blox/behavior.rb
|
53
|
+
- lib/sticky_blox/proc_extension.rb
|
54
|
+
- spec/helper.rb
|
55
|
+
- spec/spearmint.rb
|
56
|
+
- spec/sticky_blox_spec.rb
|
57
|
+
- sticky_blox.gemspec
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: http://github.com/jacaetevha/sticky_blox
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options:
|
64
|
+
- --charset=UTF-8
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.6
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Late-binding traits for Ruby
|
88
|
+
test_files:
|
89
|
+
- spec/helper.rb
|
90
|
+
- spec/spearmint.rb
|
91
|
+
- spec/sticky_blox_spec.rb
|