Permalink
Browse files

Initial commit to yardstick

  • Loading branch information...
0 parents commit 221622ceaa5b1468e18774d9d9f71919ba4281d7 @dkubb committed Jul 18, 2009
@@ -0,0 +1,5 @@
+README.rdoc
+lib/**/*.rb
+bin/*
+features/**/*.feature
+LICENSE
@@ -0,0 +1,8 @@
+*.sw?
+.DS_Store
+.yardoc
+coverage
+rdoc
+doc
+pkg
+tmp
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Dan Kubb
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,7 @@
+= yardstick
+
+Description goes here.
+
+== Copyright
+
+Copyright (c) 2009 Dan Kubb. See LICENSE for details.
@@ -0,0 +1,24 @@
+require 'rubygems'
+require 'rake'
+require 'pathname'
+
+Pathname.glob('tasks/**/*.rake').each { |task| load task.expand_path }
+
+begin
+ require 'jeweler'
+
+ Jeweler::Tasks.new do |gem|
+ gem.name = 'yardstick'
+ gem.summary = %Q{TODO}
+ gem.email = 'dan.kubb@gmail.com'
+ gem.homepage = 'http://github.com/dkubb/yardstick'
+ gem.authors = [ 'Dan Kubb' ]
+ gem.rubyforge_project = 'yardstick'
+
+ gem.add_dependency('yard', '~>0.2')
+ end
+
+ Jeweler::RubyforgeTasks.new
+rescue LoadError
+ puts 'Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler'
+end
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby -KU
+
+require 'rubygems'
+require 'pathname'
+require Pathname(__FILE__).dirname.expand_path.parent.join('lib', 'yardstick')
+
+Yardstick::CLI.run(*ARGV)
@@ -0,0 +1,75 @@
+require 'pathname'
+require 'yard'
+
+module Yardstick
+ VERSION = '0.0.1'.freeze
+ ROOT = Pathname(__FILE__).dirname.parent.expand_path.freeze
+
+ # Measure a list of files
+ #
+ # @example
+ # Yardstick.measure('article.rb') # => [ Measurement ]
+ #
+ # @param [Array<#to_str>, #to_str] paths
+ # optional list of paths to measure
+ # @param [Hash] options
+ # optional configuration
+ #
+ # @return [Array<Measurement>]
+ # the measurements for each file
+ #
+ # @api public
+ def self.measure(files = 'lib/**/*.rb', options = {})
+ Yardstick::Processor.process_files(files)
+ end
+
+ # Measure a string of code and YARD documentation
+ #
+ # @example
+ # string = "def my_method; end"
+ #
+ # Yardstick.measure_string(string) # => [ Measurement ]
+ #
+ # @param [#to_str] string
+ # the string to measure
+ # @param [Hash] options
+ # optional configuration
+ #
+ # @return [Array<Measurement>]
+ # the measurements for the string
+ #
+ # @api public
+ def self.measure_string(string, options = {})
+ Yardstick::Processor.process_string(string)
+ end
+end
+
+$LOAD_PATH << Yardstick::ROOT + 'lib'
+
+require 'yardstick/core_ext/object'
+require 'yardstick/autoload'
+
+module YARD
+ module CodeObjects
+ class MethodObject
+
+ # Return the docstring associated with the method
+ #
+ # @example
+ # method_object.docstring # => YARD::Docstring instance
+ #
+ # @return [YARD::Docstring]
+ # the docstring for this method
+ #
+ # @api public
+ def docstring
+ # TODO: update to use super() once reek does not flag it as
+ # a utility method
+ @docstring.extend(Yardstick::Method)
+ end
+
+ # TODO: create an object to wrap tags, and extend each tag object with
+ # the matching module, if one exists under Yardstick::Tag::*
+ end
+ end
+end
@@ -0,0 +1,8 @@
+module Yardstick
+ autoload :Class, 'yardstick/class'
+ autoload :CLI, 'yardstick/cli'
+ autoload :Measurable, 'yardstick/measurable'
+ autoload :Measurement, 'yardstick/measurement'
+ autoload :Method, 'yardstick/method'
+ autoload :Processor, 'yardstick/processor'
+end
@@ -0,0 +1,4 @@
+module Yardstick
+ module Class
+ end
+end
@@ -0,0 +1,76 @@
+require 'optparse'
+
+module Yardstick
+ class CLI
+ # Parse the command line options, and run the command
+ #
+ # @example
+ # Yardstick::CLI.run(%w[ article.rb ]) # => [ Measurement ]
+ #
+ # @param [Array] args
+ # arguments passed in from the command line
+ #
+ # @return [Array(Measurement)]
+ # the measurement for each file
+ #
+ # @api private
+ def self.run(*args)
+ Yardstick.measure(*parse_options(args)).each do |measurement|
+ measurement.warn
+ end
+ end
+
+ # Parse the options provided from the command-line
+ #
+ # @param [Array<String>] args
+ # the command-line options
+ #
+ # @return [Array(Array<String>, Hash)]
+ # the list of files, and options parsed from the command-line
+ #
+ # @api private
+ def self.parse_options(args)
+ args << '--help' if args.empty?
+ options = {}
+ option_parser(options).parse!(args)
+ [ args, options ]
+ rescue OptionParser::InvalidOption => error
+ display_exit(error.message << "\n" << args.inspect)
+ end
+
+ # Return an OptionParser instance for the command-line app
+ #
+ # @param [Hash] options
+ # the options to set when parsing the command-line arguments
+ #
+ # @return [OptionParser]
+ # the option parser instance
+ #
+ # @api private
+ def self.option_parser(options)
+ opts = OptionParser.new
+ opts.on_tail('-v', '--version', 'print version information and exit') { display_exit("#{opts.program_name} #{Yardstick::VERSION}") }
+ opts.on_tail('-h', '--help', 'display this help and exit') { display_exit(opts) }
+ opts
+ end
+
+ # Display a message and exit
+ #
+ # @param [#to_str] message
+ # the message to display
+ #
+ # @return [undefined]
+ #
+ # @api private
+ def self.display_exit(message)
+ puts message.to_str
+ exit
+ end
+
+ class << self
+ private :option_parser
+ private :parse_options
+ private :display_exit
+ end
+ end
+end
@@ -0,0 +1,11 @@
+class Object
+ # Return the meta class for this instance
+ #
+ # @return [Class]
+ # the meta class
+ #
+ # @api private
+ def meta_class
+ class << self; self end
+ end
+end
@@ -0,0 +1,82 @@
+module Yardstick
+ module Measurable
+ include Measurement::UtilityMethods
+
+ module ClassMethods
+ # List of measurement types for this class
+ #
+ # @return [Array<Array(String, Symbol)>]
+ #
+ # @api private
+ def measurements
+ @measurements ||= []
+ end
+
+ # Set the description for the next method
+ #
+ # @return [undefined]
+ #
+ # @api private
+ def measurement(description, &block)
+ measurements << [ description, block ]
+ end
+
+ private
+
+ # Include the class or module with measurable class methods
+ #
+ # @param [Module] mod
+ # the module to include within
+ #
+ # @return [undefined]
+ #
+ # @api private
+ def included(mod)
+ mod.extend(ClassMethods)
+ copy_measurements(mod)
+ end
+
+ # Extend the docstring meta class with measurable class methods
+ #
+ # @param [YARD::Docstring] docstring
+ # the docstring to extend
+ #
+ # @return [undefined]
+ #
+ # @api private
+ def extended(docstring)
+ meta_class = docstring.meta_class
+ meta_class.extend(ClassMethods)
+ copy_measurements(meta_class)
+ end
+
+ # Copy measurements from the ancestor to the descendant
+ #
+ # @param [Module] descendant
+ # the descendant module or class
+ #
+ # @return [undefined]
+ #
+ # @api private
+ def copy_measurements(descendant)
+ descendant.measurements.concat(measurements).uniq!
+ end
+ end
+
+ extend ClassMethods
+
+ # Return a list of measurements for this docstring instance
+ #
+ # @example
+ # docstring.measure # => [ Measurement ]
+ #
+ # @return [Array<Measurement>]
+ #
+ # @api public
+ def measure
+ meta_class.measurements.map do |(description, block)|
+ Measurement.new(description, self, &block)
+ end
+ end
+ end
+end
Oops, something went wrong. Retry.

0 comments on commit 221622c

Please sign in to comment.