pax_global_header 0000666 0000000 0000000 00000000064 12050546225 0014513 g ustar 00root root 0000000 0000000 52 comment=cae61bc39b430efcac274ca2c4c1f41a10342fe4
ruby-tins-0.6.0/ 0000775 0000000 0000000 00000000000 12050546225 0013452 5 ustar 00root root 0000000 0000000 ruby-tins-0.6.0/.gitignore 0000664 0000000 0000000 00000000076 12050546225 0015445 0 ustar 00root root 0000000 0000000 .*.sw[pon]
.AppleDouble
.rbx
.rvmrc
Gemfile.lock
coverage
pkg
ruby-tins-0.6.0/.travis.yml 0000664 0000000 0000000 00000000332 12050546225 0015561 0 ustar 00root root 0000000 0000000 rvm:
- 1.8.6
- 1.8.7
- 1.9.2
- 1.9.3
- ruby-head
- ree
- rbx-18mode
- rbx-19mode
- jruby-18mode
- jruby-19mode
matrix:
allow_failures:
- rvm: 1.8.6
- rvm: rbx-18mode
- rvm: rbx-19mode
ruby-tins-0.6.0/Gemfile 0000664 0000000 0000000 00000000254 12050546225 0014746 0 ustar 00root root 0000000 0000000 # vim: set filetype=ruby et sw=2 ts=2:
source :rubygems
gemspec
group :development do
gem 'simplecov', :platform => :mri_19
gem 'debugger', :platform => :mri_19
end
ruby-tins-0.6.0/LICENSE 0000664 0000000 0000000 00000002021 12050546225 0014452 0 ustar 00root root 0000000 0000000 Copyright (c) 2010 Florian Frank
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 X
CONSORTIUM 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.
ruby-tins-0.6.0/README.rdoc 0000664 0000000 0000000 00000000476 12050546225 0015267 0 ustar 00root root 0000000 0000000 = Tins - Useful tools library in Ruby
== Description
Non yet.
== Download
The homepage of this library is located at
* http://flori.github.com/tins
== Author
Florian Frank mailto:flori@ping.de
== License
This software is licensed under the X11 (or MIT) license:
http://www.xfree86.org/3.3.6/COPYRIGHT2.html#3
ruby-tins-0.6.0/Rakefile 0000664 0000000 0000000 00000001574 12050546225 0015126 0 ustar 00root root 0000000 0000000 # vim: set filetype=ruby et sw=2 ts=2:
require 'gem_hadar'
GemHadar do
name 'tins'
author 'Florian Frank'
email 'flori@ping.de'
homepage "http://flori.github.com/#{name}"
summary 'Useful stuff.'
description 'All the stuff that isn\'t good/big enough for a real library.'
test_dir 'tests'
test_files.concat Dir["#{test_dir}/*_test.rb"]
ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', '.rvmrc', 'coverage', '.rbx',
'.AppleDouble'
readme 'README.rdoc'
development_dependency 'test-unit', '~>2.5'
development_dependency 'utils'
install_library do
libdir = CONFIG["sitelibdir"]
cd 'lib' do
for file in Dir['**/*.rb']
dst = File.join(libdir, file)
mkdir_p File.dirname(dst)
install file, dst
end
end
install 'bin/enum', File.join(CONFIG['bindir'], 'enum')
end
end
ruby-tins-0.6.0/TODO 0000664 0000000 0000000 00000000000 12050546225 0014130 0 ustar 00root root 0000000 0000000 ruby-tins-0.6.0/VERSION 0000664 0000000 0000000 00000000006 12050546225 0014516 0 ustar 00root root 0000000 0000000 0.6.0
ruby-tins-0.6.0/lib/ 0000775 0000000 0000000 00000000000 12050546225 0014220 5 ustar 00root root 0000000 0000000 ruby-tins-0.6.0/lib/spruz.rb 0000664 0000000 0000000 00000000044 12050546225 0015726 0 ustar 00root root 0000000 0000000 require 'tins'
require 'tins/alias'
ruby-tins-0.6.0/lib/tins.rb 0000664 0000000 0000000 00000002207 12050546225 0015523 0 ustar 00root root 0000000 0000000 module Tins
require 'tins/attempt'
require 'tins/bijection'
require 'tins/count_by'
require 'tins/deep_dup'
require 'tins/file_binary'
require 'tins/find'
require 'tins/generator'
require 'tins/go'
require 'tins/hash_symbolize_keys_recursive'
require 'tins/hash_union'
require 'tins/limited'
require 'tins/lines_file'
require 'tins/memoize'
require 'tins/minimize'
require 'tins/module_group'
require 'tins/null'
require 'tins/once'
require 'tins/p'
require 'tins/partial_application'
require 'tins/range_plus'
require 'tins/require_maybe'
require 'tins/round'
require 'tins/secure_write'
require 'tins/shuffle'
require 'tins/string_camelize'
require 'tins/string_underscore'
require 'tins/string_version'
require 'tins/subhash'
require 'tins/time_dummy'
require 'tins/date_dummy'
require 'tins/date_time_dummy'
require 'tins/to_proc'
require 'tins/uniq_by'
require 'tins/version'
require 'tins/write'
require 'tins/extract_last_argument_options'
require 'tins/deep_const_get'
require 'tins/responding'
require 'tins/proc_compose'
require 'tins/concern'
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/ 0000775 0000000 0000000 00000000000 12050546225 0015175 5 ustar 00root root 0000000 0000000 ruby-tins-0.6.0/lib/tins/alias.rb 0000664 0000000 0000000 00000000015 12050546225 0016607 0 ustar 00root root 0000000 0000000 Spruz = Tins
ruby-tins-0.6.0/lib/tins/ask_and_send.rb 0000664 0000000 0000000 00000000526 12050546225 0020136 0 ustar 00root root 0000000 0000000 module Tins
module AskAndSend
def ask_and_send(method_name, *args, &block)
if respond_to?(method_name)
__send__(method_name, *args, &block)
end
end
def ask_and_send!(method_name, *args, &block)
if respond_to?(method_name, true)
__send__(method_name, *args, &block)
end
end
end
end
ruby-tins-0.6.0/lib/tins/attempt.rb 0000664 0000000 0000000 00000002252 12050546225 0017201 0 ustar 00root root 0000000 0000000 module Tins
module Attempt
def attempt(opts = {}, &block)
sleep = nil
exception_class = StandardError
if Numeric === opts
attempts = opts
else
attempts = opts[:attempts] || 1
exception_class = opts[:exception_class] if opts.key?(:exception_class)
sleep = opts[:sleep]
reraise = opts[:reraise]
end
return if attempts <= 0
count = 0
if exception_class.nil?
begin
count += 1
if block.call(count)
return true
elsif count < attempts
sleep_duration(sleep, count)
end
end until count == attempts
false
else
begin
count += 1
block.call(count)
true
rescue exception_class
if count < attempts
sleep_duration(sleep, count)
retry
end
reraise ? raise : false
end
end
end
private
def sleep_duration(duration, count)
case duration
when Numeric
sleep duration
when Proc
sleep duration.call(count)
end
end
end
end
ruby-tins-0.6.0/lib/tins/bijection.rb 0000664 0000000 0000000 00000001743 12050546225 0017475 0 ustar 00root root 0000000 0000000 module Tins
class Bijection < Hash
def self.[](*pairs)
pairs.size % 2 == 0 or
raise ArgumentError, "odd number of arguments for #{self}"
new.fill do |obj|
(pairs.size / 2).times do |i|
j = 2 * i
key = pairs[j]
value = pairs[j + 1]
obj.key?(key) and raise ArgumentError, "duplicate key #{key.inspect} for #{self}"
obj.inverted.key?(value) and raise ArgumentError, "duplicate value #{value.inspect} for #{self}"
obj[pairs[j]] = pairs[j + 1]
end
end
end
def initialize(inverted = Bijection.new(self))
@inverted = inverted
end
def fill
if empty?
yield self
freeze
end
self
end
def freeze
r = super
unless @inverted.frozen?
@inverted.freeze
end
r
end
def []=(key, value)
key?(key) and return
super
@inverted[value] = key
end
attr_reader :inverted
end
end
ruby-tins-0.6.0/lib/tins/concern.rb 0000664 0000000 0000000 00000001357 12050546225 0017157 0 ustar 00root root 0000000 0000000 module Tins
module Concern
def self.extended(base)
base.instance_variable_set("@_dependencies", [])
end
def append_features(base)
if base.instance_variable_defined?("@_dependencies")
base.instance_variable_get("@_dependencies") << self
return false
else
return false if base < self
@_dependencies.each { |dep| base.send(:include, dep) }
super
base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
end
end
def included(base = nil, &block)
if base.nil?
@_included_block = block
else
super
end
end
end
end
ruby-tins-0.6.0/lib/tins/count_by.rb 0000664 0000000 0000000 00000000221 12050546225 0017337 0 ustar 00root root 0000000 0000000 module Tins
module CountBy
def count_by(&b)
b ||= lambda { |x| true }
inject(0) { |s, e| s += 1 if b[e]; s }
end
end
end
ruby-tins-0.6.0/lib/tins/date_dummy.rb 0000664 0000000 0000000 00000001243 12050546225 0017652 0 ustar 00root root 0000000 0000000 require 'date'
module Tins
module DateDummy
def self.included(modul)
class << modul
alias really_today today
remove_method :today rescue nil
attr_writer :dummy
def dummy(value = nil)
if value.nil?
@dummy
else
begin
old_dummy = @dummy
@dummy = value
yield
ensure
@dummy = old_dummy
end
end
end
def today
if dummy
dummy.dup
else
really_today
end
end
end
super
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/date_time_dummy.rb 0000664 0000000 0000000 00000001231 12050546225 0020665 0 ustar 00root root 0000000 0000000 require 'date'
module Tins
module DateTimeDummy
def self.included(modul)
class << modul
alias really_now now
remove_method :now rescue nil
attr_writer :dummy
def dummy(value = nil)
if value.nil?
@dummy
else
begin
old_dummy = @dummy
@dummy = value
yield
ensure
@dummy = old_dummy
end
end
end
def now
if dummy
dummy.dup
else
really_now
end
end
end
super
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/deep_const_get.rb 0000664 0000000 0000000 00000002011 12050546225 0020476 0 ustar 00root root 0000000 0000000 module Tins
module DeepConstGet
if ::Object.method(:const_defined?).arity == 1
# :nocov:
# We do not create coverage on 1.8
def self.const_defined_in?(modul, constant)
modul.const_defined?(constant)
end
# :nocov:
else
def self.const_defined_in?(modul, constant)
modul.const_defined?(constant, false)
end
end
def deep_const_get(path, start_module = Object)
path.to_s.split('::').inject(start_module) do |p, c|
case
when c.empty?
if start_module == Object
Object
else
raise ArgumentError, "top level constants cannot be reached from"\
" start module #{start_module.inspect}"
end
when ::Tins::DeepConstGet.const_defined_in?(p, c) then p.const_get(c)
else
begin
p.const_missing(c)
rescue NameError => e
raise ArgumentError, "can't get const #{path}: #{e}"
end
end
end
end
end
end
ruby-tins-0.6.0/lib/tins/deep_dup.rb 0000664 0000000 0000000 00000000244 12050546225 0017307 0 ustar 00root root 0000000 0000000 module Tins
module DeepDup
def deep_dup
Marshal.load(Marshal.dump(self))
rescue TypeError
return self
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/extract_last_argument_options.rb 0000664 0000000 0000000 00000000473 12050546225 0023700 0 ustar 00root root 0000000 0000000 module Tins
module ExtractLastArgumentOptions
def extract_last_argument_options
last_argument = last
if last_argument.respond_to?(:to_hash) and
options = last_argument.to_hash.dup
then
return self[0..-2], options
else
return dup, {}
end
end
end
end
ruby-tins-0.6.0/lib/tins/file_binary.rb 0000664 0000000 0000000 00000005165 12050546225 0020014 0 ustar 00root root 0000000 0000000 require 'tins/xt/hash_union'
module Tins
module FileBinary
module Constants
SEEK_SET = ::File::SEEK_SET
ZERO = "\x00"
BINARY = "\x01-\x1f\x7f-\xff"
if defined?(::Encoding)
ZERO.force_encoding(Encoding::ASCII_8BIT)
BINARY.force_encoding(Encoding::ASCII_8BIT)
end
end
class << self
# Default options can be queried/set via this hash.
attr_accessor :default_options
end
self.default_options = {
:offset => 0,
:buffer_size => 2 ** 13,
:percentage_binary => 30.0,
:percentage_zeros => 0.0,
}
# Returns true if this file is considered to be binary, false if it is not
# considered to be binary, and nil if it was empty.
#
# A file is considered to be binary if the percentage of zeros exceeds
# options[:percentage_zeros] or the percentage of binary bytes
# (8-th bit is 1) exceeds options[:percentage_binary] in the
# buffer of size options[:buffer_size] that is checked (beginning
# from offset options[:offset]). If an option isn't given the one
# from FileBinary.default_options is used instead.
def binary?(options = {})
options |= FileBinary.default_options
old_pos = tell
seek options[:offset], Constants::SEEK_SET
data = read options[:buffer_size]
!data or data.empty? and return nil
data_size = data.size
data.count(Constants::ZERO).to_f / data_size >
options[:percentage_zeros] / 100.0 and return true
data.count(Constants::BINARY).to_f / data_size >
options[:percentage_binary] / 100.0
ensure
old_pos and seek old_pos, Constants::SEEK_SET
end
# Returns true if FileBinary#binary? returns false, false if
# FileBinary#binary? returns true, and nil otherwise. For an explanation of
# +options+, see FileBinary#binary?.
def ascii?(options = {})
case binary?(options)
when true then false
when false then true
end
end
def self.included(modul)
modul.instance_eval do
extend ClassMethods
end
super
end
module ClassMethods
# Returns true if the file with name +name+ is considered to be binary
# using the FileBinary#binary? method.
def binary?(name, options = {})
open(name, 'rb') { |f| f.binary?(options) }
end
# Returns true if the file with name +name+ is considered to be ascii
# using the FileBinary#ascii? method.
def ascii?(name, options = {})
open(name, 'rb') { |f| f.ascii?(options) }
end
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/find.rb 0000664 0000000 0000000 00000006747 12050546225 0016460 0 ustar 00root root 0000000 0000000 require 'enumerator'
require 'pathname'
require 'tins/module_group'
module Tins
module Find
EXPECTED_STANDARD_ERRORS = ModuleGroup[
Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP,
Errno::ENAMETOOLONG
]
class Finder
module PathExtension
attr_accessor :finder
def finder_stat
finder.protect_from_errors do
finder.follow_symlinks ? File.stat(self) : File.lstat(self)
end
end
def file
finder.protect_from_errors do
File.new(self) if file?
end
end
def file?
finder.protect_from_errors { s = finder_stat and s.file? }
end
def directory?
finder.protect_from_errors { s = finder_stat and s.directory? }
end
def exist?
finder.protect_from_errors { File.exist?(self) }
end
def stat
finder.protect_from_errors { File.stat(self) }
end
def lstat
finder.protect_from_errors { File.lstat(self) }
end
def pathname
Pathname.new(self)
end
def suffix
pathname.extname[1..-1] || ''
end
end
def initialize(opts = {})
@show_hidden = opts.fetch(:show_hidden) { true }
@raise_errors = opts.fetch(:raise_errors) { false }
@follow_symlinks = opts.fetch(:follow_symlinks) { true }
opts[:suffix].full? { |s| @suffix = [*s] }
end
attr_accessor :show_hidden
attr_accessor :raise_errors
attr_accessor :follow_symlinks
attr_accessor :suffix
def visit_path?(path)
@suffix.nil? || @suffix.include?(path.suffix)
end
def find(*paths)
block_given? or return enum_for(__method__, *paths)
paths.collect! { |d| d.dup }
while path = paths.shift
path = prepare_path(path)
catch(:prune) do
stat = path.finder_stat or next
visit_path?(path) and yield path
if stat.directory?
ps = protect_from_errors { Dir.entries(path) } or next
ps.sort!
ps.reverse_each do |p|
next if p == "." or p == ".."
next if !@show_hidden && p.start_with?('.')
p = File.join(path, p)
paths.unshift p.untaint
end
end
end
end
end
def prepare_path(path)
path = path.dup.taint
path.extend PathExtension
path.finder = self
path
end
def protect_from_errors(errors = Find::EXPECTED_STANDARD_ERRORS)
yield
rescue errors
raise_errors and raise
return
end
end
#
# Calls the associated block with the name of every path and directory
# listed as arguments, then recursively on their subdirectories, and so on.
#
# See the +Find+ module documentation for an example.
#
def find(*paths, &block) # :yield: path
opts = Hash === paths.last ? paths.pop : {}
Finder.new(opts).find(*paths, &block)
end
#
# Skips the current path or directory, restarting the loop with the next
# entry. If the current path is a directory, that directory will not be
# recursively entered. Meaningful only within the block associated with
# Find::find.
#
# See the +Find+ module documentation for an example.
#
def prune
throw :prune
end
module_function :find, :prune
end
end
ruby-tins-0.6.0/lib/tins/generator.rb 0000664 0000000 0000000 00000003754 12050546225 0017521 0 ustar 00root root 0000000 0000000 module Tins
# This class can create generator objects, that can produce all tuples, that
# would be created by as many for-loops as dimensions were given.
#
# The generator
# g = Tins::Generator[1..2, %w[a b c]]
# produces
# g.to_a # => [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"]]
#
# The 'each' method can be used to iterate over the tuples
# g.each { |a, b| puts "#{a} #{b}" }
# and Tins::Generator includes the Enumerable module, so
# Enumerable.instance_methods can be used as well:
# g.select { |a, b| %w[a c].include? b } # => [[1, "a"], [1, "c"], [2, "a"], [2, "c"]]
#
class Generator
include Enumerable
# Create a new Generator object from the enumberables _enums_.
def self.[](*enums)
new(enums)
end
# Create a new Generator instance. Use the objects in the Array _enums_
# as dimensions. The should all respond to the :each method (see module
# Enumerable in the core ruby library).
def initialize(enums)
@enums, @iterators, @n = [], [], 0
enums.each { |e| add_dimension(e) }
end
# Iterate over all tuples produced by this generator and yield to them.
def each(&block) # :yield: tuple
recurse(&block)
self
end
def recurse(tuple = [ nil ] * @n, i = 0, &block)
if i < @n - 1 then
@enums[i].__send__(@iterators[i]) do |x|
tuple[i] = x
recurse(tuple, i + 1, &block)
end
else
@enums[i].__send__(@iterators[i]) do |x|
tuple[i] = x
yield tuple.dup
end
end
end
private :recurse
# Add another dimension to this generator. _enum_ is an object, that ought
# to respond to the _iterator_ method (defaults to :each).
def add_dimension(enum, iterator = :each)
@enums << enum
@iterators << iterator
@n += 1
end
# Return the size of this generator, that is the number of its dimensions.
def size
@enums.size
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/go.rb 0000664 0000000 0000000 00000003173 12050546225 0016133 0 ustar 00root root 0000000 0000000 module Tins
module GO
module EnumerableExtension
def push(argument)
@arguments ||= []
@arguments.push argument
self
end
def each(&block)
@arguments.each(&block)
self
end
include Enumerable
end
module_function
# Parses the argument array _args_, according to the pattern _s_, to
# retrieve the single character command line options from it. If _s_ is
# 'xy:' an option '-x' without an option argument is searched, and an
# option '-y foo' with an option argument ('foo').
#
# An option hash is returned with all found options set to true or the
# found option argument.
def go(s, args = ARGV)
b, v = s.scan(/(.)(:?)/).inject([ {}, {} ]) { |t, (o, a)|
a = a == ':'
t[a ? 1 : 0][o] = a ? nil : false
t
}
while a = args.shift
a !~ /\A-(.+)/ and args.unshift a and break
p = $1
until p == ''
o = p.slice!(0, 1)
if v.key?(o)
if p == '' then
a = args.shift or break 1
else
a = p
end
if v[o].nil?
a = a.dup
a.extend EnumerableExtension
a.push a
v[o] = a
else
v[o].push a
end
break
elsif b.key?(o)
if b[o] == false
b[o]= 1
else
b[o] += 1
end
else
args.unshift a
break 1
end
end and break
end
b.merge(v)
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/hash_symbolize_keys_recursive.rb 0000664 0000000 0000000 00000001144 12050546225 0023664 0 ustar 00root root 0000000 0000000 module Tins
module HashSymbolizeKeysRecursive
def symbolize_keys_recursive
inject(self.class.new) do |h,(k, v)|
k = k.to_s
k.empty? and next
case v
when Hash
h[k.to_sym] = v.symbolize_keys_recursive
when Array
h[k.to_sym] = a = v.dup
v.each_with_index do |x, i|
Hash === x and a[i] = x.symbolize_keys_recursive
end
else
h[k.to_sym] = v
end
h
end
end
def symbolize_keys_recursive!
replace symbolize_keys_recursive
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/hash_union.rb 0000664 0000000 0000000 00000000411 12050546225 0017651 0 ustar 00root root 0000000 0000000 module Tins
module HashUnion
def |(other)
case
when other.respond_to?(:to_hash)
other = other.to_hash
when other.respond_to?(:to_h)
other = other.to_h
end
other.merge(self)
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/if_predicate.rb 0000664 0000000 0000000 00000000127 12050546225 0020140 0 ustar 00root root 0000000 0000000 module Tins
module IfPredicate
def if?
self ? self : nil
end
end
end
ruby-tins-0.6.0/lib/tins/limited.rb 0000664 0000000 0000000 00000001526 12050546225 0017155 0 ustar 00root root 0000000 0000000 require 'thread'
module Tins
class Limited
# Create a Limited instance, that runs _maximum_ threads at most.
def initialize(maximum)
@mutex = Mutex.new
@continue = ConditionVariable.new
@maximum = Integer(maximum)
raise ArgumentError, "maximum < 1" if @maximum < 1
@count = 0
end
# The maximum number of worker threads.
attr_reader :maximum
# Execute _maximum_ number of threads in parallel.
def execute
@mutex.synchronize do
loop do
if @count < @maximum
@count += 1
Thread.new do
yield
@mutex.synchronize { @count -= 1 }
@continue.signal
end
return
else
@continue.wait(@mutex)
end
end
end
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/lines_file.rb 0000664 0000000 0000000 00000004736 12050546225 0017645 0 ustar 00root root 0000000 0000000 module Tins
class LinesFile
module LineExtension
attr_reader :line_number
def filename
lines_file.filename.dup
end
end
def self.for_filename(filename, line_number = nil)
obj = new(File.readlines(filename), line_number)
obj.filename = filename
obj
end
def self.for_file(file, line_number = nil)
obj = new(file.readlines, line_number)
obj.filename = file.path
obj
end
def self.for_lines(lines, line_number = nil)
new(lines, line_number)
end
def initialize(lines, line_number = nil)
@lines = lines
@lines.each_with_index do |line, i|
line.extend LineExtension
line.instance_variable_set :@line_number, i + 1
line.instance_variable_set :@lines_file, self
end
instance_variable_set :@line_number, line_number || (@lines.empty? ? 0 : 1)
end
attr_accessor :filename
attr_reader :line_number
def rewind
self.line_number = 1
self
end
def next!
old = line_number
self.line_number += 1
line_number > old ? self : nil
end
def previous!
old = line_number
self.line_number -= 1
line_number < old ? self : nil
end
def line_number=(number)
number = number.to_i
if number > 0 && number <= last_line_number
@line_number = number
end
end
def last_line_number
@lines.size
end
def empty?
@lines.empty?
end
def each(&block)
empty? and return self
old_line_number = line_number
1.upto(last_line_number) do |number|
self.line_number = number
block.call(line)
end
self
ensure
self.line_number = old_line_number
end
include Enumerable
def line
index = line_number - 1
@lines[index] if index >= 0
end
def file_linenumber
"#{filename}:#{line_number}"
end
def match_backward(regexp, previous_after_match = false)
begin
if line =~ regexp
previous_after_match and previous!
return $~.captures
end
end while previous!
end
def match_forward(regexp, next_after_match = false)
begin
if line =~ regexp
next_after_match and next!
return $~.captures
end
end while next!
end
def to_s
"#{line_number} #{line.chomp}"
end
def inspect
"#<#{self.class}: #{to_s.inspect}>"
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/memoize.rb 0000664 0000000 0000000 00000004077 12050546225 0017177 0 ustar 00root root 0000000 0000000 module Tins
module Memoize
module CacheMethods
# Return the cache object.
def __memoize_cache__
@__memoize_cache__ ||= {}
end
# Clear cached values for all methods/functions.
def memoize_cache_clear
__memoize_cache__.clear
self
end
end
class ::Module
# Automatically memoize calls of the the methods +method_ids+. The
# memoized results do NOT ONLY depend on the arguments, but ALSO on the
# object the method is called on.
def memoize_method(*method_ids)
include CacheMethods
method_ids.each do |method_id|
method_id = method_id.to_s.to_sym
orig_method = instance_method(method_id)
__send__(:define_method, method_id) do |*args|
mc = __memoize_cache__
if mc.key?(method_id) and result = mc[method_id][args]
result
else
(mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
$DEBUG and warn "#{self.class} cached method #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect} [#{__id__}]"
end
result
end
end
end
include CacheMethods
# Automatically memoize calls of the functions +function_ids+. The
# memoized result does ONLY depend on the arguments given to the
# function.
def memoize_function(*function_ids)
mc = __memoize_cache__
function_ids.each do |method_id|
method_id = method_id.to_s.to_sym
orig_method = instance_method(method_id)
__send__(:define_method, method_id) do |*args|
if mc.key?(method_id) and result = mc[method_id][args]
result
else
(mc[method_id] ||= {})[args] = result = orig_method.bind(self).call(*args)
$DEBUG and warn "#{self.class} cached function #{method_id}(#{args.inspect unless args.empty?}) = #{result.inspect}"
end
result
end
end
end
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/minimize.rb 0000664 0000000 0000000 00000003362 12050546225 0017347 0 ustar 00root root 0000000 0000000 module Tins
# This module can be mixed into all classes, whose instances respond to the
# [] and size-methods, like for example Array. The returned elements from []
# should respond to the succ method.
module Minimize
# Returns a minimized version of this object, that is successive elements
# are substituted with ranges a..b. In the situation ..., x, y,... and y !=
# x.succ a range x..x is created, to make it easier to iterate over all the
# ranges in one run. A small example:
# [ 'A', 'B', 'C', 'G', 'K', 'L', 'M' ].minimize # => [ 'A'..'C', 'G'..'G', 'K'..'M' ]
#
# If the order of the original elements doesn't matter, it's a good idea to
# first sort them and then minimize:
# [ 5, 1, 4, 2 ].sort.minimize # => [ 1..2, 4..5 ]
def minimize
result = []
last_index = size - 1
size.times do |i|
result << [ self[0] ] if i == 0
if self[i].succ != self[i + 1] or i == last_index
result[-1] << self[i]
result << [ self[i + 1] ] unless i == last_index
end
end
result.map! { |a, b| a..b }
end
# First minimizes this object, then calls the replace method with the
# result.
def minimize!
replace minimize
end
# Invert a minimized version of an object. Some small examples:
# [ 'A'..'C', 'G'..'G', 'K'..'M' ].unminimize # => [ 'A', 'B', 'C', 'G', 'K', 'L', 'M' ]
# and
# [ 1..2, 4..5 ].unminimize # => [ 1, 2, 4, 5 ]
def unminimize
result = []
for range in self
for e in range
result << e
end
end
result
end
# Invert a minimized version of this object in place.
def unminimize!
replace unminimize
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/module_group.rb 0000664 0000000 0000000 00000000324 12050546225 0020222 0 ustar 00root root 0000000 0000000 module Tins
module ModuleGroup
def self.[](*modules)
modul = Module.new
modules.each do |m|
m.module_eval { include modul }
end
modul
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/null.rb 0000664 0000000 0000000 00000001330 12050546225 0016471 0 ustar 00root root 0000000 0000000 module Tins
# Implementation of the null object pattern in Ruby.
module Null
def method_missing(*)
self
end
def const_missing(*)
self
end
def to_s
''
end
def to_str
nil
end
def to_f
0.0
end
def to_i
0
end
def to_int
nil
end
def to_a
[]
end
def to_ary
nil
end
def inspect
'NULL'
end
def nil?
true
end
def blank?
true
end
module Kernel
def Null(value = nil)
value.nil? ? Tins::NULL : value
end
end
end
class NullClass < Module
include Tins::Null
end
NULL = NullClass.new.freeze
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/once.rb 0000664 0000000 0000000 00000001057 12050546225 0016451 0 ustar 00root root 0000000 0000000 module Tins
module Once
include File::Constants
module_function
def only_once(lock_filename = nil, locking_constant = nil)
lock_filename ||= $0
locking_constant ||= LOCK_EX
f = File.new(lock_filename, RDONLY)
f.flock(locking_constant) and yield
ensure
if f
f.flock LOCK_UN
f.close
end
end
def try_only_once(lock_filename = nil, locking_constant = nil, &block)
only_once(lock_filename, locking_constant || LOCK_EX | LOCK_NB, &block)
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/p.rb 0000664 0000000 0000000 00000001163 12050546225 0015762 0 ustar 00root root 0000000 0000000 require 'pp'
module Tins
module P
private
# Raise a runtime error with the inspected objects +objs+ (obtained by
# calling the #inspect method) as their message text. This is useful for
# quick debugging.
def p!(*objs)
raise((objs.size < 2 ? objs.first : objs).inspect)
end
# Raise a runtime error with the inspected objects +objs+ (obtained by
# calling the #pretty_inspect method) as their message text. This is useful
# for quick debugging.
def pp!(*objs)
raise("\n" + (objs.size < 2 ? objs.first : objs).pretty_inspect.chomp)
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/partial_application.rb 0000664 0000000 0000000 00000001521 12050546225 0021540 0 ustar 00root root 0000000 0000000 module Tins
module PartialApplication
# If this module is included into a Proc (or similar object), it tampers
# with its Proc#arity method.
def self.included(modul)
modul.module_eval do
old_arity = instance_method(:arity)
define_method(:arity) do
@__arity__ or old_arity.bind(self).call
end
end
super
end
# Create a partial application of this Proc (or similar object) using
# _args_ as the already applied arguments.
def partial(*args)
if args.empty?
dup
elsif args.size > arity
raise ArgumentError, "wrong number of arguments (#{args.size} for #{arity})"
else
f = lambda { |*b| call(*(args + b)) }
f.instance_variable_set :@__arity__, arity - args.size
f
end
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/proc_compose.rb 0000664 0000000 0000000 00000000231 12050546225 0020206 0 ustar 00root root 0000000 0000000 module Tins
module ProcCompose
def compose(other)
self.class.new { |*args| call(*other.call(*args)) }
end
alias * compose
end
end
ruby-tins-0.6.0/lib/tins/range_plus.rb 0000664 0000000 0000000 00000000160 12050546225 0017656 0 ustar 00root root 0000000 0000000 module Tins
module RangePlus
def +(other)
to_a + other.to_a
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/require_maybe.rb 0000664 0000000 0000000 00000000242 12050546225 0020351 0 ustar 00root root 0000000 0000000 module Tins
module RequireMaybe
def require_maybe(library)
require library
rescue LoadError => e
block_given? and yield e
end
end
end
ruby-tins-0.6.0/lib/tins/responding.rb 0000664 0000000 0000000 00000000613 12050546225 0017672 0 ustar 00root root 0000000 0000000 module Tins
module Responding
def responding?(*method_names)
Class.new do
define_method(:to_s) do
"Responding to #{method_names * ', '}"
end
alias inspect to_s
define_method(:===) do |object|
method_names.all? do |method_name|
object.respond_to?(method_name)
end
end
end.new
end
end
end
ruby-tins-0.6.0/lib/tins/round.rb 0000664 0000000 0000000 00000002333 12050546225 0016652 0 ustar 00root root 0000000 0000000 module Tins
# A bit more versatile rounding for Ruby
module Round
def self.included(klass)
if klass.instance_method(:round)
klass.class_eval do
begin
alias_method :__old_round__, :round
remove_method :round
rescue NameError
end
end
super
else
raise NoMethodError, 'no round method found'
end
end
def round(places = nil)
if places == nil || places == 0
return __old_round__
elsif places.respond_to?(:to_int)
places = places.to_int
else
raise TypeError, "argument places has to be like an Integer"
end
if places < 0
max_places = -Math.log(self.abs + 1) / Math.log(10)
raise ArgumentError, "places has to be >= #{max_places.ceil}" if max_places > places
end
t = self
f = 10.0 ** places
t *= f
if t.infinite?
result = self
else
if t >= 0.0
t = (t + 0.5).floor
elsif t < 0.0
t = (t - 0.5).ceil
end
t /= f
result = t.nan? ? self : t
end
max_places and result = result.to_i # if places < 0
result
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/secure_write.rb 0000664 0000000 0000000 00000001244 12050546225 0020223 0 ustar 00root root 0000000 0000000 module Tins
module SecureWrite
# Write to a file atomically
def secure_write(filename, content = nil, mode = 'w')
temp = File.new(filename + ".tmp.#$$.#{Time.now.to_f}", mode)
if content.nil? and block_given?
yield temp
elsif !content.nil?
temp.write content
else
raise ArgumentError, "either content or block argument required"
end
temp.fsync
size = temp.stat.size
temp.close
File.rename temp.path, filename
size
ensure
if temp
!temp.closed? and temp.close
File.file?(temp.path) and File.unlink temp.path
end
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/shuffle.rb 0000664 0000000 0000000 00000000414 12050546225 0017155 0 ustar 00root root 0000000 0000000 module Tins
module Shuffle
# :nocov:
def shuffle!
(size - 1) .downto(1) do |i|
j = rand(i + 1)
self[i], self[j] = self[j], self[i]
end
self
end
def shuffle
dup.shuffle!
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/string_camelize.rb 0000664 0000000 0000000 00000000541 12050546225 0020701 0 ustar 00root root 0000000 0000000 module Tins
module StringCamelize
def camelize(first_letter = :upper)
case first_letter
when :upper, true
gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
when :lower, false
self[0].chr.downcase + camelize[1..-1]
end
end
alias camelcase camelize
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/string_underscore.rb 0000664 0000000 0000000 00000000445 12050546225 0021264 0 ustar 00root root 0000000 0000000 module Tins
module StringUnderscore
def underscore
word = dup
word.gsub!(/::/, '/')
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
word.tr!("-", "_")
word.downcase!
word
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/string_version.rb 0000664 0000000 0000000 00000003412 12050546225 0020575 0 ustar 00root root 0000000 0000000 require 'tins/xt/symbol_to_proc'
module Tins
module StringVersion
class Version
include Comparable
def initialize(string)
string =~ /\A[\.\d]+\z/ or raise ArgumentError, "#{string.inspect} isn't a version number"
@version = string.frozen? ? string.dup : string
end
def major
self[0]
end
def major=(number)
self[0] = number
end
def minor
self[1]
end
def minor=(number)
self[1] = number
end
def build
self[2]
end
def build=(number)
self[2] = number
end
def revision
self[3]
end
def revision=(number)
self[3] = number
end
def [](index)
array[index]
end
def []=(index, value)
value = value.to_i
value >= 0 or raise ArgumentError, "version numbers can't contain negative numbers like #{value}"
a = array
@array = nil
a[index] = value
a.map! { |x| x.nil? ? 0 : x }
@version.replace a * '.'
end
def succ!
self[-1] += 1
end
def pred!
self[-1] -= 1
end
def <=>(other)
pairs = array.zip(other.array)
pairs.map! { |a, b| [ a.nil? ? 0 : a, b.nil? ? 0 : b ] }
a, b = pairs.transpose
a <=> b
end
def ==(other)
(self <=> other).zero?
end
def array
@version.split('.').map(&:to_i)
end
alias to_a array
def to_s
@version
end
alias inspect to_s
def version
self
end
end
def version
if frozen?
Version.new(self)
else
@version ||= Version.new(self)
end
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/subhash.rb 0000664 0000000 0000000 00000002413 12050546225 0017157 0 ustar 00root root 0000000 0000000 module Tins
module Subhash
# Create a subhash from this hash, that only contains key-value pairs
# matching +patterns+ and return it. +patterns+ can be for example /^foo/
# to put 'foobar' and 'foobaz' or 'foo'/:foo to put 'foo' into the subhash.
#
# If a block is given this method yields to it after the first pattern
# matched with a 3-tuple of +(key, value, match_data)+ using the return
# value of the block as the value of the result hash. +match_data+ is a
# MatchData instance if the matching pattern was a regular rexpression
# otherwise it is nil.
def subhash(*patterns)
patterns.map! do |pat|
pat = pat.to_sym.to_s if pat.respond_to?(:to_sym)
pat.respond_to?(:match) ? pat : pat.to_s
end
result =
if default_proc
self.class.new(&default_proc)
else
self.class.new(default)
end
if block_given?
each do |k, v|
patterns.each { |pat|
if pat === k.to_s
result[k] = yield(k, v, $~)
break
end
}
end
else
each do |k, v|
result[k] = v if patterns.any? { |pat| pat === k.to_s }
end
end
result
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/time_dummy.rb 0000664 0000000 0000000 00000001470 12050546225 0017675 0 ustar 00root root 0000000 0000000 module Tins
module TimeDummy
def self.included(modul)
class << modul
alias really_new new
alias really_now now
remove_method :now rescue nil
remove_method :new rescue nil
attr_writer :dummy
def dummy(value = nil)
if value.nil?
@dummy
else
begin
old_dummy = @dummy
@dummy = value
yield
ensure
@dummy = old_dummy
end
end
end
def new(*a)
if dummy
dummy.dup
elsif caller.first =~ /now/
really_now
else
really_new(*a)
end
end
def now
new
end
end
super
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/to_proc.rb 0000664 0000000 0000000 00000000262 12050546225 0017167 0 ustar 00root root 0000000 0000000 module Tins
module ToProc
# :nocov:
def to_proc
lambda do |obj, *args|
obj.__send__(self, *args[0..-1])
end
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/uniq_by.rb 0000664 0000000 0000000 00000000251 12050546225 0017166 0 ustar 00root root 0000000 0000000 module Tins
module UniqBy
def uniq_by(&b)
b ||= lambda { |x| x }
inject({}) { |h, e| h[b[e]] ||= e; h }.values
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/version.rb 0000664 0000000 0000000 00000000417 12050546225 0017211 0 ustar 00root root 0000000 0000000 module Tins
# Tins version
VERSION = '0.6.0'
VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
VERSION_BUILD = VERSION_ARRAY[2] # :nodoc:
end
ruby-tins-0.6.0/lib/tins/write.rb 0000664 0000000 0000000 00000000714 12050546225 0016656 0 ustar 00root root 0000000 0000000 require 'tins/secure_write'
module Tins
module Write
def self.extended(modul)
modul.extend SecureWrite
if modul.respond_to?(:write)
$DEBUG and warn "Skipping inclusion of Tins::Write#write method, include Tins::Write::SecureWrite#secure_write instead"
else
class << modul; self; end.instance_eval do
alias_method :write, :secure_write
end
end
super
end
end
end
require 'tins/alias'
ruby-tins-0.6.0/lib/tins/xt.rb 0000664 0000000 0000000 00000001750 12050546225 0016160 0 ustar 00root root 0000000 0000000 require 'tins'
module Tins
require 'tins/xt/attempt'
require 'tins/xt/blank'
require 'tins/xt/count_by'
require 'tins/xt/deep_dup'
require 'tins/xt/file_binary'
require 'tins/xt/full'
require 'tins/xt/hash_symbolize_keys_recursive'
require 'tins/xt/hash_union'
require 'tins/xt/irb'
require 'tins/xt/named'
require 'tins/xt/null'
require 'tins/xt/p'
require 'tins/xt/partial_application'
require 'tins/xt/range_plus'
require 'tins/xt/require_maybe'
require 'tins/xt/round'
require 'tins/xt/secure_write'
require 'tins/xt/string'
require 'tins/xt/subhash'
require 'tins/xt/symbol_to_proc'
require 'tins/xt/time_dummy'
require 'tins/xt/date_dummy'
require 'tins/xt/date_time_dummy'
require 'tins/xt/uniq_by'
require 'tins/xt/write'
require 'tins/xt/if_predicate'
require 'tins/xt/ask_and_send'
require 'tins/xt/extract_last_argument_options'
require 'tins/xt/deep_const_get'
require 'tins/xt/responding'
require 'tins/xt/proc_compose'
end
ruby-tins-0.6.0/lib/tins/xt/ 0000775 0000000 0000000 00000000000 12050546225 0015630 5 ustar 00root root 0000000 0000000 ruby-tins-0.6.0/lib/tins/xt/ask_and_send.rb 0000664 0000000 0000000 00000000141 12050546225 0020562 0 ustar 00root root 0000000 0000000 require 'tins/ask_and_send'
module Tins
class ::Object
include Tins::AskAndSend
end
end
ruby-tins-0.6.0/lib/tins/xt/attempt.rb 0000664 0000000 0000000 00000000131 12050546225 0017626 0 ustar 00root root 0000000 0000000 require 'tins/attempt'
module Tins
class ::Object
include Tins::Attempt
end
end
ruby-tins-0.6.0/lib/tins/xt/blank.rb 0000664 0000000 0000000 00000001746 12050546225 0017254 0 ustar 00root root 0000000 0000000 module Tins
module Blank
module Object
def blank?
respond_to?(:empty?) ? empty? : !self
end
def present?
!blank?
end
end
module NilClass
def blank?
true
end
end
module FalseClass
def blank?
true
end
end
module TrueClass
def blank?
false
end
end
module Array
def self.included(modul)
modul.module_eval do
alias_method :blank?, :empty?
end
end
end
module Hash
def self.included(modul)
modul.module_eval do
alias_method :blank?, :empty?
end
end
end
module String
def blank?
self !~ /\S/
end
end
module Numeric
def blank?
false
end
end
end
unless Object.respond_to?(:blank?)
for k in Blank.constants
Object.const_get(k).class_eval do
include Blank.const_get(k)
end
end
end
end
ruby-tins-0.6.0/lib/tins/xt/count_by.rb 0000664 0000000 0000000 00000000204 12050546225 0017773 0 ustar 00root root 0000000 0000000 require 'tins/count_by'
module Tins
module ::Enumerable
include CountBy
end
class ::Array
include CountBy
end
end
ruby-tins-0.6.0/lib/tins/xt/date_dummy.rb 0000664 0000000 0000000 00000000126 12050546225 0020304 0 ustar 00root root 0000000 0000000 require 'tins/date_dummy'
module Tins
class ::Date
include DateDummy
end
end
ruby-tins-0.6.0/lib/tins/xt/date_time_dummy.rb 0000664 0000000 0000000 00000000143 12050546225 0021321 0 ustar 00root root 0000000 0000000 require 'tins/date_time_dummy'
module Tins
class ::DateTime
include DateTimeDummy
end
end
ruby-tins-0.6.0/lib/tins/xt/deep_const_get.rb 0000664 0000000 0000000 00000000137 12050546225 0021140 0 ustar 00root root 0000000 0000000 require 'tins/deep_const_get'
module Tins
class ::Object
include DeepConstGet
end
end
ruby-tins-0.6.0/lib/tins/xt/deep_dup.rb 0000664 0000000 0000000 00000000132 12050546225 0017736 0 ustar 00root root 0000000 0000000 require 'tins/deep_dup'
module Tins
class ::Object
include Tins::DeepDup
end
end
ruby-tins-0.6.0/lib/tins/xt/extract_last_argument_options.rb 0000664 0000000 0000000 00000000173 12050546225 0024330 0 ustar 00root root 0000000 0000000 require 'tins/extract_last_argument_options'
module Tins
class ::Array
include ExtractLastArgumentOptions
end
end
ruby-tins-0.6.0/lib/tins/xt/file_binary.rb 0000664 0000000 0000000 00000000130 12050546225 0020432 0 ustar 00root root 0000000 0000000 require 'tins/file_binary'
module Tins
class ::File
include FileBinary
end
end
ruby-tins-0.6.0/lib/tins/xt/full.rb 0000664 0000000 0000000 00000001774 12050546225 0017130 0 ustar 00root root 0000000 0000000 require 'tins/xt/blank'
module Tins
module Full
# Returns the object if it isn't blank (as in Object#blank?), otherwise it
# returns nil. If a block was given as an argument and the object isn't
# blank, the block is executed with the object as its first argument. If an
# argument +dispatch+ was given and the object wasn't blank the method
# given by dispatch is called on the object. This is the same as
# foo.full?(&:bar) in the previous block form.
def full?(dispatch = nil, *args)
if blank?
obj = nil
#elsif Module === dispatch # TODO
# dispatch.found?(self)
elsif dispatch
obj = __send__(dispatch, *args)
obj = nil if obj.blank?
else
obj = self
end
if block_given? and obj
yield obj
else
obj
end
end
def all_full?
if respond_to?(:all?) && all?(&:full?)
block_given? ? yield(self) : self
end
end
end
class ::Object
include Full
end
end
ruby-tins-0.6.0/lib/tins/xt/hash_symbolize_keys_recursive.rb 0000664 0000000 0000000 00000000172 12050546225 0024317 0 ustar 00root root 0000000 0000000 require 'tins/hash_symbolize_keys_recursive'
module Tins
class ::Hash
include HashSymbolizeKeysRecursive
end
end
ruby-tins-0.6.0/lib/tins/xt/hash_union.rb 0000664 0000000 0000000 00000000326 12050546225 0020311 0 ustar 00root root 0000000 0000000 require 'tins/hash_union'
module Tins
class ::Hash
if method_defined?(:|)
warn "#{self}#| already defined, didn't include at #{__FILE__}:#{__LINE__}"
else
include HashUnion
end
end
end
ruby-tins-0.6.0/lib/tins/xt/if_predicate.rb 0000664 0000000 0000000 00000000142 12050546225 0020570 0 ustar 00root root 0000000 0000000 require 'tins/if_predicate'
module Tins
class ::Object
include Tins::IfPredicate
end
end
ruby-tins-0.6.0/lib/tins/xt/irb.rb 0000664 0000000 0000000 00000000654 12050546225 0016736 0 ustar 00root root 0000000 0000000 require 'irb'
module Tins
IRB = ::IRB
module ::IRB
def self.examine(binding = TOPLEVEL_BINDING)
setup nil
workspace = WorkSpace.new binding
irb = Irb.new workspace
@CONF[:MAIN_CONTEXT] = irb.context
catch(:IRB_EXIT) { irb.eval_input }
rescue Interrupt
exit
end
end
class ::Object
def examine(binding = TOPLEVEL_BINDING)
IRB.examine(binding)
end
end
end
ruby-tins-0.6.0/lib/tins/xt/named.rb 0000664 0000000 0000000 00000001723 12050546225 0017244 0 ustar 00root root 0000000 0000000 require 'tins/xt/string_version'
class Object
if RUBY_VERSION.version >= '1.9'.version
def named(name, method, *args, &named_block)
extend Module.new {
define_method(name) do |*rest, &block|
block = named_block if named_block
__send__(method, *(args + rest), &block)
end
}
end
else
def named(name, method, *args, &block)
extend Module.new { define_method(name) { |*rest| __send__(method, *(args + rest), &block) } }
end
end
end
class Module
if RUBY_VERSION.version >= '1.9'.version
def named(name, method, *args, &named_block)
include Module.new {
define_method(name) do |*rest, &block|
block = named_block if named_block
__send__(method, *(args + rest), &block)
end
}
end
else
def named(name, method, *args, &block)
include Module.new { define_method(name) { |*rest| __send__(method, *(args + rest), &block) } }
end
end
end
ruby-tins-0.6.0/lib/tins/xt/null.rb 0000664 0000000 0000000 00000000162 12050546225 0017126 0 ustar 00root root 0000000 0000000 require 'tins/null'
module Tins
::NULL = Tins::NULL
class ::Object
include Tins::Null::Kernel
end
end
ruby-tins-0.6.0/lib/tins/xt/p.rb 0000664 0000000 0000000 00000000115 12050546225 0016411 0 ustar 00root root 0000000 0000000 require 'tins/p'
module Tins
class ::Object
include Tins::P
end
end
ruby-tins-0.6.0/lib/tins/xt/partial_application.rb 0000664 0000000 0000000 00000000237 12050546225 0022176 0 ustar 00root root 0000000 0000000 require 'tins/partial_application'
module Tins
class ::Proc
include PartialApplication
end
class ::Method
include PartialApplication
end
end
ruby-tins-0.6.0/lib/tins/xt/proc_compose.rb 0000664 0000000 0000000 00000000140 12050546225 0020640 0 ustar 00root root 0000000 0000000 require 'tins/proc_compose'
module Tins
class ::Proc
include Tins::ProcCompose
end
end
ruby-tins-0.6.0/lib/tins/xt/range_plus.rb 0000664 0000000 0000000 00000000330 12050546225 0020310 0 ustar 00root root 0000000 0000000 require 'tins/range_plus'
module Tins
class ::Range
if method_defined?(:+)
warn "#{self}#+ already defined, didn't include at #{__FILE__}:#{__LINE__}"
else
include RangePlus
end
end
end
ruby-tins-0.6.0/lib/tins/xt/require_maybe.rb 0000664 0000000 0000000 00000000144 12050546225 0021005 0 ustar 00root root 0000000 0000000 require 'tins/require_maybe'
module Tins
class ::Object
include Tins::RequireMaybe
end
end
ruby-tins-0.6.0/lib/tins/xt/responding.rb 0000664 0000000 0000000 00000000137 12050546225 0020326 0 ustar 00root root 0000000 0000000 require 'tins/responding'
module Tins
class ::Object
include Tins::Responding
end
end
ruby-tins-0.6.0/lib/tins/xt/round.rb 0000664 0000000 0000000 00000000232 12050546225 0017301 0 ustar 00root root 0000000 0000000 require 'tins/round'
module Tins
module Round
class ::Float
include Round
end
class ::Integer
include Round
end
end
end
ruby-tins-0.6.0/lib/tins/xt/secure_write.rb 0000664 0000000 0000000 00000000226 12050546225 0020655 0 ustar 00root root 0000000 0000000 require 'tins/secure_write'
module Tins
#class ::Object
# include Tins::SecureWrite
#end
class ::IO
extend Tins::SecureWrite
end
end
ruby-tins-0.6.0/lib/tins/xt/shuffle.rb 0000664 0000000 0000000 00000000336 12050546225 0017613 0 ustar 00root root 0000000 0000000 require 'tins/shuffle'
module Tins
class ::Array
if method_defined?(:shuffle)
warn "#{self}#shuffle already defined, didn't include at #{__FILE__}:#{__LINE__}"
else
include Shuffle
end
end
end
ruby-tins-0.6.0/lib/tins/xt/string.rb 0000664 0000000 0000000 00000000175 12050546225 0017466 0 ustar 00root root 0000000 0000000 module Tins
require 'tins/xt/string_camelize'
require 'tins/xt/string_underscore'
require 'tins/xt/string_version'
end
ruby-tins-0.6.0/lib/tins/xt/string_camelize.rb 0000664 0000000 0000000 00000000143 12050546225 0021332 0 ustar 00root root 0000000 0000000 module Tins
require 'tins/string_camelize'
class ::String
include StringCamelize
end
end
ruby-tins-0.6.0/lib/tins/xt/string_underscore.rb 0000664 0000000 0000000 00000000147 12050546225 0021716 0 ustar 00root root 0000000 0000000 module Tins
require 'tins/string_underscore'
class ::String
include StringUnderscore
end
end
ruby-tins-0.6.0/lib/tins/xt/string_version.rb 0000664 0000000 0000000 00000000142 12050546225 0021225 0 ustar 00root root 0000000 0000000 module Tins
require 'tins/string_version'
class ::String
include StringVersion
end
end
ruby-tins-0.6.0/lib/tins/xt/subhash.rb 0000664 0000000 0000000 00000000235 12050546225 0017612 0 ustar 00root root 0000000 0000000 require 'tins/subhash'
module Tins
class ::Hash
include Tins::Subhash
def subhash!(*patterns)
replace subhash(*patterns)
end
end
end
ruby-tins-0.6.0/lib/tins/xt/symbol_to_proc.rb 0000664 0000000 0000000 00000000200 12050546225 0021177 0 ustar 00root root 0000000 0000000 module Tins
unless ::Symbol.method_defined?(:to_proc)
# :nocov:
class ::Symbol
include ToProc
end
end
end
ruby-tins-0.6.0/lib/tins/xt/time_dummy.rb 0000664 0000000 0000000 00000000126 12050546225 0020325 0 ustar 00root root 0000000 0000000 require 'tins/time_dummy'
module Tins
class ::Time
include TimeDummy
end
end
ruby-tins-0.6.0/lib/tins/xt/uniq_by.rb 0000664 0000000 0000000 00000000271 12050546225 0017623 0 ustar 00root root 0000000 0000000 require 'tins/uniq_by'
module Tins
module ::Enumerable
include UniqBy
end
class ::Array
include UniqBy
def uniq_by!(&b)
replace uniq_by(&b)
end
end
end
ruby-tins-0.6.0/lib/tins/xt/write.rb 0000664 0000000 0000000 00000000203 12050546225 0017302 0 ustar 00root root 0000000 0000000 require 'tins/write'
module Tins
#class ::Object
# include Tins::Write
#end
class ::IO
extend Tins::Write
end
end
ruby-tins-0.6.0/metadata.yml 0000664 0000000 0000000 00000021177 12050546225 0015765 0 ustar 00root root 0000000 0000000 --- !ruby/object:Gem::Specification
name: tins
version: !ruby/object:Gem::Version
version: 0.6.0
prerelease:
platform: ruby
authors:
- Florian Frank
autorequire:
bindir: bin
cert_chain: []
date: 2012-10-02 00:00:00.000000000 Z
dependencies:
- !ruby/object:Gem::Dependency
name: gem_hadar
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: 0.1.8
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: 0.1.8
- !ruby/object:Gem::Dependency
name: test-unit
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '2.5'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: '2.5'
- !ruby/object:Gem::Dependency
name: utils
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
description: All the stuff that isn't good/big enough for a real library.
email: flori@ping.de
executables: []
extensions: []
extra_rdoc_files:
- README.rdoc
- lib/spruz.rb
- lib/tins/alias.rb
- lib/tins/ask_and_send.rb
- lib/tins/attempt.rb
- lib/tins/bijection.rb
- lib/tins/concern.rb
- lib/tins/count_by.rb
- lib/tins/date_dummy.rb
- lib/tins/date_time_dummy.rb
- lib/tins/deep_const_get.rb
- lib/tins/deep_dup.rb
- lib/tins/extract_last_argument_options.rb
- lib/tins/file_binary.rb
- lib/tins/find.rb
- lib/tins/generator.rb
- lib/tins/go.rb
- lib/tins/hash_symbolize_keys_recursive.rb
- lib/tins/hash_union.rb
- lib/tins/if_predicate.rb
- lib/tins/limited.rb
- lib/tins/lines_file.rb
- lib/tins/memoize.rb
- lib/tins/minimize.rb
- lib/tins/module_group.rb
- lib/tins/null.rb
- lib/tins/once.rb
- lib/tins/p.rb
- lib/tins/partial_application.rb
- lib/tins/proc_compose.rb
- lib/tins/range_plus.rb
- lib/tins/require_maybe.rb
- lib/tins/responding.rb
- lib/tins/round.rb
- lib/tins/secure_write.rb
- lib/tins/shuffle.rb
- lib/tins/string_camelize.rb
- lib/tins/string_underscore.rb
- lib/tins/string_version.rb
- lib/tins/subhash.rb
- lib/tins/time_dummy.rb
- lib/tins/to_proc.rb
- lib/tins/uniq_by.rb
- lib/tins/version.rb
- lib/tins/write.rb
- lib/tins/xt/ask_and_send.rb
- lib/tins/xt/attempt.rb
- lib/tins/xt/blank.rb
- lib/tins/xt/count_by.rb
- lib/tins/xt/date_dummy.rb
- lib/tins/xt/date_time_dummy.rb
- lib/tins/xt/deep_const_get.rb
- lib/tins/xt/deep_dup.rb
- lib/tins/xt/extract_last_argument_options.rb
- lib/tins/xt/file_binary.rb
- lib/tins/xt/full.rb
- lib/tins/xt/hash_symbolize_keys_recursive.rb
- lib/tins/xt/hash_union.rb
- lib/tins/xt/if_predicate.rb
- lib/tins/xt/irb.rb
- lib/tins/xt/named.rb
- lib/tins/xt/null.rb
- lib/tins/xt/p.rb
- lib/tins/xt/partial_application.rb
- lib/tins/xt/proc_compose.rb
- lib/tins/xt/range_plus.rb
- lib/tins/xt/require_maybe.rb
- lib/tins/xt/responding.rb
- lib/tins/xt/round.rb
- lib/tins/xt/secure_write.rb
- lib/tins/xt/shuffle.rb
- lib/tins/xt/string.rb
- lib/tins/xt/string_camelize.rb
- lib/tins/xt/string_underscore.rb
- lib/tins/xt/string_version.rb
- lib/tins/xt/subhash.rb
- lib/tins/xt/symbol_to_proc.rb
- lib/tins/xt/time_dummy.rb
- lib/tins/xt/uniq_by.rb
- lib/tins/xt/write.rb
- lib/tins/xt.rb
- lib/tins.rb
files:
- .gitignore
- .travis.yml
- Gemfile
- LICENSE
- README.rdoc
- Rakefile
- TODO
- VERSION
- lib/spruz.rb
- lib/tins.rb
- lib/tins/alias.rb
- lib/tins/ask_and_send.rb
- lib/tins/attempt.rb
- lib/tins/bijection.rb
- lib/tins/concern.rb
- lib/tins/count_by.rb
- lib/tins/date_dummy.rb
- lib/tins/date_time_dummy.rb
- lib/tins/deep_const_get.rb
- lib/tins/deep_dup.rb
- lib/tins/extract_last_argument_options.rb
- lib/tins/file_binary.rb
- lib/tins/find.rb
- lib/tins/generator.rb
- lib/tins/go.rb
- lib/tins/hash_symbolize_keys_recursive.rb
- lib/tins/hash_union.rb
- lib/tins/if_predicate.rb
- lib/tins/limited.rb
- lib/tins/lines_file.rb
- lib/tins/memoize.rb
- lib/tins/minimize.rb
- lib/tins/module_group.rb
- lib/tins/null.rb
- lib/tins/once.rb
- lib/tins/p.rb
- lib/tins/partial_application.rb
- lib/tins/proc_compose.rb
- lib/tins/range_plus.rb
- lib/tins/require_maybe.rb
- lib/tins/responding.rb
- lib/tins/round.rb
- lib/tins/secure_write.rb
- lib/tins/shuffle.rb
- lib/tins/string_camelize.rb
- lib/tins/string_underscore.rb
- lib/tins/string_version.rb
- lib/tins/subhash.rb
- lib/tins/time_dummy.rb
- lib/tins/to_proc.rb
- lib/tins/uniq_by.rb
- lib/tins/version.rb
- lib/tins/write.rb
- lib/tins/xt.rb
- lib/tins/xt/ask_and_send.rb
- lib/tins/xt/attempt.rb
- lib/tins/xt/blank.rb
- lib/tins/xt/count_by.rb
- lib/tins/xt/date_dummy.rb
- lib/tins/xt/date_time_dummy.rb
- lib/tins/xt/deep_const_get.rb
- lib/tins/xt/deep_dup.rb
- lib/tins/xt/extract_last_argument_options.rb
- lib/tins/xt/file_binary.rb
- lib/tins/xt/full.rb
- lib/tins/xt/hash_symbolize_keys_recursive.rb
- lib/tins/xt/hash_union.rb
- lib/tins/xt/if_predicate.rb
- lib/tins/xt/irb.rb
- lib/tins/xt/named.rb
- lib/tins/xt/null.rb
- lib/tins/xt/p.rb
- lib/tins/xt/partial_application.rb
- lib/tins/xt/proc_compose.rb
- lib/tins/xt/range_plus.rb
- lib/tins/xt/require_maybe.rb
- lib/tins/xt/responding.rb
- lib/tins/xt/round.rb
- lib/tins/xt/secure_write.rb
- lib/tins/xt/shuffle.rb
- lib/tins/xt/string.rb
- lib/tins/xt/string_camelize.rb
- lib/tins/xt/string_underscore.rb
- lib/tins/xt/string_version.rb
- lib/tins/xt/subhash.rb
- lib/tins/xt/symbol_to_proc.rb
- lib/tins/xt/time_dummy.rb
- lib/tins/xt/uniq_by.rb
- lib/tins/xt/write.rb
- tests/ask_and_send_test.rb
- tests/bijection_test.rb
- tests/blank_full_test.rb
- tests/concern_test.rb
- tests/count_by_test.rb
- tests/date_dummy_test.rb
- tests/date_time_dummy_test.rb
- tests/deep_const_get_test.rb
- tests/deep_dup_test.rb
- tests/extract_last_argument_options_test.rb
- tests/file_binary_test.rb
- tests/find_test.rb
- tests/generator_test.rb
- tests/go_test.rb
- tests/hash_symbolize_keys_recursive_test.rb
- tests/hash_union_test.rb
- tests/if_predicate_test.rb
- tests/limited_test.rb
- tests/lines_file_test.rb
- tests/memoize_test.rb
- tests/minimize_test.rb
- tests/module_group_test.rb
- tests/named_test.rb
- tests/null_test.rb
- tests/p_test.rb
- tests/partial_application_test.rb
- tests/proc_compose_test.rb
- tests/range_plus_test.rb
- tests/require_maybe_test.rb
- tests/responding_test.rb
- tests/round_test.rb
- tests/secure_write_test.rb
- tests/shuffle_test.rb
- tests/string_camelize_test.rb
- tests/string_underscore_test.rb
- tests/string_version_test.rb
- tests/subhash_test.rb
- tests/test_helper.rb
- tests/time_dummy_test.rb
- tests/try_test.rb
- tests/uniq_by_test.rb
- tins.gemspec
homepage: http://flori.github.com/tins
licenses: []
post_install_message:
rdoc_options:
- --title
- Tins - Useful stuff.
- --main
- README.rdoc
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
segments:
- 0
hash: 4391703042856250428
required_rubygems_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
requirements: []
rubyforge_project:
rubygems_version: 1.8.24
signing_key:
specification_version: 3
summary: Useful stuff.
test_files:
- tests/ask_and_send_test.rb
- tests/bijection_test.rb
- tests/blank_full_test.rb
- tests/concern_test.rb
- tests/count_by_test.rb
- tests/date_dummy_test.rb
- tests/date_time_dummy_test.rb
- tests/deep_const_get_test.rb
- tests/deep_dup_test.rb
- tests/extract_last_argument_options_test.rb
- tests/file_binary_test.rb
- tests/find_test.rb
- tests/generator_test.rb
- tests/go_test.rb
- tests/hash_symbolize_keys_recursive_test.rb
- tests/hash_union_test.rb
- tests/if_predicate_test.rb
- tests/limited_test.rb
- tests/lines_file_test.rb
- tests/memoize_test.rb
- tests/minimize_test.rb
- tests/module_group_test.rb
- tests/named_test.rb
- tests/null_test.rb
- tests/p_test.rb
- tests/partial_application_test.rb
- tests/proc_compose_test.rb
- tests/range_plus_test.rb
- tests/require_maybe_test.rb
- tests/responding_test.rb
- tests/round_test.rb
- tests/secure_write_test.rb
- tests/shuffle_test.rb
- tests/string_camelize_test.rb
- tests/string_underscore_test.rb
- tests/string_version_test.rb
- tests/subhash_test.rb
- tests/test_helper.rb
- tests/time_dummy_test.rb
- tests/try_test.rb
- tests/uniq_by_test.rb
ruby-tins-0.6.0/tests/ 0000775 0000000 0000000 00000000000 12050546225 0014614 5 ustar 00root root 0000000 0000000 ruby-tins-0.6.0/tests/ask_and_send_test.rb 0000664 0000000 0000000 00000001114 12050546225 0020606 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/xt'
module Tins
class AskAndSendTest < Test::Unit::TestCase
class A
public
def foo
:foo
end
private
def bar
:bar
end
end
def test_asking_publicly
assert_equal :foo, A.new.ask_and_send(:foo)
assert_nil A.new.ask_and_send(:bar)
assert_nil A.new.ask_and_send(:baz)
end
def test_asking_privately
assert_equal :foo, A.new.ask_and_send!(:foo)
assert_equal :bar, A.new.ask_and_send!(:bar)
assert_nil A.new.ask_and_send(:baz)
end
end
end
ruby-tins-0.6.0/tests/bijection_test.rb 0000664 0000000 0000000 00000000711 12050546225 0020145 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins'
module Tins
class BijectionTest < Test::Unit::TestCase
def test_bijection
assert_equal [ [ 1, 2 ], [ 3, 4 ] ], Tins::Bijection[ 1, 2, 3, 4 ].to_a.sort
assert_raise(ArgumentError) do
Tins::Bijection[1,2,3]
end
assert_raise(ArgumentError) do
Tins::Bijection[1,2,3,2]
end
assert_raise(ArgumentError) do
Tins::Bijection[1,2,1,3]
end
end
end
end
ruby-tins-0.6.0/tests/blank_full_test.rb 0000664 0000000 0000000 00000003617 12050546225 0020320 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/xt'
require 'set'
module Tins
class BlankFullTest < Test::Unit::TestCase
def test_blank
assert !true.blank?
assert false.blank?
assert nil.blank?
assert [].blank?
assert ![23].blank?
assert Set[].blank?
assert !Set[23].blank?
assert({}.blank?)
assert !{ :foo => 23 }.blank?
assert "".blank?
assert " ".blank?
assert !"foo".blank?
end
def test_present
assert true.present?
assert !false.present?
assert !nil.present?
assert ![].present?
assert [23].present?
assert !Set[].present?
assert Set[23].present?
assert !{}.present?
assert({ :foo => 23 }.present?)
assert !"".present?
assert !" ".present?
assert "foo".present?
end
def test_full
assert_equal true, true.full?
assert_nil false.full?
assert_nil nil.full?
assert_nil [].full?
assert_equal [ 23 ], [ 23 ].full?
assert_nil Set[].full?
assert_equal Set[23], Set[23].full?
assert_nil({}.full?)
assert_equal({ :foo => 23 }, { :foo => 23 }.full?)
assert_nil "".full?
assert_nil " ".full?
assert_equal "foo", "foo".full?
assert_nil " ".full?(&:size)
assert_equal 3, "foo".full?(&:size)
assert_nil " ".full?(&:size)
assert_equal 3, "foo".full?(&:size)
assert_nil " ".full?(:size)
assert_equal 3, "foo".full?(:size)
assert_nil " ".full?(:size)
assert_equal 3, "foo".full?(:size)
end
def test_all_full
assert_equal [1, 2], [1, 2].all_full?
assert_nil [nil, 2].all_full?
assert_nil [1, ''].all_full?
end
def test_all_full_with_block
[1, 2].all_full? do |x, y|
assert_equal 1, x
assert_equal 2, y
end
['', 2].all_full? do |x, y|
assert false
end
end
end
end
ruby-tins-0.6.0/tests/concern_test.rb 0000664 0000000 0000000 00000001127 12050546225 0017630 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/concern'
module Tins
class ConcernTest < Test::Unit::TestCase
module ConcernTroll
extend ::Tins::Concern
included do
@included = :included
end
module ClassMethods
def class_foo
:class_foo
end
end
def foo
:foo
end
end
class Troll
include ConcernTroll
end
def test_concern
assert_equal :class_foo, Troll.class_foo
assert_equal :foo, Troll.new.foo
assert_equal :included, Troll.instance_variable_get(:@included)
end
end
end
ruby-tins-0.6.0/tests/count_by_test.rb 0000664 0000000 0000000 00000001054 12050546225 0020022 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/xt'
module Tins
class CountByTest < Test::Unit::TestCase
def test_count_by
assert_equal 0, [].count_by { |x| x % 2 == 0 }
assert_equal 0, [ 1 ].count_by { |x| x % 2 == 0 }
assert_equal 1, [ 1 ].count_by { |x| x % 2 == 1 }
assert_equal 1, [ 1, 2 ].count_by { |x| x % 2 == 0 }
assert_equal 1, [ 1, 2 ].count_by { |x| x % 2 == 1 }
assert_equal 2, [ 1, 2, 3, 4, 5 ].count_by { |x| x % 2 == 0 }
assert_equal 3, [ 1, 2, 3, 4, 5 ].count_by { |x| x % 2 == 1 }
end
end
end
ruby-tins-0.6.0/tests/date_dummy_test.rb 0000664 0000000 0000000 00000001352 12050546225 0020331 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins'
module Tins
class DateDummyTest < Test::Unit::TestCase
require 'tins/xt/date_dummy'
require 'date'
def test_date_dummy
date = Date.parse('2009-09-09')
assert_not_equal date, Date.today
Date.dummy = date
assert_equal date, Date.today
Date.dummy = nil
assert_not_equal date, Date.today
end
def test_date_dummy_block
date = Date.parse('2009-09-09')
assert_not_equal date, Date.today
Date.dummy date do
assert_equal date, Date.today
Date.dummy date + 1 do
assert_equal date + 1, Date.today
end
assert_equal date, Date.today
end
assert_not_equal date, Date.today
end
end
end
ruby-tins-0.6.0/tests/date_time_dummy_test.rb 0000664 0000000 0000000 00000001556 12050546225 0021355 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins'
module Tins
class DateTimeDummyTest < Test::Unit::TestCase
require 'tins/xt/date_time_dummy'
require 'date'
def test_time_dummy
date_time = DateTime.parse('2009-09-09 21:09:09')
assert_not_equal date_time, DateTime.now
DateTime.dummy = date_time
assert_equal date_time, DateTime.now
DateTime.dummy = nil
assert_not_equal date_time, DateTime.now
end
def test_time_dummy_block
date_time = DateTime.parse('2009-09-09 21:09:09')
assert_not_equal date_time, DateTime.now
DateTime.dummy date_time do
assert_equal date_time, DateTime.now
DateTime.dummy date_time + 1 do
assert_equal date_time + 1, DateTime.now
end
assert_equal date_time, DateTime.now
end
assert_not_equal date_time, DateTime.now
end
end
end
ruby-tins-0.6.0/tests/deep_const_get_test.rb 0000664 0000000 0000000 00000002020 12050546225 0021154 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/xt/deep_const_get'
module Tins
class DeepConstGetTest < Test::Unit::TestCase
module A
module B
end
end
module C
module NotB
end
def self.const_missing(c)
NotB
end
end
def test_deep_const_get_with_start_module
assert_raise(ArgumentError) { deep_const_get '::B', A }
assert_equal A::B, deep_const_get('B', A)
end
def test_deep_const_get_without_start_module
assert_equal Tins::DeepConstGetTest::A::B, deep_const_get('::Tins::DeepConstGetTest::A::B')
assert_equal Tins::DeepConstGetTest::A::B, deep_const_get('Tins::DeepConstGetTest::A::B')
assert_equal Array, deep_const_get('::Array')
assert_equal Array, deep_const_get('Array')
end
def test_deep_const_get_with_const_missing
assert_raise(ArgumentError) { deep_const_get '::Tins::DeepConstGetTest::A::D' }
assert_equal Tins::DeepConstGetTest::C::NotB, deep_const_get('::Tins::DeepConstGetTest::C::B')
end
end
end
ruby-tins-0.6.0/tests/deep_dup_test.rb 0000664 0000000 0000000 00000000610 12050546225 0017762 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/xt'
module Tins
class DeepDupTest < Test::Unit::TestCase
def test_deep_dup
a = [1,2,3]
assert_equal a, a.deep_dup
assert_not_same a, a.deep_dup
end
def test_deep_dup_proc
f = lambda { |x| 2 * x }
g = f.deep_dup
assert_equal f[3], g[3]
assert_equal f, g
assert_same f, g
end
end
end
ruby-tins-0.6.0/tests/extract_last_argument_options_test.rb 0000664 0000000 0000000 00000001626 12050546225 0024357 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins'
module Tins
class ExtractLastArgumentOptionsTest < Test::Unit::TestCase
require 'tins/xt/extract_last_argument_options'
def test_empty_argument_array
arguments = []
result = arguments.extract_last_argument_options
assert_equal [ [], {} ], result
assert_not_same arguments, result.first
end
def test_argument_array_without_options
arguments = [ 1, 2, 3 ]
result = arguments.extract_last_argument_options
assert_equal [ [ 1, 2, 3 ], {} ], result
assert_not_same arguments, result.first
end
def test_argument_array_witt_options
arguments = [ 1, 2, 3, { :foo => :bar } ]
result = arguments.extract_last_argument_options
assert_equal [ [ 1, 2, 3 ], { :foo => :bar } ], result
assert_not_same arguments, result.first
assert_not_same arguments.last, result.last
end
end
end
ruby-tins-0.6.0/tests/file_binary_test.rb 0000664 0000000 0000000 00000003521 12050546225 0020464 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tempfile'
require 'tins/xt'
module Tins
class TinsFileBinaryTest < Test::Unit::TestCase
def test_ascii_buffer_size
write_file do |file|
file.write "A" * 10 + "\x00"
assert_equal true, file.ascii?(:buffer_size => 10)
assert_equal true, File.ascii?(file.path, :buffer_size => 10)
assert_equal false, file.binary?(:buffer_size => 10)
assert_equal false, File.binary?(file.path, :buffer_size => 10)
end
end
def test_binary
write_file do |file|
file.write "A" * 69 + "\x01" * 31
assert_equal true, file.binary?
assert_equal true, File.binary?(file.path)
assert_equal false, file.ascii?
assert_equal false, File.ascii?(file.path)
end
end
def test_ascii_offset
write_file do |file|
file.write "\x01" * 31 + "A" * 70
assert_equal false, file.binary?(:offset => 1)
assert_equal false, File.binary?(file.path, :offset => 1)
assert_equal true, file.ascii?(:offset => 1)
assert_equal true, File.ascii?(file.path, :offset => 1)
end
end
def test_binary_zero
write_file do |file|
file.write "A" * 50 + "\0" + "A" * 49
assert_equal true, file.binary?
assert_equal true, File.binary?(file.path)
assert_equal false, file.ascii?
assert_equal false, File.ascii?(file.path)
end
end
def test_ascii
write_file do |file|
file.write "A" * 100
assert_equal false, file.binary?
assert_equal false, File.binary?(file.path)
assert_equal true, file.ascii?
assert_equal true, File.ascii?(file.path)
end
end
private
def write_file
File.open(File.join(Dir.tmpdir, "temp.#$$"), 'wb+') do |file|
yield file
end
end
end
end
ruby-tins-0.6.0/tests/find_test.rb 0000664 0000000 0000000 00000007725 12050546225 0017133 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/find'
require 'fileutils'
require 'tempfile'
module Tins
class FindTest < Test::Unit::TestCase
include Tins::Find
include FileUtils
def setup
mkdir_p @work_dir = File.join(Dir.tmpdir, "test.#$$")
end
def teardown
rm_rf @work_dir
end
def test_raising_errors
assert_equal [], find(File.join(@work_dir, 'nix'), :raise_errors => false).to_a
assert_equal [], find(File.join(@work_dir, 'nix')).to_a
assert_raise(Errno::ENOENT) do
find(File.join(@work_dir, 'nix'), :raise_errors => true).to_a
end
end
def test_showing_hidden
touch file = File.join(@work_dir, '.foo')
assert_equal [ @work_dir ], find(@work_dir, :show_hidden => false).to_a
assert_equal [ @work_dir, file ], find(@work_dir).to_a
assert_equal [ @work_dir, file ], find(@work_dir, :show_hidden => true).to_a
end
def test_check_directory_without_access
# do not run this test on JRuby
omit_if(RUBY_PLATFORM =~ /java/, "Can't run the test on JRuby")
# do not run this test if we're root, as it will fail.
omit_if(Process::UID.eid == 0, "Can't run the test as root")
begin
mkdir_p directory1 = File.join(@work_dir, 'foo')
mkdir_p directory2 = File.join(directory1, 'bar')
touch file = File.join(directory2, 'file')
chmod 0, directory2
assert_equal [ @work_dir, directory1, directory2 ], find(@work_dir, :raise_errors => false).to_a
assert_equal [ @work_dir, directory1, directory2 ], find(@work_dir).to_a
assert_raise(Errno::EACCES) do
find(@work_dir, :raise_errors => true).to_a
end
ensure
File.exist?(directory2) and chmod 0777, directory2
end
end
def test_follow_symlinks
mkdir_p directory1 = File.join(@work_dir, 'foo1')
mkdir_p directory2 = File.join(@work_dir, 'foo2')
mkdir_p directory3 = File.join(directory1, 'bar')
touch file = File.join(directory3, 'foo')
ln_s directory3, link = File.join(directory2, 'baz')
assert_equal [ directory2, link ], find(directory2, :follow_symlinks => false).to_a
assert_equal [ directory2, link, linked = File.join(link, 'foo') ], find(directory2).to_a
assert_equal [ directory2, link, linked ], find(directory2, :follow_symlinks => true).to_a
end
def test_path_file
File.open(File.join(@work_dir, 'foo'), 'w') do |f|
f.print "hello"
f.fsync
assert_equal "hello", find(@work_dir).select { |f| f.stat.file? }.first.file.read
end
end
def test_path_extension
finder = Tins::Find::Finder.new
f = File.open(path = File.join(@work_dir, 'foo.bar'), 'w')
ln_s path, path2 = File.join(@work_dir, 'foo2.bar')
path2 = finder.prepare_path path2
path = finder.prepare_path path
assert_true path.exist?
assert_true path.file?
assert_false path.directory?
assert_true finder.prepare_path(Dir.pwd).directory?
assert_equal Pathname.new(path), path.pathname
assert_equal 'bar', path.suffix
assert_true path2.lstat.symlink?
ensure
f and rm_f f.path
end
def test_suffix
finder = Tins::Find::Finder.new(:suffix => 'bar')
f = File.open(fpath = File.join(@work_dir, 'foo.bar'), 'w')
g = File.open(gpath = File.join(@work_dir, 'foo.baz'), 'w')
fpath = finder.prepare_path fpath
gpath = finder.prepare_path gpath
assert_true finder.visit_path?(fpath)
assert_false finder.visit_path?(gpath)
finder.suffix = nil
assert_true finder.visit_path?(fpath)
assert_true finder.visit_path?(gpath)
ensure
f and rm_f f.path
g and rm_f g.path
end
def test_prune
mkdir_p directory1 = File.join(@work_dir, 'foo1')
mkdir_p directory2 = File.join(@work_dir, 'foo2')
result = []
find(@work_dir) { |f| f =~ /foo2\z/ and prune; result << f }
assert_equal [ @work_dir, directory1 ], result
end
end
end
ruby-tins-0.6.0/tests/generator_test.rb 0000664 0000000 0000000 00000001756 12050546225 0020177 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins'
module Tins
class GeneratorTest < Test::Unit::TestCase
def setup
@numeric = [ 1, 2, 3 ]
@string = %w[a b c]
@chars = 'abc'
end
def test_generator
g = Tins::Generator[@numeric, @string]
assert_equal 2, g.size
g.add_dimension(@chars, :each_byte)
assert_equal 3, g.size
assert_equal\
[[1, "a", 97],
[1, "a", 98],
[1, "a", 99],
[1, "b", 97],
[1, "b", 98],
[1, "b", 99],
[1, "c", 97],
[1, "c", 98],
[1, "c", 99],
[2, "a", 97],
[2, "a", 98],
[2, "a", 99],
[2, "b", 97],
[2, "b", 98],
[2, "b", 99],
[2, "c", 97],
[2, "c", 98],
[2, "c", 99],
[3, "a", 97],
[3, "a", 98],
[3, "a", 99],
[3, "b", 97],
[3, "b", 98],
[3, "b", 99],
[3, "c", 97],
[3, "c", 98],
[3, "c", 99]], g.to_a
end
end
end
ruby-tins-0.6.0/tests/go_test.rb 0000664 0000000 0000000 00000002425 12050546225 0016610 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/go'
module Tins
class GoTest < Test::Unit::TestCase
include Tins::GO
def test_empty_string
r = go '', args = %w[a b c]
assert_equal({}, r)
assert_equal %w[a b c], args
end
def test_empty_args
r = go 'ab:', args = []
assert_equal({ 'a' => false, 'b' => nil }, r)
assert_equal [], args
end
def test_simple
r = go 'ab:', args = %w[-b hello -a -c rest]
assert_equal({ 'a' => 1, 'b' => 'hello' }, r)
assert_equal %w[-c rest], args
end
def test_complex
r = go 'ab:', args = %w[-a -b hello -a -bworld -c rest]
assert_equal({ 'a' => 2, 'b' => 'hello' }, r)
assert_equal %w[hello world], r['b'].to_a
assert_equal %w[-c rest], args
end
def test_complex2
r = go 'ab:', args = %w[-b hello -aa -b world -c rest]
assert_equal({ 'a' => 2, 'b' => 'hello' }, r)
assert_equal %w[hello world], r['b'].to_a
assert_equal %w[-c rest], args
end
def test_complex_frozen
args = %w[-b hello -aa -b world -c rest]
args = args.map(&:freeze)
r = go 'ab:', args
assert_equal({ 'a' => 2, 'b' => 'hello' }, r)
assert_equal %w[hello world], r['b'].to_a
assert_equal %w[-c rest], args
end
end
end
ruby-tins-0.6.0/tests/hash_symbolize_keys_recursive_test.rb 0000664 0000000 0000000 00000000774 12050546225 0024352 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins'
module Tins
class HashSymbolizeKeysRecursiveTest < Test::Unit::TestCase
require 'tins/xt/hash_symbolize_keys_recursive'
def test_symbolize
hash = {
'key' => [
{
'key' => {
'key' => true
}
}
],
}
hash2 = hash.symbolize_keys_recursive
assert hash2[:key][0][:key][:key]
hash.symbolize_keys_recursive!
assert hash[:key][0][:key][:key]
end
end
end
ruby-tins-0.6.0/tests/hash_union_test.rb 0000664 0000000 0000000 00000001735 12050546225 0020341 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/xt'
module Tins
class HashUnionTest < Test::Unit::TestCase
class HashLike1
def to_hash
{ 'foo' => true }
end
end
class HashLike2
def to_h
{ 'foo' => true }
end
end
def test_union
defaults = { 'foo' => true, 'bar' => false, 'quux' => nil }
hash = { 'foo' => false }
assert_equal [ ['bar', false], ['foo', false], ['quux', nil] ],
(hash | defaults).sort
hash |= defaults
assert_equal [ ['bar', false], ['foo', false], ['quux', nil] ],
hash.sort
hash = { 'foo' => false }
hash |= {
'quux' => true,
'baz' => 23,
} | defaults
assert_equal [ ['bar', false], [ 'baz', 23 ], ['foo', false],
['quux', true] ],
hash.sort
end
def test_hash_conversion
assert_equal({ 'foo' => true }, { } | HashLike1.new)
assert_equal({ 'foo' => true }, { } | HashLike2.new)
end
end
end
ruby-tins-0.6.0/tests/if_predicate_test.rb 0000664 0000000 0000000 00000000347 12050546225 0020622 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins/xt'
module Tins
class IfPredicateTest
def test_if_predicate
assert_equal :foo, true.if? && :foo
assert_nil false.if? && :foo
assert_nil nil.if? && :foo
end
end
end
ruby-tins-0.6.0/tests/limited_test.rb 0000664 0000000 0000000 00000000715 12050546225 0017632 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tins'
module Tins
class LimitedTest < Test::Unit::TestCase
class ::Array
include Tins::Shuffle
end
def test_limited
count = {}
limited = Tins::Limited.new(5)
5.times do
limited.execute do
count[Thread.current] = true
sleep
end
end
until count.size >= 5
sleep 0.1
end
assert_equal 5, count.keys.uniq.size
end
end
end
ruby-tins-0.6.0/tests/lines_file_test.rb 0000664 0000000 0000000 00000005133 12050546225 0020313 0 ustar 00root root 0000000 0000000 require 'test_helper'
require 'tempfile'
require 'tins/lines_file'
module Tins
class TinsLinesFileTest < Test::Unit::TestCase
FILE = <