Update contracts gem. #1494

This commit is contained in:
Thomas Reynolds 2015-04-24 10:26:42 -07:00
parent 4740159a3a
commit 55c5a46440
17 changed files with 61 additions and 94 deletions

View file

@ -29,8 +29,8 @@ gem 'therubyracer', '>= 0.12', platforms: :ruby
# Code Quality # Code Quality
gem 'rubocop', '~> 0.24', require: false gem 'rubocop', '~> 0.24', require: false
gem 'simplecov', '0.9', require: false gem 'simplecov', '~> 0.9', require: false
gem 'coveralls', '~> 0.7', require: false gem 'coveralls', '~> 0.8', require: false
gem 'codeclimate-test-reporter', '~> 0.3', require: false, group: :test gem 'codeclimate-test-reporter', '~> 0.3', require: false, group: :test
# Middleman itself # Middleman itself

View file

@ -30,6 +30,9 @@ require 'middleman-core/template_renderer'
# Core Middleman Class # Core Middleman Class
module Middleman module Middleman
MiddlewareDescriptor = Struct.new(:class, :options, :block)
MapDescriptor = Struct.new(:path, :block)
class Application class Application
extend Forwardable extend Forwardable
include Contracts include Contracts
@ -180,10 +183,10 @@ module Middleman
attr_reader :extensions attr_reader :extensions
attr_reader :sources attr_reader :sources
Contract None => SetOf['Middleman::Application::MiddlewareDescriptor'] Contract SetOf[MiddlewareDescriptor]
attr_reader :middleware attr_reader :middleware
Contract None => SetOf['Middleman::Application::MapDescriptor'] Contract SetOf[MapDescriptor]
attr_reader :mappings attr_reader :mappings
# Reference to Logger singleton # Reference to Logger singleton
@ -343,19 +346,15 @@ module Middleman
Pathname(File.join(root, config[:source])) Pathname(File.join(root, config[:source]))
end end
MiddlewareDescriptor = Struct.new(:class, :options, :block)
# Use Rack middleware # Use Rack middleware
# #
# @param [Class] middleware Middleware module # @param [Class] middleware Middleware module
# @return [void] # @return [void]
Contract Any, Args[Any], Maybe[Proc] => Any # Contract Any, Args[Any], Maybe[Proc] => Any
def use(middleware, *args, &block) def use(middleware, *args, &block)
@middleware << MiddlewareDescriptor.new(middleware, args, block) @middleware << MiddlewareDescriptor.new(middleware, args, block)
end end
MapDescriptor = Struct.new(:path, :block)
# Add Rack App mapped to specific path # Add Rack App mapped to specific path
# #
# @param [String] map Path to map # @param [String] map Path to map

View file

@ -44,7 +44,7 @@ module Middleman
# Run the build phase. # Run the build phase.
# @return [Boolean] Whether the build was successful. # @return [Boolean] Whether the build was successful.
Contract None => Bool Contract Bool
def run! def run!
@has_error = false @has_error = false
@events = {} @events = {}
@ -77,7 +77,7 @@ module Middleman
# Pre-request CSS to give Compass a chance to build sprites # Pre-request CSS to give Compass a chance to build sprites
# @return [Array<Resource>] List of css resources that were output. # @return [Array<Resource>] List of css resources that were output.
Contract None => ResourceList Contract ResourceList
def prerender_css def prerender_css
logger.debug '== Prerendering CSS' logger.debug '== Prerendering CSS'
@ -96,7 +96,7 @@ module Middleman
# Find all the files we need to output and do so. # Find all the files we need to output and do so.
# @return [Array<Resource>] List of resources that were output. # @return [Array<Resource>] List of resources that were output.
Contract None => ResourceList Contract ResourceList
def output_files def output_files
logger.debug '== Building files' logger.debug '== Building files'
@ -209,7 +209,7 @@ module Middleman
# Get a list of all the paths in the destination folder and save them # Get a list of all the paths in the destination folder and save them
# for comparison against the files we build in this cycle # for comparison against the files we build in this cycle
# @return [void] # @return [void]
Contract None => Any Contract Any
def queue_current_paths def queue_current_paths
@to_clean = [] @to_clean = []
@ -234,7 +234,7 @@ module Middleman
end end
# Remove files which were not built in this cycle # Remove files which were not built in this cycle
Contract None => ArrayOf[Pathname] Contract ArrayOf[Pathname]
def clean def clean
@to_clean.each do |f| @to_clean.each do |f|
FileUtils.rm(f) FileUtils.rm(f)

