Permalink
Browse files

WIP - minor tweaks

  • Loading branch information...
1 parent cb54a9a commit 2afc703bb50d2f5c53a2f25469cfb30a69099fa6 @kbrock committed Dec 10, 2012
View
1 lib/stamp.rb
@@ -1,6 +1,7 @@
require "date"
require "time"
+require "stamp/emitters/simple"
require "stamp/emitters/am_pm_emitter"
require "stamp/emitters/composite_emitter"
require "stamp/emitters/delegate_emitter"
View
2 lib/stamp/emitters/composite_emitter.rb
@@ -16,8 +16,6 @@ def format(target)
def <<(emitter)
if emitter.is_a?(Enumerable)
emitter.each { |e| self << e }
- # elsif !emitter.is_a?(Emitter)
- # raise "not an emitter: #{emitter.nil? ? "nil" : emitter}"
elsif @emitters.last.is_a?(StringEmitter) && emitter.is_a?(StringEmitter)
@emitters.last << emitter
else
View
4 lib/stamp/emitters/lookup_emitter.rb
@@ -22,6 +22,10 @@ def lookup(value)
@lookup[value]
end
end
+
+ def regexp
@jeremyw
jeremyw Dec 11, 2012

Is this ever called?

@kbrock
kbrock Dec 12, 2012

couldn't get it to work - kill it

+ /^(#{@lookup.join('|')})$/i
+ end
end
end
end
View
6 lib/stamp/emitters/numeric_emitter.rb
@@ -1,18 +1,20 @@
module Stamp
module Emitters
class NumericEmitter
+ IDENTITY = lambda() {|d| d}
attr_reader :field
# @param [field] the field to be formatted (e.g.: mon, year)
# @param [options]
- def initialize(field, options={})
+ def initialize(field, options={}, &block)
@field = field
@options = options
+ @options[:modifier] ||= block || IDENTITY
end
def format(target)
value = target.send(field)
- value = @options[:modifier].call(value) if @options[:modifier]
+ value = @options[:modifier].call(value)
value = '%2.2d' % value if @options[:leading_zero]
value.to_s
end
View
4 lib/stamp/emitters/ordinal_emitter.rb
@@ -24,10 +24,6 @@ def ordinalize(number)
end
end
end
-
- def blank?
- false
- end
end
end
end
View
18 lib/stamp/emitters/simple.rb
@@ -0,0 +1,18 @@
+module Stamp
+ module Emitters
+ class Simple
+ IDENTITY = lambda() {|d| d}
+ attr_accessor :field
@jeremyw
jeremyw Dec 10, 2012

I'd use attr_reader on :field to make it immutable.

+ attr_accessor :modifier
@jeremyw
jeremyw Dec 10, 2012

Don't think you need the attr_accessor on :modifier. The reader is never used, and the setter should never be used (these objects should be immutable, I think).

+ def initialize(field=nil, legacy=nil, &block)
+ @field=field
+ @modifier=block || IDENTITY
+ end
+
+ def format(d)
+ @modifier.call(d.send(@field))
+ end
+ alias :call :format
+ end
+ end
+end
View
6 lib/stamp/translator.rb
@@ -25,10 +25,10 @@ class StrftimeTranslator
OBVIOUS_DAYS = 13..31
OBVIOUS_24_HOUR = 13..23
- TWO_DIGIT_YEAR_EMITTER = Emitters::NumericEmitter.new(:year, :leading_zero => true, :modifier => lambda { |year| year % 100 })
+ TWO_DIGIT_YEAR_EMITTER = Emitters::NumericEmitter.new(:year, :leading_zero => true) { |year| year % 100 }
TWO_DIGIT_MONTH_EMITTER = Emitters::NumericEmitter.new(:month, :leading_zero => true)
TWO_DIGIT_DAY_EMITTER = Emitters::NumericEmitter.new(:day, :leading_zero => true)
- HOUR_TO_12_HOUR = lambda { |h| h = h % 12; h == 0 ? 12 : h }
+ HOUR_TO_12_HOUR = lambda { |h| (h-1) % 12 + 1 }
@jeremyw
jeremyw Dec 10, 2012

If this is equivalent behavior, I like it much better!

@kbrock
kbrock Dec 11, 2012

added a test

OBVIOUS_DATE_MAP = {
OBVIOUS_YEARS => TWO_DIGIT_YEAR_EMITTER,
@@ -47,7 +47,6 @@ class StrftimeTranslator
:min => Emitters::NumericEmitter.new(:sec, :leading_zero => true)
}
-
def translate(example)
# extract any substrings that look like times, like "23:59" or "8:37 am"
before, time_example, after = example.partition(TIME_REGEXP)
@@ -154,6 +153,5 @@ def date_emitter(token, previous_part)
Emitters::NumericEmitter.new(:day)
end
end
-
end
end

0 comments on commit 2afc703

Please sign in to comment.