Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jdantonio committed May 4, 2013
0 parents commit 77bb46f
Show file tree
Hide file tree
Showing 16 changed files with 347 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .gitignore
@@ -0,0 +1,28 @@
tests.txt
*.gem
.rvmrc
.ruby-version
.ruby-gemset
.bundle/*
.yardoc/*
doc/*
tmp/*
man/*
*.tmproj
rdoc/*
*.orig
*.BACKUP.*
*.BASE.*
*.LOCAL.*
*.REMOTE.*
git_pull.txt
coverage
.DS_Store
TAGS
tmtags
*.swo
*.swp
.idea
.rbx/*
*.py
*.pyc
14 changes: 14 additions & 0 deletions Gemfile
@@ -0,0 +1,14 @@
source "http://rubygems.org"

gemspec

# create API documentation
gem 'yard', :platforms => :ruby
gem 'redcarpet', :platforms => :ruby
#gem 'github-markup'

# testing
gem 'rake'
gem 'rspec'
gem 'simplecov'
gem 'countloc'
39 changes: 39 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,39 @@
PATH
remote: .
specs:
pattern_matching (0.0.1)

GEM
remote: http://rubygems.org/
specs:
countloc (0.4.0)
diff-lcs (1.2.4)
multi_json (1.7.2)
rake (10.0.4)
redcarpet (2.2.2)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
simplecov (0.7.1)
multi_json (~> 1.0)
simplecov-html (~> 0.7.1)
simplecov-html (0.7.1)
yard (0.8.6.1)

PLATFORMS
ruby

DEPENDENCIES
bundler
countloc
pattern_matching!
rake
redcarpet
rspec
simplecov
yard
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
Copyright (c) Jerry D'Antonio -- released under the MIT license.

http://www.opensource.org/licenses/mit-license.php

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.
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
# PatternMatching

A gem for adding Erlang-style pattern matching to Ruby.
18 changes: 18 additions & 0 deletions Rakefile
@@ -0,0 +1,18 @@
$:.push File.join(File.dirname(__FILE__), 'lib')
$:.push File.join(File.dirname(__FILE__), 'tasks/support')

require 'rubygems'
require 'bundler/gem_tasks'
require 'rspec'
require 'rspec/core/rake_task'

require 'pattern_matching'

Bundler::GemHelper.install_tasks

$:.unshift 'tasks'
Dir.glob('tasks/**/*.rake').each do|rakefile|
load rakefile
end

task :default => [:spec]
5 changes: 5 additions & 0 deletions lib/pattern_matching.rb
@@ -0,0 +1,5 @@
require 'pattern_matching/version'

module PatternMatching

end
Empty file added lib/pattern_matching/.gitignore
Empty file.
3 changes: 3 additions & 0 deletions lib/pattern_matching/version.rb
@@ -0,0 +1,3 @@
module PatternMatching
VERSION = '0.0.1'
end
33 changes: 33 additions & 0 deletions pattern_matching.gemspec
@@ -0,0 +1,33 @@
$LOAD_PATH << File.expand_path('../lib', __FILE__)

require 'pattern_matching/version'
require 'date'
require 'rbconfig'

Gem::Specification.new do |s|
s.name = 'pattern_matching'
s.version = PatternMatching::VERSION
s.platform = Gem::Platform::RUBY
s.author = "Jerry D'Antonio"
s.email = 'jerry.dantonio@gmail.com'
s.homepage = 'https://github.com/jdantonio/pattern_matching/'
s.summary = 'Erlang-style pattern matching to Ruby.'
s.license = 'MIT'
s.date = Date.today.to_s

s.description = <<-EOF
A gem for adding Erlang-style pattern matching to Ruby.
EOF

s.files = Dir['README*', 'LICENSE*', 'CHANGELOG*']
s.files += Dir['{lib,spec}/**/*']
s.test_files = Dir['{spec}/**/*']
s.extra_rdoc_files = ['README.md']
s.extra_rdoc_files = Dir['README*', 'LICENSE*', 'CHANGELOG*']
s.require_paths = ['lib']

s.required_ruby_version = '>= 1.9.2'
s.post_install_message = 'Happy matching!'

s.add_development_dependency 'bundler'
end
Empty file added spec/.gitignore
Empty file.
33 changes: 33 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,33 @@
require 'simplecov'
SimpleCov.start do
project_name 'ratistics'
add_filter '/data/'
add_filter '/spec/'
add_filter '/tasks/'
end