View file

@ -27,38 +27,6 @@ if ENV['TEST'] || ENV['CONTRACTS'] == 'true'
end end
end end
class ArrayOf
def initialize(contract)
@contract = contract.is_a?(String) ? IsA[contract] : contract
end
end
class SetOf < CallableClass
def initialize(contract)
@contract = contract.is_a?(String) ? IsA[contract] : contract
end
def valid?(vals)
return false unless vals.is_a?(Set)
vals.all? do |val|
res, _ = Contract.valid?(val, @contract)
res
end
end
def to_s
"a set of #{@contract}"
end
def testable?
Testable.testable? @contract
end
def test_data
Set.new([], [Testable.test_data(@contract)], [Testable.test_data(@contract), Testable.test_data(@contract)])
end
end
# class MethodDefined # class MethodDefined
# def self.[](val) # def self.[](val)
# @lookup ||= {} # @lookup ||= {}

View file

@ -28,7 +28,7 @@ module Middleman
@root_collector = LazyCollectorRoot.new(self) @root_collector = LazyCollectorRoot.new(self)
end end
Contract None => Any Contract Any
def before_configuration def before_configuration
@leaves.clear @leaves.clear

View file

@ -195,7 +195,7 @@ module Middleman
# Convert all the data into a static hash # Convert all the data into a static hash
# #
# @return [Hash] # @return [Hash]
Contract None => Hash Contract Hash
def to_h def to_h
data = {} data = {}

View file

@ -6,7 +6,7 @@ module Middleman
# API for watching file change events # API for watching file change events
class FileWatcher < Extension class FileWatcher < Extension
# All defined sources. # All defined sources.
Contract None => IsA['Middleman::Sources'] Contract IsA['Middleman::Sources']
attr_reader :sources attr_reader :sources
# The default list of ignores. # The default list of ignores.
@ -43,7 +43,7 @@ module Middleman
# Before we config, find initial files. # Before we config, find initial files.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def before_configuration def before_configuration
@sources.find_new_files! @sources.find_new_files!
end end
@ -51,7 +51,7 @@ module Middleman
# After we config, find new files since config can change paths. # After we config, find new files since config can change paths.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def after_configuration def after_configuration
if @original_source_dir != app.config[:source] if @original_source_dir != app.config[:source]
@watcher.update_path(app.config[:source]) @watcher.update_path(app.config[:source])

View file

@ -75,7 +75,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
end end
end end
Contract None => ArrayOf[Symbol] Contract ArrayOf[Symbol]
def langs def langs
@langs ||= known_languages @langs ||= known_languages
end end
@ -130,7 +130,7 @@ class Middleman::CoreExtensions::Internationalization < ::Middleman::Extension
::I18n.fallbacks = ::I18n::Locale::Fallbacks.new if ::I18n.respond_to?(:fallbacks) ::I18n.fallbacks = ::I18n::Locale::Fallbacks.new if ::I18n.respond_to?(:fallbacks)
end end
Contract None => ArrayOf[Symbol] Contract ArrayOf[Symbol]
def known_languages def known_languages
if options[:langs] if options[:langs]
Array(options[:langs]).map(&:to_sym) Array(options[:langs]).map(&:to_sym)

View file

@ -102,7 +102,7 @@ module Middleman
# Get the template data from a path # Get the template data from a path
# @param [String] path # @param [String] path
# @return [String] # @return [String]
Contract None => String Contract String
def template_data_for_file def template_data_for_file
if @app.extensions[:front_matter] if @app.extensions[:front_matter]
@app.extensions[:front_matter].template_data_for_file(@path) || '' @app.extensions[:front_matter].template_data_for_file(@path) || ''

