Skip to content

Commit

Permalink
Context validation tweaks.
Browse files Browse the repository at this point in the history
Changed required context method name to 'needs.' Updated needs to use
@jbarnette's concise version of needs.

Made validate a noop method instead of checking if a filter respond_to?
validate.
  • Loading branch information
benubois committed Nov 30, 2012
1 parent a044dba commit 011aa07
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/html/pipeline/camo_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def call
# Implementation of validate hook.
# Errors should raise exceptions or use an existing validator.
def validate
context_needs :asset_proxy, :asset_proxy_secret_key
needs :asset_proxy, :asset_proxy_secret_key
end

# The camouflaged URL for a given image URL.
Expand Down
2 changes: 1 addition & 1 deletion lib/html/pipeline/emoji_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def call
# Implementation of validate hook.
# Errors should raise exceptions or use an existing validator.
def validate
context_needs :asset_root
needs :asset_root
end

# Replace :emoji: with corresponding images.
Expand Down
25 changes: 11 additions & 14 deletions lib/html/pipeline/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ def initialize(doc, context = nil, result = nil)
end
@context = context || {}
@result = result || {}

# Filter validation. Sub classes need to implement this method to get
# validated
validate if self.respond_to? :validate
validate
end

# Public: Returns a simple Hash used to pass extra information into filters
Expand Down Expand Up @@ -77,6 +74,10 @@ def html
def call
raise NotImplementedError
end

# Make sure the context has everything we need. Noop: Subclasses can override.
def validate
end

# The Repository object provided in the context hash, or nil when no
# :repository was specified.
Expand Down Expand Up @@ -164,16 +165,12 @@ def self.to_html(input, context = nil)
# If any errors are found an ArgumentError will be raised with a
# message listing all the missing contexts and the filters that
# require them.
def context_needs(*contexts)
contexts = contexts.map { |context| context.to_sym }
errors = []
contexts.each do |context|
unless @context.include? context
errors << "Missing context :#{context.to_s} for #{self.class.name}."
end
end
if errors.any?
raise ArgumentError, errors.join(' ')
def needs(*keys)
missing = keys.reject { |key| context.include? key }

if missing.any?
raise ArgumentError,
"Missing context keys for #{self.class.name}: #{missing.map(&:inspect).join ', '}"
end
end
end
Expand Down

0 comments on commit 011aa07

Please sign in to comment.