Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

WIP - minor tweaks

  • Loading branch information...
commit 2afc703bb50d2f5c53a2f25469cfb30a69099fa6 1 parent cb54a9a
Keenan Brock authored December 09, 2012
1  lib/stamp.rb
... ...
@@ -1,6 +1,7 @@
1 1
 require "date"
2 2
 require "time"
3 3
 
  4
+require "stamp/emitters/simple"
4 5
 require "stamp/emitters/am_pm_emitter"
5 6
 require "stamp/emitters/composite_emitter"
6 7
 require "stamp/emitters/delegate_emitter"
2  lib/stamp/emitters/composite_emitter.rb
@@ -16,8 +16,6 @@ def format(target)
16 16
       def <<(emitter)
17 17
         if emitter.is_a?(Enumerable)
18 18
           emitter.each { |e| self << e }
19  
-        # elsif !emitter.is_a?(Emitter)
20  
-        #   raise "not an emitter: #{emitter.nil? ? "nil" : emitter}"
21 19
         elsif @emitters.last.is_a?(StringEmitter) && emitter.is_a?(StringEmitter)
22 20
           @emitters.last << emitter
23 21
         else
4  lib/stamp/emitters/lookup_emitter.rb
@@ -22,6 +22,10 @@ def lookup(value)
22 22
           @lookup[value]
23 23
         end
24 24
       end
  25
+
  26
+      def regexp
  27
+        /^(#{@lookup.join('|')})$/i
  28
+      end
25 29
     end
26 30
   end
27 31
 end
6  lib/stamp/emitters/numeric_emitter.rb
... ...
@@ -1,18 +1,20 @@
1 1
 module Stamp
2 2
   module Emitters
3 3
     class NumericEmitter
  4
+      IDENTITY = lambda() {|d| d}
4 5
       attr_reader :field
5 6
 
6 7
       # @param [field] the field to be formatted (e.g.: mon, year)
7 8
       # @param [options]
8  
-      def initialize(field, options={})
  9
+      def initialize(field, options={}, &block)
9 10
         @field = field
10 11
         @options = options
  12
+        @options[:modifier] ||= block || IDENTITY
11 13
       end
12 14
 
13 15
       def format(target)
14 16
         value = target.send(field)
15  
-        value = @options[:modifier].call(value) if @options[:modifier]
  17
+        value = @options[:modifier].call(value)
16 18
         value = '%2.2d' % value if @options[:leading_zero]
17 19
         value.to_s
18 20
       end
4  lib/stamp/emitters/ordinal_emitter.rb
@@ -24,10 +24,6 @@ def ordinalize(number)
24 24
           end
25 25
         end
26 26
       end
27  
-
28  
-      def blank?
29  
-        false
30  
-      end
31 27
     end
32 28
   end
33 29
 end
18  lib/stamp/emitters/simple.rb
... ...
@@ -0,0 +1,18 @@
  1
+module Stamp
  2
+  module Emitters
  3
+    class Simple
  4
+      IDENTITY = lambda() {|d| d}
  5
+      attr_accessor :field
  6
+      attr_accessor :modifier
  7
+      def initialize(field=nil, legacy=nil, &block)
  8
+        @field=field
  9
+        @modifier=block || IDENTITY
  10
+      end
  11
+
  12
+      def format(d)
  13
+        @modifier.call(d.send(@field))
  14
+      end
  15
+      alias :call :format
  16
+    end
  17
+  end
  18
+end
6  lib/stamp/translator.rb
@@ -25,10 +25,10 @@ class StrftimeTranslator
25 25
     OBVIOUS_DAYS           = 13..31
26 26
     OBVIOUS_24_HOUR        = 13..23
27 27
 
28  
-    TWO_DIGIT_YEAR_EMITTER  = Emitters::NumericEmitter.new(:year, :leading_zero => true, :modifier => lambda { |year| year % 100 })
  28
+    TWO_DIGIT_YEAR_EMITTER  = Emitters::NumericEmitter.new(:year, :leading_zero => true) { |year| year % 100 }
29 29
     TWO_DIGIT_MONTH_EMITTER = Emitters::NumericEmitter.new(:month, :leading_zero => true)
30 30
     TWO_DIGIT_DAY_EMITTER   = Emitters::NumericEmitter.new(:day, :leading_zero => true)
31  
-    HOUR_TO_12_HOUR         = lambda { |h| h = h % 12; h == 0 ? 12 : h }
  31
+    HOUR_TO_12_HOUR         = lambda { |h| (h-1) % 12 + 1 }
32 32
 
33 33
     OBVIOUS_DATE_MAP = {
34 34
       OBVIOUS_YEARS  => TWO_DIGIT_YEAR_EMITTER,
@@ -47,7 +47,6 @@ class StrftimeTranslator
47 47
       :min  => Emitters::NumericEmitter.new(:sec, :leading_zero => true)
48 48
     }
49 49
 
50  
-
51 50
     def translate(example)
52 51
       # extract any substrings that look like times, like "23:59" or "8:37 am"
53 52
       before, time_example, after = example.partition(TIME_REGEXP)
@@ -154,6 +153,5 @@ def date_emitter(token, previous_part)
154 153
         Emitters::NumericEmitter.new(:day)
155 154
       end
156 155
     end
157  
-
158 156
   end
159 157
 end

0 notes on commit 2afc703

Jeremy Weiskotten

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).

Jeremy Weiskotten

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

Jeremy Weiskotten

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

Keenan Brock

couldn't get it to work - kill it

Please sign in to comment.
Something went wrong with that request. Please try again.