View file

@ -22,7 +22,7 @@ module Middleman
end end
end end
Contract None => Any Contract Any
def before_configuration def before_configuration
app.files.on_change(:source, &method(:update_files)) app.files.on_change(:source, &method(:update_files))
end end

View file

@ -96,7 +96,7 @@ module Middleman
# The resource for the page this page is proxied to. Throws an exception # The resource for the page this page is proxied to. Throws an exception
# if there is no resource. # if there is no resource.
# @return [Sitemap::Resource] # @return [Sitemap::Resource]
Contract None => IsA['Middleman::Sitemap::Resource'] Contract IsA['Middleman::Sitemap::Resource']
def target_resource def target_resource
resource = @store.find_resource_by_path(@target) resource = @store.find_resource_by_path(@target)
@ -111,12 +111,12 @@ module Middleman
resource resource
end end
Contract None => IsA['Middleman::SourceFile'] Contract IsA['Middleman::SourceFile']
def source_file def source_file
target_resource.source_file target_resource.source_file
end end
Contract None => Maybe[String] Contract Maybe[String]
def content_type def content_type
mime_type = super mime_type = super
return mime_type if mime_type return mime_type if mime_type

View file

@ -44,7 +44,7 @@ module Middleman
end end
class RedirectResource < ::Middleman::Sitemap::Resource class RedirectResource < ::Middleman::Sitemap::Resource
Contract None => Maybe[Proc] Contract Maybe[Proc]
attr_accessor :output attr_accessor :output
def initialize(store, path, target) def initialize(store, path, target)
@ -53,7 +53,7 @@ module Middleman
super(store, path) super(store, path)
end end
Contract None => Bool Contract Bool
def template? def template?
true true
end end
@ -82,7 +82,7 @@ module Middleman
end end
end end
Contract None => Bool Contract Bool
def ignored? def ignored?
false false
end end

View file

@ -52,7 +52,7 @@ module Middleman
end end
class EndpointResource < ::Middleman::Sitemap::Resource class EndpointResource < ::Middleman::Sitemap::Resource
Contract None => Maybe[Proc] Contract Maybe[Proc]
attr_accessor :output attr_accessor :output
def initialize(store, path, request_path) def initialize(store, path, request_path)
@ -60,10 +60,10 @@ module Middleman
@request_path = ::Middleman::Util.normalize_path(request_path) @request_path = ::Middleman::Util.normalize_path(request_path)
end end
Contract None => String Contract String
attr_reader :request_path attr_reader :request_path
Contract None => Bool Contract Bool
def template? def template?
true true
end end
@ -73,7 +73,7 @@ module Middleman
return output.call if output return output.call if output
end end
Contract None => Bool Contract Bool
def ignored? def ignored?
false false
end end

View file

