Resolve Contracts warning

This commit is contained in:
Thomas Reynolds 2015-12-14 10:22:27 -08:00
parent 308ea6db5a
commit 0e620a1ba1
2 changed files with 21 additions and 10 deletions

View file

@ -10,7 +10,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
option :data, 'locales', 'The directory holding your locale configurations' option :data, 'locales', 'The directory holding your locale configurations'
# Exposes `locales` to templates # Exposes `locales` to templates
expose_to_template :locales, :langs expose_to_template :locales, :langs, :locale, :lang
def initialize(*) def initialize(*)
super super
@ -129,6 +129,14 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
# Backwards API compat # Backwards API compat
alias_method :langs, :locales alias_method :langs, :locales
Contract Symbol
def locale
::I18n.locale
end
# Backwards API compat
alias_method :lang, :locale
# Update the main sitemap resource list # Update the main sitemap resource list
# @return Array<Middleman::Sitemap::Resource> # @return Array<Middleman::Sitemap::Resource>
Contract ResourceList => ResourceList Contract ResourceList => ResourceList

View file

@ -14,8 +14,11 @@ module Middleman
extend Forwardable extend Forwardable
include Contracts include Contracts
# Types which trigger a livereload # Types which could cause output to change.
LIVERELOAD_TYPES = [:source, :locales, :data] OUTPUT_TYPES = [:source, :locales, :data]
# Types which require a reload to eval ruby
CODE_TYPES = [:reload]
Matcher = Or[Regexp, RespondTo[:call]] Matcher = Or[Regexp, RespondTo[:call]]
@ -172,7 +175,7 @@ module Middleman
# @param [String] path The file path. # @param [String] path The file path.
# @param [Boolean] glob If the path contains wildcard or glob characters. # @param [Boolean] glob If the path contains wildcard or glob characters.
# @return [Middleman::SourceFile, nil] # @return [Middleman::SourceFile, nil]
Contract Or[Symbol, ArrayOf[Symbol]], String, Maybe[Bool] => Maybe[SourceFile] Contract Or[Symbol, ArrayOf[Symbol], SetOf[Symbol]], String, Maybe[Bool] => Maybe[SourceFile]
def find(types, path, glob=false) def find(types, path, glob=false)
watchers watchers
.lazy .lazy
@ -187,7 +190,7 @@ module Middleman
# @param [Symbol,Array<Symbol>] types The list of file "type". # @param [Symbol,Array<Symbol>] types The list of file "type".
# @param [String] path The file path relative to it's source root. # @param [String] path The file path relative to it's source root.
# @return [Boolean] # @return [Boolean]
Contract Or[Symbol, ArrayOf[Symbol]], String => Bool Contract Or[Symbol, ArrayOf[Symbol], SetOf[Symbol]], String => Bool
def exists?(types, path) def exists?(types, path)
watchers watchers
.lazy .lazy
@ -200,7 +203,7 @@ module Middleman
# @param [Symbol,Array<Symbol>] types The list of file "type". # @param [Symbol,Array<Symbol>] types The list of file "type".
# @param [String] path The file path relative to it's source root. # @param [String] path The file path relative to it's source root.
# @return [Boolean] # @return [Boolean]
Contract Or[Symbol, ArrayOf[Symbol]], String => Maybe[HANDLER] Contract Or[Symbol, ArrayOf[Symbol], SetOf[Symbol]], String => Maybe[HANDLER]
def watcher_for_path(types, path) def watcher_for_path(types, path)
watchers watchers
.select { |d| Array(types).include?(d.type) } .select { |d| Array(types).include?(d.type) }
@ -243,7 +246,7 @@ module Middleman
# #
# @param [Symbol,Array<Symbol>] types The change types to register the callback. # @param [Symbol,Array<Symbol>] types The change types to register the callback.
# @return [void] # @return [void]
Contract Or[Symbol, ArrayOf[Symbol]], Proc => Any Contract Or[Symbol, ArrayOf[Symbol], SetOf[Symbol]], Proc => Any
def on_change(types, &block) def on_change(types, &block)
Array(types).each do |type| Array(types).each do |type|
@on_change_callbacks = @on_change_callbacks.push(CallbackDescriptor.new(type, block)) @on_change_callbacks = @on_change_callbacks.push(CallbackDescriptor.new(type, block))
@ -255,7 +258,7 @@ module Middleman
# @param [nil,Regexp] matcher A Regexp to match the change path against # @param [nil,Regexp] matcher A Regexp to match the change path against
Contract Maybe[Matcher] => Any Contract Maybe[Matcher] => Any
def changed(matcher=nil, &block) def changed(matcher=nil, &block)
on_change LIVERELOAD_TYPES do |updated, _removed| on_change OUTPUT_TYPES do |updated, _removed|
updated updated
.select { |f| matcher.nil? ? true : matches?(matcher, f) } .select { |f| matcher.nil? ? true : matches?(matcher, f) }
.each { |f| block.call(f[:relative_path]) } .each { |f| block.call(f[:relative_path]) }
@ -267,7 +270,7 @@ module Middleman
# @param [nil,Regexp] matcher A Regexp to match the change path against # @param [nil,Regexp] matcher A Regexp to match the change path against
Contract Maybe[Matcher] => Any Contract Maybe[Matcher] => Any
def deleted(matcher=nil, &block) def deleted(matcher=nil, &block)
on_change LIVERELOAD_TYPES do |_updated, removed| on_change OUTPUT_TYPES do |_updated, removed|
removed removed
.select { |f| matcher.nil? ? true : matches?(matcher, f) } .select { |f| matcher.nil? ? true : matches?(matcher, f) }
.each { |f| block.call(f[:relative_path]) } .each { |f| block.call(f[:relative_path]) }
@ -279,7 +282,7 @@ module Middleman
# @param [Pathname,String] path The path to check. # @param [Pathname,String] path The path to check.
Contract Or[Pathname, String] => Bool Contract Or[Pathname, String] => Bool
def ignored?(path) def ignored?(path)
descriptor = find(LIVERELOAD_TYPES, path) descriptor = find(OUTPUT_TYPES, path)
!descriptor || globally_ignored?(descriptor) !descriptor || globally_ignored?(descriptor)
end end