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
gem 'rubocop', '~> 0.24', require: false
gem 'simplecov', '0.9', require: false
gem 'coveralls', '~> 0.7', require: false
gem 'simplecov', '~> 0.9', require: false
gem 'coveralls', '~> 0.8', require: false
gem 'codeclimate-test-reporter', '~> 0.3', require: false, group: :test
# Middleman itself

View file

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

View file

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

View file

@ -27,38 +27,6 @@ if ENV['TEST'] || ENV['CONTRACTS'] == 'true'
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
# def self.[](val)
# @lookup ||= {}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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