@ -23,7 +23,7 @@ module Middleman
# The on-disk source file for this resource, if there is one # The on-disk source file for this resource, if there is one
# @return [String] # @return [String]
Contract None => Maybe[IsA['Middleman::SourceFile']] Contract Maybe[IsA['Middleman::SourceFile']]
attr_reader :source_file attr_reader :source_file
# The path to use when requesting this resource. Normally it's # The path to use when requesting this resource. Normally it's
@ -35,7 +35,7 @@ module Middleman
# The metadata for this resource # The metadata for this resource
# @return [Hash] # @return [Hash]
Contract None => METADATA_HASH Contract METADATA_HASH
attr_reader :metadata attr_reader :metadata
# Initialize resource with parent store and URL # Initialize resource with parent store and URL
@ -69,7 +69,7 @@ module Middleman
# Whether this resource has a template file # Whether this resource has a template file
# @return [Boolean] # @return [Boolean]
Contract None => Bool Contract Bool
def template? def template?
return false if source_file.nil? return false if source_file.nil?
!::Tilt[source_file[:full_path].to_s].nil? !::Tilt[source_file[:full_path].to_s].nil?
@ -88,7 +88,7 @@ module Middleman
# Data about this resource, populated from frontmatter or extensions. # Data about this resource, populated from frontmatter or extensions.
# @return [HashWithIndifferentAccess] # @return [HashWithIndifferentAccess]
Contract None => IsA['Middleman::Util::HashWithIndifferentAccess'] Contract IsA['Middleman::Util::HashWithIndifferentAccess']
def data def data
# TODO: Should this really be a HashWithIndifferentAccess? # TODO: Should this really be a HashWithIndifferentAccess?
::Middleman::Util.recursively_enhance(metadata[:page]) ::Middleman::Util.recursively_enhance(metadata[:page])
@ -97,21 +97,21 @@ module Middleman
# Options about how this resource is rendered, such as its :layout, # Options about how this resource is rendered, such as its :layout,
# :renderer_options, and whether or not to use :directory_indexes. # :renderer_options, and whether or not to use :directory_indexes.
# @return [Hash] # @return [Hash]
Contract None => Hash Contract Hash
def options def options
metadata[:options] metadata[:options]
end end
# Local variable mappings that are used when rendering the template for this resource. # Local variable mappings that are used when rendering the template for this resource.
# @return [Hash] # @return [Hash]
Contract None => Hash Contract Hash
def locals def locals
metadata[:locals] metadata[:locals]
end end
# Extension of the path (i.e. '.js') # Extension of the path (i.e. '.js')
# @return [String] # @return [String]
Contract None => String Contract String
def ext def ext
File.extname(path) File.extname(path)
end end
@ -141,7 +141,7 @@ module Middleman
# A path without the directory index - so foo/index.html becomes # A path without the directory index - so foo/index.html becomes
# just foo. Best for linking. # just foo. Best for linking.
# @return [String] # @return [String]
Contract None => String Contract String
def url def url
url_path = destination_path url_path = destination_path
if @app.config[:strip_index_file] if @app.config[:strip_index_file]
@ -154,7 +154,7 @@ module Middleman
# Whether the source file is binary. # Whether the source file is binary.
# #
# @return [Boolean] # @return [Boolean]
Contract None => Bool Contract Bool
def binary? def binary?
!source_file.nil? && ::Middleman::Util.binary?(source_file[:full_path].to_s) !source_file.nil? && ::Middleman::Util.binary?(source_file[:full_path].to_s)
end end
@ -162,14 +162,14 @@ module Middleman
# Ignore a resource directly, without going through the whole # Ignore a resource directly, without going through the whole
# ignore filter stuff. # ignore filter stuff.
# @return [void] # @return [void]
Contract None => Any Contract Any
def ignore! def ignore!
@ignored = true @ignored = true
end end
# Whether the Resource is ignored # Whether the Resource is ignored
# @return [Boolean] # @return [Boolean]
Contract None => Bool Contract Bool
def ignored? def ignored?
return true if @ignored return true if @ignored
# Ignore based on the source path (without template extensions) # Ignore based on the source path (without template extensions)
@ -184,7 +184,7 @@ module Middleman
# The preferred MIME content type for this resource based on extension or metadata # The preferred MIME content type for this resource based on extension or metadata
# @return [String] MIME type for this resource # @return [String] MIME type for this resource
Contract None => Maybe[String] Contract Maybe[String]
def content_type def content_type
options[:content_type] || ::Rack::Mime.mime_type(ext, nil) options[:content_type] || ::Rack::Mime.mime_type(ext, nil)
end end

View file

