-
-
Notifications
You must be signed in to change notification settings - Fork 382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Place Dependency Management On Filters #77
Comments
Here is a module HTML
module DependencyManagement
def dependency(path, gem=nil)
require path
rescue LoadError => error
# http://ruby-doc.org/core-1.9.3/LoadError.html
# Possibilities:
# > require 'this/file/does/not/exist'
# LoadError: no such file to load -- this/file/does/not/exist
missing_gem = gem.nil? ? path : gem
self.load_error = "missing dependency for #{self}: #{error.message}\n" +
"add `gem '#{missing_gem}'` to your Gemfile, or `gem install #{missing_gem}`"
rescue NameError => error
# We rescue NameError incase the required dependency has another dependency
# which has not been required e.g. `uninitialized constant AnotherDependency`
#
# http://ruby-doc.org/core-1.9.3/NameError.html
# Possibilities:
# > puts foo
# NameError: undefined local variable or method `foo' for main:Object
# > Fixnum.const_set :answer, 42
# NameError: wrong constant name answer
self.load_error = "unable to require #{path} dependency for #{self}: #{error.message}"
end
def new(*)
raise load_error unless loaded?
super
end
def loaded?
load_error.nil?
end
def load_error
@load_error
end
private
def load_error=(load_error)
@load_error = load_error
end
end
end
module HTML
class Filter
extend DependencyManagement
dependency 'rake'
dependency 'net/http/persistent', 'net-http-persistent'
end
end
HTML::Filter.new Here are a couple examples of messages raised.
|
I remember seeing Faraday's dependency management DSL. I'll sleep on it tonight, but I feel it's a bit overkill. Yes, it would save a few lines at the top of each filter class, but it's just a glorified dependency 'rake', '~> 0.10'
dependency 'net-http-persistent', :require => 'net/http/persistent' How do you feel about that? Even more overkill? By the way, however we release this change, I'll probably bump it to version 1.0. The library's been stable for a long time, and most pulls have been docs related. This would be a big enough change to push it over. |
I could agree with overkill. Here is a simplistic dependency management implementation from em-synchrony. The proposed prototype DRYs the error messages, but you could just use a constant. # filter.rb
module HTML
class Pipeline
NOTICE_MISSING_GEM = "Missing html-pipeline dependency: gem install %s"
end
end
# foo_filter.rb
begin
require "net/http/persistent"
rescue LoadError => error
raise sprintf(HTML::Pipeline::NOTICE_MISSING_GEM, "net-http-persistent")
# => "Missing html-pipeline dependency: gem install net-http-persistent"
end
module HTML
class Foo < Filter
end
end I really like the
Plus, Filters with many dependencies will not repeat That said, only Semver is super interesting, but it sounds like even more overkill. 😄 Awesome! 1.0 ✨ |
Hmm, maybe we can integrate semver into the simple dependency management, at least the version syntax. # filter.rb
module HTML
class Pipeline
NOTICE_MISSING_GEM = %Q(Missing html-pipeline dependency.\nPlease add `gem "%s", "%s"` to your Gemfile.)
end
end
# foo_filter.rb
begin
require "net/http/persistent"
rescue LoadError => error
raise sprintf(HTML::Pipeline::NOTICE_MISSING_GEM, "net-http-persistent", "~> 2.9")
# => "Missing html-pipeline dependency.\nPlease add `gem \"net-http-persistent\", \"~> 2.9\"` to your Gemfile."
end
module HTML
class Foo < Filter
end
end Initially, we could hard-code the versions. For this release or a subsequent release, we could add a build task which introspects the Gemfile Thoughts? |
Here's some notes about the existing dependencies: Required
Optional
|
I'm 👎 on any sort of module or magic beyond a line or two of ruby to handle dependancies here. Feels like overkill given how simple our dependencies are for this stuff. |
After weighing the options, I agree with @rsanheim's and @jch's initial thoughts. Keep it simple.
What about the exception message? I prefer the clear message with steps to fix.
|
Sounds good! Looking forwards to the pull. On Wednesday, August 21, 2013, Simeon Willbanks wrote:
-Jerry |
For sure. I'll get started. |
#48 kickstarted discussion, and here is a plan for placing dependency management on Filters.
message
:test
blockmanagement
The text was updated successfully, but these errors were encountered: