From 55c5a464406d0f4b124b0dc3c514cf77750c1537 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Fri, 24 Apr 2015 10:26:42 -0700 Subject: [PATCH] Update contracts gem. #1494 --- Gemfile | 4 +-- .../lib/middleman-core/application.rb | 13 ++++---- middleman-core/lib/middleman-core/builder.rb | 10 +++--- .../lib/middleman-core/contracts.rb | 32 ------------------- .../core_extensions/collections.rb | 2 +- .../middleman-core/core_extensions/data.rb | 2 +- .../core_extensions/file_watcher.rb | 6 ++-- .../middleman-core/core_extensions/i18n.rb | 4 +-- .../lib/middleman-core/file_renderer.rb | 2 +- .../sitemap/extensions/on_disk.rb | 2 +- .../sitemap/extensions/proxies.rb | 6 ++-- .../sitemap/extensions/redirects.rb | 6 ++-- .../sitemap/extensions/request_endpoints.rb | 8 ++--- .../lib/middleman-core/sitemap/resource.rb | 24 +++++++------- middleman-core/lib/middleman-core/sources.rb | 16 +++++----- .../middleman-core/sources/source_watcher.rb | 16 +++++----- middleman-core/middleman-core.gemspec | 2 +- 17 files changed, 61 insertions(+), 94 deletions(-) diff --git a/Gemfile b/Gemfile index 5ef41f48..9e3c9cda 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/middleman-core/lib/middleman-core/application.rb b/middleman-core/lib/middleman-core/application.rb index ef32c06b..210f2c16 100644 --- a/middleman-core/lib/middleman-core/application.rb +++ b/middleman-core/lib/middleman-core/application.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/builder.rb b/middleman-core/lib/middleman-core/builder.rb index 9ea50f0d..f5563293 100644 --- a/middleman-core/lib/middleman-core/builder.rb +++ b/middleman-core/lib/middleman-core/builder.rb @@ -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] 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] 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) diff --git a/middleman-core/lib/middleman-core/contracts.rb b/middleman-core/lib/middleman-core/contracts.rb index 9d7e77ee..349728e2 100644 --- a/middleman-core/lib/middleman-core/contracts.rb +++ b/middleman-core/lib/middleman-core/contracts.rb @@ -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 ||= {} diff --git a/middleman-core/lib/middleman-core/core_extensions/collections.rb b/middleman-core/lib/middleman-core/core_extensions/collections.rb index 714e1f51..c8ceb1fd 100644 --- a/middleman-core/lib/middleman-core/core_extensions/collections.rb +++ b/middleman-core/lib/middleman-core/core_extensions/collections.rb @@ -28,7 +28,7 @@ module Middleman @root_collector = LazyCollectorRoot.new(self) end - Contract None => Any + Contract Any def before_configuration @leaves.clear diff --git a/middleman-core/lib/middleman-core/core_extensions/data.rb b/middleman-core/lib/middleman-core/core_extensions/data.rb index ba0cba70..3cb725c5 100644 --- a/middleman-core/lib/middleman-core/core_extensions/data.rb +++ b/middleman-core/lib/middleman-core/core_extensions/data.rb @@ -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 = {} diff --git a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb index 13cdd3b2..75d1071c 100644 --- a/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb +++ b/middleman-core/lib/middleman-core/core_extensions/file_watcher.rb @@ -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]) diff --git a/middleman-core/lib/middleman-core/core_extensions/i18n.rb b/middleman-core/lib/middleman-core/core_extensions/i18n.rb index d8c72503..bb769faf 100644 --- a/middleman-core/lib/middleman-core/core_extensions/i18n.rb +++ b/middleman-core/lib/middleman-core/core_extensions/i18n.rb @@ -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) diff --git a/middleman-core/lib/middleman-core/file_renderer.rb b/middleman-core/lib/middleman-core/file_renderer.rb index 2f6a5d3a..91b083e1 100644 --- a/middleman-core/lib/middleman-core/file_renderer.rb +++ b/middleman-core/lib/middleman-core/file_renderer.rb @@ -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) || '' diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb index 3f58e169..2f7f0498 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/on_disk.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb index 07c99e0f..536200db 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/proxies.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb index fdec744c..84f96749 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/redirects.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb index f0a12dd0..63de03c7 100644 --- a/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb +++ b/middleman-core/lib/middleman-core/sitemap/extensions/request_endpoints.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/sitemap/resource.rb b/middleman-core/lib/middleman-core/sitemap/resource.rb index 57712d26..a0f9fee4 100644 --- a/middleman-core/lib/middleman-core/sitemap/resource.rb +++ b/middleman-core/lib/middleman-core/sitemap/resource.rb @@ -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 diff --git a/middleman-core/lib/middleman-core/sources.rb b/middleman-core/lib/middleman-core/sources.rb index f7f8bd7f..005bdde1 100644 --- a/middleman-core/lib/middleman-core/sources.rb +++ b/middleman-core/lib/middleman-core/sources.rb @@ -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] - 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 diff --git a/middleman-core/lib/middleman-core/sources/source_watcher.rb b/middleman-core/lib/middleman-core/sources/source_watcher.rb index 3393cd1c..e09a8aba 100644 --- a/middleman-core/lib/middleman-core/sources/source_watcher.rb +++ b/middleman-core/lib/middleman-core/sources/source_watcher.rb @@ -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] - 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 diff --git a/middleman-core/middleman-core.gemspec b/middleman-core/middleman-core.gemspec index 82e7dae3..3df76827 100644 --- a/middleman-core/middleman-core.gemspec +++ b/middleman-core/middleman-core.gemspec @@ -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