Skip to content

Commit

Permalink
pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gutenye committed Aug 22, 2012
1 parent 7a23c19 commit 4d9effe
Show file tree
Hide file tree
Showing 35 changed files with 191 additions and 1,042 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/tags

/*.gem
/Gemfile
/spec/tmp/*
/.yardoc
/tags
4 changes: 2 additions & 2 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*tagen 2.0.0*
* based on 'active_support'

* remove 'PyFormat'
* a lot of methods come from 'active_support'
* remove 'PyFormat', 'Array#extract_extend_options[!]', 'Time:Deta'
12 changes: 4 additions & 8 deletions lib/tagen/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
require "tagen/core/enumerator"
require "tagen/core/array"
require "tagen/core/hash"
require "tagen/core/exception"
#require "tagen/core/extend_hash"
#require "tagen/core/re"

require "tagen/core/time"
#require "tagen/core/io"
#require "tagen/core/process"

#require "tagen/core/open_option"
require "tagen/core/exception"
require "tagen/core/io"
require "tagen/core/process"
require "tagen/core/re"

require "active_support/concern"
require "active_support/deprecation"
20 changes: 18 additions & 2 deletions lib/tagen/core/array/delete_values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,28 @@ def delete_values(*values, &blk)
end

# Deletes the element at the specified indexes.
#
#
# @example
#
# a = [1, 2, 3, 4]
# a.delete_values_at(0, 2) -> [1, 3]
# a -> [2, 4]
#
# @return [Array]
# @see Array#delete_at
def delete_values_at(*indexs, &blk)
offset = 0

# convert to positve index
indexs.map { |i| i < 0 ? length + i : i }

indexs.each.with_object([]) {|i,m|
m << delete_at(i, &blk)
if i > length
m << nil
else
m << delete_at(i-offset, &blk)
offset += 1
end
}
end
end
2 changes: 1 addition & 1 deletion lib/tagen/core/array/extract_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Array
# @see extract_options!
def extract_options(default={})
if last.is_a?(Hash) && last.extractable_options?
[self[0...-1], defalut.merge(self[-1])]
[self[0...-1], default.merge(self[-1])]
else
[self, default]
end
Expand Down
14 changes: 7 additions & 7 deletions lib/tagen/core/enumerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ class Enumerator
# @yieldparam [Object] (*args)
# @yieldparam [Fixnum] idx index
# @yieldparam [Object] memo_obj
def with_iobject *args, &blk
def with_iobject(*args, &blk)
return self.to_enum(:with_iobject, *args) unless blk

offset = args.find!{|v| Fixnum===v} || 0
raise ArgumentError "must provide memo_obj" if args.empty?
memo = args[0]
(offset,), (memo,) = args.partition{|v| Fixnum === v}
index = offset || 0
raise ArgumentError, "must provide memo object" unless memo

i = offset-1
self.with_object memo do |args, m|
blk.call args,i+=1,m
with_object(memo) do |args2, m|
blk.call args2, index, m
index += 1
end
end
end
52 changes: 8 additions & 44 deletions lib/tagen/core/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,60 +7,24 @@
=end
class IO
class << self

# a convient function to write text.
#
# @example
#
# File.write("/tmp/a", "hello world")
#
# @param [String] path
# @param [String] text
# @param [Hash] o options
# @option o [Boolean] :mkdir auto create missing directory in path
# @return [String]
def write(path, text, o={})
mkdir_p(File.dirname(path)) if o[:mkdir]

open(path, "w"){|f| f.write(text)}
end

# a convient function to append text.
#
# @overload append(name, string, [offset], [open_args])
#
# @example
#
# File.append("/tmp/a", "hello world")
#
# @param [String] path
# @param [String] text
# @return [String]
def append(path, text)
open(path, "a"){|f| f.write(text)}
end

private

# @param [Hash] o options
# @option o [Fixnum] :mode (0775)
def mkdir_p(path, o={})
return if File.exists?(path)
# @see IO.write
def append(name, string, *args)
(offset,), o = args.extract_options
o[:mode] = "a"

o[:mode] ||= 0775

stack = []
until path == stack.last
break if File.exists?(path)
stack << path
path = File.dirname(path)
end

stack.reverse.each do |p|
Dir.mkdir(p)
File.chmod(o[:mode], p)
end
write(name, string, offset, o)
end
end


alias fd fileno

end # class IO
34 changes: 0 additions & 34 deletions lib/tagen/core/marshal.rb

This file was deleted.

4 changes: 3 additions & 1 deletion lib/tagen/core/process.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "tagen/core/kernel/platform"

module Process
# check if the pid's process is running.
#
# @note for linux only
# @param [String, Integer] pid process id
# @return [Boolean]
def self.exists?(pid)
raise NotImplementError unless linux?
raise NotImplementedError unless linux?
File.exists?("/proc/#{pid}")
end
end
1 change: 0 additions & 1 deletion lib/tagen/core/re.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ def to_hash
Hash[names.map(&:to_sym).zip(captures)]
end
end

40 changes: 0 additions & 40 deletions lib/tagen/core/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,6 @@ class Time
def self.time
now.to_f
end

class Deta
class <<self
# @param [Time] from
# @param [Time] to
# @return [Time::Deta] deta
def deta(from, to)
seconds = (from-to).to_i
self.new(seconds)
end
end

attr_reader :years, :months, :days, :hours, :minutes, :seconds

def initialize(seconds)
deta = seconds
deta, @seconds = deta.divmod(60)
deta, @minutes = deta.divmod(60)
deta, @hours = deta.divmod(24)
deta, @days = deta.divmod(30)
@years, @months = deta.divmod(12)
end

def display(include_seconds=true)
ret = ""
ret << "#{years} years " unless years == 0
ret << "#{months} months " unless months == 0
ret << "#{days} days " unless days==0
ret << "#{hours} hours " unless hours == 0
ret << "#{minutes} minutes " unless minutes == 0
ret << "#{seconds} seconds" if include_seconds

ret
end

# to [years, months, days, hours minutes seconds]
def to_a
[ years, months, days, hours, minutes, seconds ]
end
end
end

require "active_support/time"
80 changes: 0 additions & 80 deletions lib/tagen/date.rb

This file was deleted.

14 changes: 7 additions & 7 deletions lib/tagen/erb.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
require 'erb'
require "erb"

class ERB
alias original_result result

# add locals support
#
# @example
# erb = Erb.new("<%=a%>")
# erb.result(nil, a: 1) #=> "1"
#
# @param [Hash,OpenOption] locals
def result(bind=nil, locals={})
def result_with_tagen(bind=nil, locals={})
bind ||= TOPLEVEL_BINDING
if locals.empty?
original_result bind
result_without_tagen bind
else
result_with_locals bind, locals
end
end

private
alias result_without_tagen result
alias result result_with_tagen

private
def result_with_locals(bind, locals)
@locals = locals
evalstr = <<-EOF
Expand All @@ -32,5 +33,4 @@ def run_erb
eval evalstr
run_erb
end

end
Loading

0 comments on commit 4d9effe

Please sign in to comment.