vendor hooks, add instance hooks, fix configure blocks
This commit is contained in:
parent
3b4f3fbd22
commit
61052de807
19 changed files with 532 additions and 43 deletions
|
@ -1,7 +1,8 @@
|
||||||
require "rack"
|
require "rack"
|
||||||
require "tilt"
|
require "tilt"
|
||||||
require "i18n"
|
require "i18n"
|
||||||
require "hooks"
|
require "middleman/vendor/hooks-0.2.0/lib/hooks"
|
||||||
|
|
||||||
require "active_support"
|
require "active_support"
|
||||||
require "active_support/json"
|
require "active_support/json"
|
||||||
require "active_support/core_ext/string/inflections"
|
require "active_support/core_ext/string/inflections"
|
||||||
|
@ -9,8 +10,7 @@ require "active_support/core_ext/string/inflections"
|
||||||
class Middleman::Base
|
class Middleman::Base
|
||||||
include Hooks
|
include Hooks
|
||||||
define_hook :before
|
define_hook :before
|
||||||
define_hook :build_config
|
define_hook :ready
|
||||||
define_hook :development_config
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def reset!
|
def reset!
|
||||||
|
@ -76,10 +76,6 @@ class Middleman::Base
|
||||||
inst.instance_exec(resolved_template, &block)
|
inst.instance_exec(resolved_template, &block)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def configure(env, &block)
|
|
||||||
send("#{env}_config", &block)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def set(key, value=nil, &block)
|
def set(key, value=nil, &block)
|
||||||
|
@ -89,10 +85,6 @@ class Middleman::Base
|
||||||
send(setter, value)
|
send(setter, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
def configure(env, &block)
|
|
||||||
self.class.configure(env, &block)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Basic Sinatra config
|
# Basic Sinatra config
|
||||||
set :root, Dir.pwd
|
set :root, Dir.pwd
|
||||||
set :source, "source"
|
set :source, "source"
|
||||||
|
|
|
@ -2,7 +2,6 @@ require "thor"
|
||||||
require "thor/group"
|
require "thor/group"
|
||||||
require 'rack/test'
|
require 'rack/test'
|
||||||
require 'find'
|
require 'find'
|
||||||
require 'hooks'
|
|
||||||
|
|
||||||
SHARED_SERVER_INST = Middleman.server.inst do
|
SHARED_SERVER_INST = Middleman.server.inst do
|
||||||
set :environment, :build
|
set :environment, :build
|
||||||
|
@ -35,9 +34,6 @@ module Middleman
|
||||||
class Builder < Thor::Group
|
class Builder < Thor::Group
|
||||||
include Thor::Actions
|
include Thor::Actions
|
||||||
include Middleman::ThorActions
|
include Middleman::ThorActions
|
||||||
include ::Hooks
|
|
||||||
|
|
||||||
define_hook :after_build
|
|
||||||
|
|
||||||
def self.shared_rack
|
def self.shared_rack
|
||||||
@shared_rack ||= begin
|
@shared_rack ||= begin
|
||||||
|
@ -74,12 +70,7 @@ module Middleman
|
||||||
|
|
||||||
action GlobAction.new(self, SHARED_SERVER_INST, opts)
|
action GlobAction.new(self, SHARED_SERVER_INST, opts)
|
||||||
|
|
||||||
run_hook :after_build
|
SHARED_SERVER_INST.run_hook :after_build, self
|
||||||
end
|
|
||||||
|
|
||||||
# Old API
|
|
||||||
def self.after_run(name, &block)
|
|
||||||
after_build(&block)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,13 @@
|
||||||
module Middleman::CoreExtensions::Builder
|
module Middleman::CoreExtensions::Builder
|
||||||
class << self
|
class << self
|
||||||
def registered(app)
|
def registered(app)
|
||||||
|
app.define_hook :after_build
|
||||||
app.extend ClassMethods
|
app.extend ClassMethods
|
||||||
app.send :include, InstanceMethods
|
app.send :include, InstanceMethods
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
# Add a block/proc to be run after features have been setup
|
|
||||||
def after_build(&block)
|
|
||||||
::Middleman::Builder.after_build(&block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_reroute(&block)
|
def build_reroute(&block)
|
||||||
@build_rerouters ||= []
|
@build_rerouters ||= []
|
||||||
@build_rerouters << block if block_given?
|
@build_rerouters << block if block_given?
|
||||||
|
@ -20,10 +16,6 @@ module Middleman::CoreExtensions::Builder
|
||||||
end
|
end
|
||||||
|
|
||||||
module InstanceMethods
|
module InstanceMethods
|
||||||
def after_build(&block)
|
|
||||||
self.class.after_build(&block)
|
|
||||||
end
|
|
||||||
|
|
||||||
def build_reroute(&block)
|
def build_reroute(&block)
|
||||||
self.class.build_reroute(&block)
|
self.class.build_reroute(&block)
|
||||||
end
|
end
|
||||||
|
|
|
@ -35,12 +35,18 @@ module Middleman::CoreExtensions::Features
|
||||||
# app.set :default_features, []
|
# app.set :default_features, []
|
||||||
app.define_hook :after_configuration
|
app.define_hook :after_configuration
|
||||||
app.define_hook :before_configuration
|
app.define_hook :before_configuration
|
||||||
|
app.define_hook :build_config
|
||||||
|
app.define_hook :development_config
|
||||||
app.extend ClassMethods
|
app.extend ClassMethods
|
||||||
app.send :include, InstanceMethods
|
app.send :include, InstanceMethods
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
def configure(env, &block)
|
||||||
|
send("#{env}_config", &block)
|
||||||
|
end
|
||||||
|
|
||||||
def extensions
|
def extensions
|
||||||
@extensions ||= []
|
@extensions ||= []
|
||||||
end
|
end
|
||||||
|
@ -66,6 +72,7 @@ module Middleman::CoreExtensions::Features
|
||||||
#
|
#
|
||||||
# activate MyFeatureModule
|
# activate MyFeatureModule
|
||||||
def activate(feature)
|
def activate(feature)
|
||||||
|
$stderr.puts "Activate: #{feature}"
|
||||||
if feature.is_a? Symbol
|
if feature.is_a? Symbol
|
||||||
feature = feature.to_s
|
feature = feature.to_s
|
||||||
end
|
end
|
||||||
|
@ -78,6 +85,10 @@ module Middleman::CoreExtensions::Features
|
||||||
puts "== Activating: #{feature}" if logging?
|
puts "== Activating: #{feature}" if logging?
|
||||||
self.class.register feature
|
self.class.register feature
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def configure(env, &block)
|
||||||
|
self.class.configure(env, &block)
|
||||||
|
end
|
||||||
|
|
||||||
# Load features before starting server
|
# Load features before starting server
|
||||||
def initialize
|
def initialize
|
||||||
|
@ -91,7 +102,10 @@ module Middleman::CoreExtensions::Features
|
||||||
puts "== Reading: Local config" if logging?
|
puts "== Reading: Local config" if logging?
|
||||||
instance_eval File.read(local_config)
|
instance_eval File.read(local_config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
run_hook :build_config if build?
|
||||||
|
run_hook :development_config if development?
|
||||||
|
|
||||||
run_hook :after_configuration
|
run_hook :after_configuration
|
||||||
|
|
||||||
# Add in defaults
|
# Add in defaults
|
||||||
|
@ -100,10 +114,12 @@ module Middleman::CoreExtensions::Features
|
||||||
end
|
end
|
||||||
|
|
||||||
if logging?
|
if logging?
|
||||||
extensions.each do |ext|
|
self.class.extensions.each do |ext|
|
||||||
puts "== Extension: #{ext}"
|
puts "== Extension: #{ext}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
run_hook :ready
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,7 @@ require "sprockets"
|
||||||
module Middleman::CoreExtensions::Sprockets
|
module Middleman::CoreExtensions::Sprockets
|
||||||
class << self
|
class << self
|
||||||
def registered(app)
|
def registered(app)
|
||||||
|
app.define_hook :before_sprockets
|
||||||
app.set :js_compressor, false
|
app.set :js_compressor, false
|
||||||
app.set :css_compressor, false
|
app.set :css_compressor, false
|
||||||
|
|
||||||
|
@ -20,30 +21,28 @@ module Middleman::CoreExtensions::Sprockets
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
app.after_configuration do
|
app.ready do
|
||||||
js_env = Middleman::CoreExtensions::Sprockets::JavascriptEnvironment.new(self)
|
js_env = Middleman::CoreExtensions::Sprockets::JavascriptEnvironment.new(self)
|
||||||
|
|
||||||
vendor_dir = File.join("vendor", "assets", "javascripts")
|
vendor_dir = File.join("vendor", "assets", "javascripts")
|
||||||
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
|
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
|
||||||
::Middleman.spec_has_file?(spec, vendor_dir)
|
::Middleman.spec_has_file?(spec, vendor_dir)
|
||||||
end.each do |spec|
|
end.each do |spec|
|
||||||
js_env.append_path File.join(spec.full_gem_path, vendor_dir)
|
js_env.append_path File.join(spec.full_gem_path, vendor_dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
app_dir = File.join("app", "assets", "javascripts")
|
app_dir = File.join("app", "assets", "javascripts")
|
||||||
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
|
gems_with_js = ::Middleman.rubygems_latest_specs.select do |spec|
|
||||||
::Middleman.spec_has_file?(spec, app_dir)
|
::Middleman.spec_has_file?(spec, app_dir)
|
||||||
end.each do |spec|
|
end.each do |spec|
|
||||||
js_env.append_path File.join(spec.full_gem_path, app_dir)
|
js_env.append_path File.join(spec.full_gem_path, app_dir)
|
||||||
end
|
end
|
||||||
|
|
||||||
# add paths to js_env (vendor/assets/javascripts)
|
# add paths to js_env (vendor/assets/javascripts)
|
||||||
map "/#{self.js_dir}" do
|
map "/#{self.js_dir}" do
|
||||||
run js_env
|
run js_env
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
app.after_compass_config do
|
|
||||||
css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(self)
|
css_env = Middleman::CoreExtensions::Sprockets::StylesheetEnvironment.new(self)
|
||||||
|
|
||||||
vendor_dir = File.join("vendor", "assets", "stylesheets")
|
vendor_dir = File.join("vendor", "assets", "stylesheets")
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Middleman::Features::MinifyCss
|
||||||
class << self
|
class << self
|
||||||
def registered(app)
|
def registered(app)
|
||||||
require "middleman/features/minify_css/cssmin"
|
require "middleman/features/minify_css/cssmin"
|
||||||
app.before_configuration do
|
app.after_configuration do
|
||||||
set :css_compressor, ::CSSMin
|
set :css_compressor, ::CSSMin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,7 @@ module Middleman::Features::MinifyJavascript
|
||||||
class << self
|
class << self
|
||||||
def registered(app)
|
def registered(app)
|
||||||
require 'uglifier'
|
require 'uglifier'
|
||||||
app.before_configuration do
|
app.after_configuration do
|
||||||
app.set :js_compressor, ::Uglifier.new
|
app.set :js_compressor, ::Uglifier.new
|
||||||
end
|
end
|
||||||
app.use InlineJavascriptRack
|
app.use InlineJavascriptRack
|
||||||
|
|
9
lib/middleman/vendor/hooks-0.2.0/CHANGES.textile
vendored
Normal file
9
lib/middleman/vendor/hooks-0.2.0/CHANGES.textile
vendored
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
h2. 0.2.0
|
||||||
|
|
||||||
|
h3. Changes
|
||||||
|
* Callback blocks are now executed on the instance using @instance_exec@. If you need to access the class (former context) use @self.class@.
|
||||||
|
|
||||||
|
h2. 0.1.4
|
||||||
|
|
||||||
|
h3. Bugfixes
|
||||||
|
* An uninitialized @inheritable_attr@ doesn't crash since it is not cloned anymore. Note that an uncloneable attribute value still causes an exception.
|
3
lib/middleman/vendor/hooks-0.2.0/Gemfile
vendored
Normal file
3
lib/middleman/vendor/hooks-0.2.0/Gemfile
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
source :rubygems
|
||||||
|
|
||||||
|
gemspec
|
107
lib/middleman/vendor/hooks-0.2.0/README.rdoc
vendored
Normal file
107
lib/middleman/vendor/hooks-0.2.0/README.rdoc
vendored
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
= Hooks
|
||||||
|
|
||||||
|
<em>Generic hooks with callbacks for Ruby.</em>
|
||||||
|
|
||||||
|
|
||||||
|
== Introduction
|
||||||
|
|
||||||
|
_Hooks_ lets you define hooks declaratively in your ruby class. You can add callbacks to your hook, which will be run as soon as _you_ run the hook!
|
||||||
|
|
||||||
|
It's almost like ActiveSupport::Callbacks but 76,6% less complex. Instead, it is not more than 60 lines of code, one method compilation, no +method_missing+ and no magic.
|
||||||
|
|
||||||
|
Also, you may pass additional arguments to your callbacks when invoking a hook.
|
||||||
|
|
||||||
|
== Example
|
||||||
|
|
||||||
|
Let's take... a cat.
|
||||||
|
|
||||||
|
require 'hooks'
|
||||||
|
|
||||||
|
class Cat
|
||||||
|
include Hooks
|
||||||
|
|
||||||
|
define_hook :after_dinner
|
||||||
|
|
||||||
|
Now you can add callbacks to your hook declaratively in your class.
|
||||||
|
|
||||||
|
after_dinner do
|
||||||
|
puts "Ice cream for #{self}!"
|
||||||
|
end
|
||||||
|
|
||||||
|
after_dinner :have_a_desert # => refers to Cat#have_a_desert
|
||||||
|
|
||||||
|
def have_a_desert
|
||||||
|
puts "Hell, yeah!"
|
||||||
|
end
|
||||||
|
|
||||||
|
This will run the block and <tt>#have_a_desert</tt> from above.
|
||||||
|
|
||||||
|
cat.run_hook :after_dinner
|
||||||
|
# => Ice cream for #<Cat:0x8df9d84>!
|
||||||
|
Hell, yeah!
|
||||||
|
|
||||||
|
Callback blocks and methods will be executed with instance context. Note how +self+ in the block refers to the Cat instance.
|
||||||
|
|
||||||
|
|
||||||
|
== Inheritance
|
||||||
|
|
||||||
|
Hooks are inherited, here's a complete example to put it all together.
|
||||||
|
|
||||||
|
class Garfield < Cat
|
||||||
|
|
||||||
|
after_dinner :want_some_more
|
||||||
|
|
||||||
|
def want_some_more
|
||||||
|
puts "Is that all?"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Garfield.new.run_hook :after_dinner
|
||||||
|
# => Ice cream for #<Cat:0x8df9d84>!
|
||||||
|
Hell, yeah!
|
||||||
|
Is that all?
|
||||||
|
|
||||||
|
Note how the callbacks are invoked in the order they were inherited.
|
||||||
|
|
||||||
|
|
||||||
|
== Options for Callbacks
|
||||||
|
|
||||||
|
You're free to pass any number of arguments to #run_callback, those will be passed to the callbacks.
|
||||||
|
|
||||||
|
cat.run_hook :before_dinner, cat, Time.now
|
||||||
|
|
||||||
|
The callbacks should be ready for receiving parameters.
|
||||||
|
|
||||||
|
before_dinner :wash_pawns
|
||||||
|
before_dinner do |who, when|
|
||||||
|
...
|
||||||
|
end
|
||||||
|
|
||||||
|
def wash_pawns(who, when)
|
||||||
|
|
||||||
|
|
||||||
|
Not sure why a cat should have ice cream for dinner. Beside that, I was tempted naming this gem _hooker_.
|
||||||
|
|
||||||
|
|
||||||
|
== Installation
|
||||||
|
|
||||||
|
gem install hooks
|
||||||
|
|
||||||
|
|
||||||
|
== Anybody using it?
|
||||||
|
|
||||||
|
* Hooks is already used in [Apotomo:http://github.com/apotonick/apotomo], a hot widget framework for Rails. Look at +lib/apotomo/widget.rb+ for examples and into +lib/apotomo/tree_node.rb+ to learn how modules-driven code might benefit from hooks.
|
||||||
|
|
||||||
|
== Similar libraries
|
||||||
|
|
||||||
|
* http://github.com/nakajima/aspectory
|
||||||
|
* http://github.com/auser/backcall
|
||||||
|
* http://github.com/mmcgrana/simple_callbacks
|
||||||
|
|
||||||
|
|
||||||
|
== License
|
||||||
|
|
||||||
|
Copyright (c) 2010, Nick Sutterer
|
||||||
|
|
||||||
|
Released under the MIT License.
|
12
lib/middleman/vendor/hooks-0.2.0/Rakefile
vendored
Normal file
12
lib/middleman/vendor/hooks-0.2.0/Rakefile
vendored
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
require 'rake'
|
||||||
|
require 'rake/testtask'
|
||||||
|
|
||||||
|
desc 'Default: run unit tests.'
|
||||||
|
task :default => :test
|
||||||
|
|
||||||
|
desc 'Test the hooks plugin.'
|
||||||
|
Rake::TestTask.new(:test) do |test|
|
||||||
|
test.libs << 'test'
|
||||||
|
test.test_files = FileList['test/*_test.rb']
|
||||||
|
test.verbose = true
|
||||||
|
end
|
22
lib/middleman/vendor/hooks-0.2.0/hooks.gemspec
vendored
Normal file
22
lib/middleman/vendor/hooks-0.2.0/hooks.gemspec
vendored
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
lib = File.expand_path('../lib/', __FILE__)
|
||||||
|
$:.unshift lib unless $:.include?(lib)
|
||||||
|
|
||||||
|
require 'hooks'
|
||||||
|
|
||||||
|
Gem::Specification.new do |s|
|
||||||
|
s.name = "hooks"
|
||||||
|
s.version = Hooks::VERSION
|
||||||
|
s.platform = Gem::Platform::RUBY
|
||||||
|
s.authors = ["Nick Sutterer"]
|
||||||
|
s.email = ["apotonick@gmail.com"]
|
||||||
|
s.homepage = "http://nicksda.apotomo.de/tag/hooks"
|
||||||
|
s.summary = %q{Generic hooks with callbacks for Ruby.}
|
||||||
|
s.description = %q{Declaratively define hooks, add callbacks and run them with the options you like.}
|
||||||
|
|
||||||
|
s.files = `git ls-files`.split("\n")
|
||||||
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
||||||
|
s.require_paths = ["lib"]
|
||||||
|
|
||||||
|
s.add_development_dependency "shoulda"
|
||||||
|
s.add_development_dependency "rake"
|
||||||
|
end
|
109
lib/middleman/vendor/hooks-0.2.0/lib/hooks.rb
vendored
Normal file
109
lib/middleman/vendor/hooks-0.2.0/lib/hooks.rb
vendored
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
require File.join(File.dirname(__FILE__), "hooks/inheritable_attribute")
|
||||||
|
|
||||||
|
# Almost like ActiveSupport::Callbacks but 76,6% less complex.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# class CatWidget < Apotomo::Widget
|
||||||
|
# define_hook :after_dinner
|
||||||
|
#
|
||||||
|
# Now you can add callbacks to your hook declaratively in your class.
|
||||||
|
#
|
||||||
|
# after_dinner do puts "Ice cream!" end
|
||||||
|
# after_dinner :have_a_desert # => refers to CatWidget#have_a_desert
|
||||||
|
#
|
||||||
|
# Running the callbacks happens on instances. It will run the block and #have_a_desert from above.
|
||||||
|
#
|
||||||
|
# cat.run_hook :after_dinner
|
||||||
|
module Hooks
|
||||||
|
VERSION = "0.2.0"
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.extend InheritableAttribute
|
||||||
|
base.extend ClassMethods
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def define_hook(name)
|
||||||
|
accessor_name = "_#{name}_callbacks"
|
||||||
|
|
||||||
|
setup_hook_accessors(accessor_name)
|
||||||
|
define_hook_writer(name, accessor_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Like Hooks#run_hook but for the class. Note that +:callbacks+ must be class methods.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# class Cat
|
||||||
|
# after_eight :grab_a_beer
|
||||||
|
#
|
||||||
|
# def self.grab_a_beer(*) # and so on...
|
||||||
|
#
|
||||||
|
# where <tt>Cat.run_hook :after_eight</tt> will call the class method +grab_a_beer+.
|
||||||
|
def run_hook(name, *args)
|
||||||
|
run_hook_for(name, self, *args)
|
||||||
|
end
|
||||||
|
|
||||||
|
def run_hook_for(name, scope, *args)
|
||||||
|
callbacks_for_hook(name).each do |callback|
|
||||||
|
if callback.kind_of? Symbol
|
||||||
|
scope.send(callback, *args)
|
||||||
|
else
|
||||||
|
scope.instance_exec(*args, &callback)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns the callbacks for +name+. Handy if you want to run the callbacks yourself, say when
|
||||||
|
# they should be executed in another context.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# def initialize
|
||||||
|
# self.class.callbacks_for_hook(:after_eight).each do |callback|
|
||||||
|
# instance_exec(self, &callback)
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# would run callbacks in the object _instance_ context, passing +self+ as block parameter.
|
||||||
|
def callbacks_for_hook(name)
|
||||||
|
send("_#{name}_callbacks")
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def define_hook_writer(hook, accessor_name)
|
||||||
|
self.send(:define_method, hook.to_sym) do |&block|
|
||||||
|
if self.class.respond_to?(hook)
|
||||||
|
self.class.send(hook.to_sym, &block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
instance_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
|
||||||
|
def #{hook}(method=nil, &block)
|
||||||
|
#{accessor_name} << (block || method)
|
||||||
|
end
|
||||||
|
RUBY_EVAL
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup_hook_accessors(accessor_name)
|
||||||
|
inheritable_attr(accessor_name)
|
||||||
|
send("#{accessor_name}=", []) # initialize ivar.
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Runs the callbacks (method/block) for the specified hook +name+. Additional arguments will
|
||||||
|
# be passed to the callback.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# cat.run_hook :after_dinner, "i want ice cream!"
|
||||||
|
#
|
||||||
|
# will invoke the callbacks like
|
||||||
|
#
|
||||||
|
# desert("i want ice cream!")
|
||||||
|
# block.call("i want ice cream!")
|
||||||
|
def run_hook(name, *args)
|
||||||
|
self.class.run_hook_for(name, self, *args)
|
||||||
|
end
|
||||||
|
end
|
33
lib/middleman/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
vendored
Normal file
33
lib/middleman/vendor/hooks-0.2.0/lib/hooks/inheritable_attribute.rb
vendored
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
module Hooks
|
||||||
|
module InheritableAttribute
|
||||||
|
# Creates an inheritable attribute with accessors in the singleton class. Derived classes inherit the
|
||||||
|
# attributes. This is especially helpful with arrays or hashes that are extended in the inheritance
|
||||||
|
# chain. Note that you have to initialize the inheritable attribute.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
#
|
||||||
|
# class Cat
|
||||||
|
# inheritable_attr :drinks
|
||||||
|
# self.drinks = ["Becks"]
|
||||||
|
#
|
||||||
|
# class Garfield < Cat
|
||||||
|
# self.drinks << "Fireman's 4"
|
||||||
|
#
|
||||||
|
# and then, later
|
||||||
|
#
|
||||||
|
# Cat.drinks #=> ["Becks"]
|
||||||
|
# Garfield.drinks #=> ["Becks", "Fireman's 4"]
|
||||||
|
def inheritable_attr(name)
|
||||||
|
instance_eval %Q{
|
||||||
|
def #{name}=(v)
|
||||||
|
@#{name} = v
|
||||||
|
end
|
||||||
|
|
||||||
|
def #{name}
|
||||||
|
return @#{name} unless superclass.respond_to?(:#{name}) and value = superclass.#{name}
|
||||||
|
@#{name} ||= value.clone # only do this once.
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
141
lib/middleman/vendor/hooks-0.2.0/test/hooks_test.rb
vendored
Normal file
141
lib/middleman/vendor/hooks-0.2.0/test/hooks_test.rb
vendored
Normal file
|
@ -0,0 +1,141 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class HooksTest < Test::Unit::TestCase
|
||||||
|
class TestClass
|
||||||
|
include Hooks
|
||||||
|
|
||||||
|
def executed
|
||||||
|
@executed ||= [];
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
context "Hooks.define_hook" do
|
||||||
|
setup do
|
||||||
|
@klass = Class.new(TestClass)
|
||||||
|
|
||||||
|
@mum = @klass.new
|
||||||
|
@mum.class.define_hook :after_eight
|
||||||
|
end
|
||||||
|
|
||||||
|
should "provide accessors to the stored callbacks" do
|
||||||
|
assert_equal [], @klass._after_eight_callbacks
|
||||||
|
@klass._after_eight_callbacks << :dine
|
||||||
|
assert_equal [:dine], @klass._after_eight_callbacks
|
||||||
|
end
|
||||||
|
|
||||||
|
should "respond to Class.callbacks_for_hook" do
|
||||||
|
assert_equal [], @klass.callbacks_for_hook(:after_eight)
|
||||||
|
@klass.after_eight :dine
|
||||||
|
assert_equal [:dine], @klass.callbacks_for_hook(:after_eight)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "creates a public writer for the hook that" do
|
||||||
|
should "accepts method names" do
|
||||||
|
@klass.after_eight :dine
|
||||||
|
assert_equal [:dine], @klass._after_eight_callbacks
|
||||||
|
end
|
||||||
|
|
||||||
|
should "accepts blocks" do
|
||||||
|
@klass.after_eight do true; end
|
||||||
|
assert @klass._after_eight_callbacks.first.kind_of? Proc
|
||||||
|
end
|
||||||
|
|
||||||
|
should "be inherited" do
|
||||||
|
@klass.after_eight :dine
|
||||||
|
subklass = Class.new(@klass)
|
||||||
|
|
||||||
|
assert_equal [:dine], subklass._after_eight_callbacks
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "Hooks#run_hook" do
|
||||||
|
should "run without parameters" do
|
||||||
|
@mum.instance_eval do
|
||||||
|
def a; executed << :a; nil; end
|
||||||
|
def b; executed << :b; end
|
||||||
|
|
||||||
|
self.class.after_eight :b
|
||||||
|
self.class.after_eight :a
|
||||||
|
end
|
||||||
|
|
||||||
|
@mum.run_hook(:after_eight)
|
||||||
|
|
||||||
|
assert_equal [:b, :a], @mum.executed
|
||||||
|
end
|
||||||
|
|
||||||
|
should "accept arbitrary parameters" do
|
||||||
|
@mum.instance_eval do
|
||||||
|
def a(me, arg); executed << arg+1; end
|
||||||
|
end
|
||||||
|
@mum.class.after_eight :a
|
||||||
|
@mum.class.after_eight lambda { |me, arg| me.executed << arg-1 }
|
||||||
|
|
||||||
|
@mum.run_hook(:after_eight, @mum, 1)
|
||||||
|
|
||||||
|
assert_equal [2, 0], @mum.executed
|
||||||
|
end
|
||||||
|
|
||||||
|
should "execute block callbacks in instance context" do
|
||||||
|
@mum.class.after_eight { executed << :c }
|
||||||
|
@mum.run_hook(:after_eight)
|
||||||
|
assert_equal [:c], @mum.executed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "in class context" do
|
||||||
|
should "run a callback block" do
|
||||||
|
executed = []
|
||||||
|
@klass.after_eight do
|
||||||
|
executed << :klass
|
||||||
|
end
|
||||||
|
@klass.run_hook :after_eight
|
||||||
|
|
||||||
|
assert_equal [:klass], executed
|
||||||
|
end
|
||||||
|
|
||||||
|
should "run a class methods" do
|
||||||
|
executed = []
|
||||||
|
@klass.instance_eval do
|
||||||
|
after_eight :have_dinner
|
||||||
|
|
||||||
|
def have_dinner(executed)
|
||||||
|
executed << :have_dinner
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@klass.run_hook :after_eight, executed
|
||||||
|
|
||||||
|
assert_equal [:have_dinner], executed
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "Deriving" do
|
||||||
|
setup do
|
||||||
|
@klass = Class.new(TestClass)
|
||||||
|
|
||||||
|
@mum = @klass.new
|
||||||
|
@mum.class.define_hook :after_eight
|
||||||
|
end
|
||||||
|
|
||||||
|
should "inherit the hook" do
|
||||||
|
@klass.class_eval do
|
||||||
|
after_eight :take_shower
|
||||||
|
|
||||||
|
def take_shower
|
||||||
|
executed << :take_shower
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@kid = Class.new(@klass) do
|
||||||
|
after_eight :have_dinner
|
||||||
|
|
||||||
|
def have_dinner
|
||||||
|
executed << :have_dinner
|
||||||
|
end
|
||||||
|
end.new
|
||||||
|
|
||||||
|
assert_equal [:take_shower, :have_dinner], @kid.run_hook(:after_eight)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
55
lib/middleman/vendor/hooks-0.2.0/test/inheritable_attribute_test.rb
vendored
Normal file
55
lib/middleman/vendor/hooks-0.2.0/test/inheritable_attribute_test.rb
vendored
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class HooksTest < Test::Unit::TestCase
|
||||||
|
context "Hooks.define_hook" do
|
||||||
|
setup do
|
||||||
|
@klass = Class.new(Object) do
|
||||||
|
extend Hooks::InheritableAttribute
|
||||||
|
end
|
||||||
|
|
||||||
|
@mum = @klass.new
|
||||||
|
@klass.inheritable_attr :drinks
|
||||||
|
end
|
||||||
|
|
||||||
|
should "provide a reader with empty inherited attributes, already" do
|
||||||
|
assert_equal nil, @klass.drinks
|
||||||
|
end
|
||||||
|
|
||||||
|
should "provide a reader with empty inherited attributes in a derived class" do
|
||||||
|
assert_equal nil, Class.new(@klass).drinks
|
||||||
|
#@klass.drinks = true
|
||||||
|
#Class.new(@klass).drinks # TODO: crashes.
|
||||||
|
end
|
||||||
|
|
||||||
|
should "provide an attribute copy in subclasses" do
|
||||||
|
@klass.drinks = []
|
||||||
|
assert @klass.drinks.object_id != Class.new(@klass).drinks.object_id
|
||||||
|
end
|
||||||
|
|
||||||
|
should "provide a writer" do
|
||||||
|
@klass.drinks = [:cabernet]
|
||||||
|
assert_equal [:cabernet], @klass.drinks
|
||||||
|
end
|
||||||
|
|
||||||
|
should "inherit attributes" do
|
||||||
|
@klass.drinks = [:cabernet]
|
||||||
|
|
||||||
|
subklass_a = Class.new(@klass)
|
||||||
|
subklass_a.drinks << :becks
|
||||||
|
|
||||||
|
subklass_b = Class.new(@klass)
|
||||||
|
|
||||||
|
assert_equal [:cabernet], @klass.drinks
|
||||||
|
assert_equal [:cabernet, :becks], subklass_a.drinks
|
||||||
|
assert_equal [:cabernet], subklass_b.drinks
|
||||||
|
end
|
||||||
|
|
||||||
|
should "not inherit attributes if we set explicitely" do
|
||||||
|
@klass.drinks = [:cabernet]
|
||||||
|
subklass = Class.new(@klass)
|
||||||
|
|
||||||
|
subklass.drinks = [:merlot] # we only want merlot explicitely.
|
||||||
|
assert_equal [:merlot], subklass.drinks # no :cabernet, here
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
10
lib/middleman/vendor/hooks-0.2.0/test/test_helper.rb
vendored
Normal file
10
lib/middleman/vendor/hooks-0.2.0/test/test_helper.rb
vendored
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
require 'rubygems'
|
||||||
|
|
||||||
|
# wycats says...
|
||||||
|
require 'bundler'
|
||||||
|
Bundler.setup
|
||||||
|
require 'test/unit'
|
||||||
|
require 'shoulda'
|
||||||
|
require 'hooks'
|
||||||
|
|
||||||
|
$:.unshift File.dirname(__FILE__) # add current dir to LOAD_PATHS
|
|
@ -35,7 +35,6 @@ Gem::Specification.new do |s|
|
||||||
s.add_dependency("execjs", ["~> 1.2.7"])
|
s.add_dependency("execjs", ["~> 1.2.7"])
|
||||||
s.add_dependency("sprockets", ["~> 2.0"])
|
s.add_dependency("sprockets", ["~> 2.0"])
|
||||||
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
|
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
|
||||||
s.add_dependency("hooks", ["~> 0.2.0"])
|
|
||||||
s.add_dependency("guard", ["~> 0.8.8"])
|
s.add_dependency("guard", ["~> 0.8.8"])
|
||||||
|
|
||||||
s.add_dependency("eventmachine", ["1.0.0.beta.4.1"])
|
s.add_dependency("eventmachine", ["1.0.0.beta.4.1"])
|
||||||
|
|
|
@ -35,7 +35,6 @@ Gem::Specification.new do |s|
|
||||||
s.add_dependency("execjs", ["~> 1.2.7"])
|
s.add_dependency("execjs", ["~> 1.2.7"])
|
||||||
s.add_dependency("sprockets", ["~> 2.0"])
|
s.add_dependency("sprockets", ["~> 2.0"])
|
||||||
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
|
s.add_dependency("sprockets-sass", ["~> 0.3.0"])
|
||||||
s.add_dependency("hooks", ["~> 0.2.0"])
|
|
||||||
s.add_dependency("guard", ["~> 0.8.8"])
|
s.add_dependency("guard", ["~> 0.8.8"])
|
||||||
|
|
||||||
# OSX
|
# OSX
|
||||||
|
|
Loading…
Reference in a new issue