@ -15,14 +15,14 @@ module Middleman
include Contracts include Contracts
# A reference to the current app. # A reference to the current app.
Contract None => IsA['Middleman::Application'] Contract IsA['Middleman::Application']
attr_reader :app attr_reader :app
# Duck-typed definition of a valid source watcher # Duck-typed definition of a valid source watcher
HANDLER = RespondTo[:on_change] HANDLER = RespondTo[:on_change]
# Config # Config
Contract None => Hash Contract Hash
attr_reader :options attr_reader :options
# Reference to the global logger. # Reference to the global logger.
@ -125,7 +125,7 @@ module Middleman
end end
# A list of registered watchers # A list of registered watchers
Contract None => ArrayOf[HANDLER] Contract ArrayOf[HANDLER]
def watchers def watchers
@sorted_watchers @sorted_watchers
end end
@ -155,7 +155,7 @@ module Middleman
# Get all files for this collection of watchers. # Get all files for this collection of watchers.
# #
# @return [Array<Middleman::SourceFile>] # @return [Array<Middleman::SourceFile>]
Contract None => ArrayOf[SourceFile] Contract ArrayOf[SourceFile]
def files def files
watchers.flat_map(&:files).uniq { |f| f[:relative_path] } watchers.flat_map(&:files).uniq { |f| f[:relative_path] }
end end
@ -204,7 +204,7 @@ module Middleman
# Manually poll all watchers for new content. # Manually poll all watchers for new content.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def find_new_files! def find_new_files!
return unless @update_count != @last_update_count return unless @update_count != @last_update_count
@ -215,7 +215,7 @@ module Middleman
# Start up all listeners. # Start up all listeners.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def start! def start!
watchers.each(&:listen!) watchers.each(&:listen!)
@running = true @running = true
@ -224,7 +224,7 @@ module Middleman
# Stop the watchers. # Stop the watchers.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def stop! def stop!
watchers.each(&:stop_listener!) watchers.each(&:stop_listener!)
@running = false @running = false
@ -300,7 +300,7 @@ module Middleman
# Increment the internal counter for changes. # Increment the internal counter for changes.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def bump_count def bump_count
@update_count += 1 @update_count += 1
end end

View file

@ -17,15 +17,15 @@ module Middleman
def_delegator :app, :logger def_delegator :app, :logger
# The type this watcher is representing # The type this watcher is representing
Contract None => Symbol Contract Symbol
attr_reader :type attr_reader :type
# The directory that is being watched # The directory that is being watched
Contract None => Pathname Contract Pathname
attr_reader :directory attr_reader :directory
# Options for configuring the watcher # Options for configuring the watcher
Contract None => Hash Contract Hash
attr_reader :options attr_reader :options
# Construct a new SourceWatcher # Construct a new SourceWatcher
@ -80,7 +80,7 @@ module Middleman
# Stop watching. # Stop watching.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def unwatch def unwatch
stop_listener! stop_listener!
end end
@ -88,7 +88,7 @@ module Middleman
# All the known files in this watcher. # All the known files in this watcher.
# #
# @return [Array<Middleman::SourceFile>] # @return [Array<Middleman::SourceFile>]
Contract None => ArrayOf[IsA['Middleman::SourceFile']] Contract ArrayOf[IsA['Middleman::SourceFile']]
def files def files
@files.values @files.values
end end
@ -124,7 +124,7 @@ module Middleman
# Start the `listen` gem Listener. # Start the `listen` gem Listener.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def listen! def listen!
return if @disable_watcher || @listener || @waiting_for_existence return if @disable_watcher || @listener || @waiting_for_existence
@ -140,7 +140,7 @@ module Middleman
# Stop the listener. # Stop the listener.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def stop_listener! def stop_listener!
return unless @listener return unless @listener
@ -151,7 +151,7 @@ module Middleman
# Manually trigger update events. # Manually trigger update events.
# #
# @return [void] # @return [void]
Contract None => Any Contract Any
def poll_once! def poll_once!
removed = @files.keys removed = @files.keys

View file

@ -51,5 +51,5 @@ Gem::Specification.new do |s|
s.add_dependency('execjs', ['~> 2.0']) s.add_dependency('execjs', ['~> 2.0'])
# Testing # Testing
s.add_dependency('contracts', ['~> 0.7.0']) s.add_dependency('contracts', ['~> 0.9.0'])
end end