jruby = (0 == (RbConfig::CONFIG['ruby_install_name']=~ /^jruby$/i))
windows = (RbConfig::CONFIG['host_os'] =~ /mswin32/i || RbConfig::CONFIG['host_os'] =~ /mingw32/i)
ar = !(jruby || windows)

require 'ratistics'

require 'rspec'
require 'hamster' unless windows
require 'active_record' if ar

# import all the support files
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require File.expand_path(f) }

RSpec.configure do |config|

config.before(:suite) do
end

config.before(:each) do
end

config.after(:each) do
end

end
Empty file added spec/support/.gitignore
Empty file.
Empty file added tasks/.gitignore
Empty file.
22 changes: 22 additions & 0 deletions tasks/metrics.rake
@@ -0,0 +1,22 @@
require 'code_statistics'

#NOTE: This task has been picked up from a dependency
#desc 'Display code metrics'
#task :stats do

#STATS_DIRECTORIES = [
#%w(Libraries lib/),
##%w(Tests spec/),
#].collect { |name, dir| [ name, "./#{dir}" ] }.select { |name, dir| File.directory?(dir) }

#CodeStatistics.new(*STATS_DIRECTORIES).to_s
#end

desc 'Display LOC (lines of code) report'
task :loc do
puts `countloc -r lib`
#puts `countloc -r spec`
end

desc 'Report code statistics'
task :metrics => [:stats, :loc]
128 changes: 128 additions & 0 deletions tasks/support/code_statistics.rb
@@ -0,0 +1,128 @@
# https://github.com/rails/rails/blob/master/railties/lib/rails/code_statistics.rb
class CodeStatistics #:nodoc:

TEST_TYPES = ['Controller tests',
'Helper tests',
'Model tests',
'Mailer tests',
'Integration tests',
'Functional tests (old)',
'Unit tests (old)']

def initialize(*pairs)
@pairs = pairs
@statistics = calculate_statistics
@total = calculate_total if pairs.length > 1
end

def to_s
print_header
@pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
print_splitter

if @total
print_line("Total", @total)
print_splitter
end

print_code_test_stats
end

private
def calculate_statistics
Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}]
end

def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee)$/)
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }

Dir.foreach(directory) do |file_name|
if File.directory?(directory + "/" + file_name) and (/^\./ !~ file_name)
newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
stats.each { |k, v| stats[k] += newstats[k] }
end

next unless file_name =~ pattern

comment_started = false

case file_name
when /.*\.js$/
comment_pattern = /^\s*\/\//
else
comment_pattern = /^\s*#/
end

File.open(directory + "/" + file_name) do |f|
while line = f.gets
stats["lines"] += 1
if(comment_started)
if line =~ /^=end/
comment_started = false
end
next
else
if line =~ /^=begin/
comment_started = true
next
end
end
stats["classes"] += 1 if line =~ /^\s*class\s+[_A-Z]/
stats["methods"] += 1 if line =~ /^\s*def\s+[_a-z]/
stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ comment_pattern
end
end
end

stats
end

def calculate_total
total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
@statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
total
end

def calculate_code
code_loc = 0
@statistics.each { |k, v| code_loc += v['codelines'] unless TEST_TYPES.include? k }
code_loc
end

def calculate_tests
test_loc = 0
@statistics.each { |k, v| test_loc += v['codelines'] if TEST_TYPES.include? k }
test_loc
end

def print_header
print_splitter
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
print_splitter
end

def print_splitter
puts "+----------------------+-------+-------+---------+---------+-----+-------+"
end

def print_line(name, statistics)
m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0
loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0

puts "| #{name.ljust(20)} " +
"| #{statistics["lines"].to_s.rjust(5)} " +
"| #{statistics["codelines"].to_s.rjust(5)} " +
"| #{statistics["classes"].to_s.rjust(7)} " +
"| #{statistics["methods"].to_s.rjust(7)} " +
"| #{m_over_c.to_s.rjust(3)} " +
"| #{loc_over_m.to_s.rjust(5)} |"
end

def print_code_test_stats
code = calculate_code
tests = calculate_tests

puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
puts ""
end
end

0 comments on commit 77bb46f

Please sign in to comment.