stamp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .bundle
3
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@stamp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ stamp (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ builder (3.0.0)
10
+ cucumber (1.0.0)
11
+ builder (>= 2.1.2)
12
+ diff-lcs (>= 1.1.2)
13
+ gherkin (~> 2.4.1)
14
+ json (>= 1.4.6)
15
+ term-ansicolor (>= 1.0.5)
16
+ diff-lcs (1.1.2)
17
+ gherkin (2.4.1)
18
+ json (>= 1.4.6)
19
+ json (1.5.3)
20
+ term-ansicolor (1.0.5)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ cucumber
27
+ stamp!
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # stamp
2
+
3
+ Format dates and times based on examples, not arcane strftime directives.
4
+
5
+ [![Build Status](http://travis-ci.org/jeremyw/stamp.png)](http://travis-ci.org/jeremyw/stamp)
6
+
7
+ ## Installation
8
+
9
+ Just `gem install stamp`, or add stamp to your Gemfile and `bundle install`.
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ date = Date.new(2011, 6, 9)
15
+ date.stamp("January 1, 1999") # June 6, 2011
16
+ date.stamp("Jan 09, 1999") # Jun 06, 2011
17
+ date.stamp("Jan 1") # Jun 6
18
+ ```
19
+
20
+ More coming soon, including time formats by example.
21
+
22
+ ## Contributing to stamp
23
+
24
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
25
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
26
+ * Fork the project
27
+ * Start a feature/bugfix branch
28
+ * Commit and push until you are happy with your contribution
29
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
30
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
31
+
32
+ ## Copyright
33
+
34
+ Copyright (c) 2011 Jeremy Weiskotten (@doctorzaius). See LICENSE.txt for
35
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'cucumber/rake/task'
3
+
4
+ Cucumber::Rake::Task.new(:features)
5
+
6
+ task :default => :features
@@ -0,0 +1,20 @@
1
+ Feature: Stamping a date
2
+ In order to format dates in a more programmer-friendly way
3
+ the stamp method
4
+ formats a date given a human-readable example.
5
+
6
+ Scenario Outline: Various examples
7
+ Given the date December 9, 2011
8
+ When I stamp the example "<example>"
9
+ Then I produce "<output>"
10
+ And I like turtles
11
+
12
+ Examples:
13
+ | example | output |
14
+ | pass-through | pass-through |
15
+ | January | December |
16
+ | Jan | Dec |
17
+ | Jan 1 | Dec 9 |
18
+ | Jan 01 | Dec 09 |
19
+ | Jan 10 | Dec 09 |
20
+ | Jan 1, 1999 | Dec 9, 2011 |
@@ -0,0 +1,13 @@
1
+ Given /^the date (\w+) (\d+), (\d{4})$/ do |month_name, day, year|
2
+ month = Date::MONTHNAMES.index(month_name) || Date::ABBR_MONTHNAMES.index(month_name)
3
+ assert (1..12).include?(month), "Invalid month: #{month_name}"
4
+ @date = Date.new(year.to_i, month, day.to_i)
5
+ end
6
+
7
+ When /^I stamp the example "([^"]*)"$/ do |example|
8
+ @formatted_date = @date.stamp(example)
9
+ end
10
+
11
+ Then /^I produce "([^"]*)"$/ do |expected|
12
+ assert_equal expected, @formatted_date
13
+ end
@@ -0,0 +1,5 @@
1
+ # http://www.youtube.com/watch?v=CMNry4PE93Y
2
+ Then /^I like turtles$/ do
3
+ turtles_are_awesome = true
4
+ assert turtles_are_awesome
5
+ end
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ begin
3
+ Bundler.setup(:default, :development)
4
+ rescue Bundler::BundlerError => e
5
+ $stderr.puts e.message
6
+ $stderr.puts "Run `bundle install` to install missing gems"
7
+ exit e.status_code
8
+ end
9
+
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
11
+ require 'stamp'
12
+
13
+ require 'test/unit/assertions'
14
+ World(Test::Unit::Assertions)
data/lib/stamp.rb ADDED
@@ -0,0 +1,56 @@
1
+ require "stamp/version"
2
+ require "date"
3
+
4
+ module Stamp
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ # extend ClassMethods
8
+ include InstanceMethods
9
+ end
10
+ end
11
+
12
+ # module ClassMethods
13
+ # end
14
+
15
+ module InstanceMethods
16
+ MONTHNAMES_REGEXP = /(#{Date::MONTHNAMES.compact.join('|')})/i
17
+ ABBR_MONTHNAMES_REGEXP = /(#{Date::ABBR_MONTHNAMES.compact.join('|')})/i
18
+
19
+ def stamp(example)
20
+ strftime(strftime_directives(example))
21
+ end
22
+
23
+ def strftime_directives(example)
24
+ directives = []
25
+ terms = example.split(/\b/)
26
+
27
+ terms.each_with_index do |term, index|
28
+ previous_term = (index == 0) ? nil : terms[index - 1]
29
+ next_term = (index == terms.size-1) ? nil : terms[index + 1]
30
+
31
+ directives << (strftime_directive(term, previous_term, next_term) || term)
32
+ end
33
+
34
+ directives.join
35
+ end
36
+ private :strftime_directives
37
+
38
+ def strftime_directive(term, previous_term=nil, next_term=nil)
39
+ case term
40
+ when MONTHNAMES_REGEXP
41
+ '%B'
42
+ when ABBR_MONTHNAMES_REGEXP
43
+ '%b'
44
+ when /\d{4}/
45
+ '%Y'
46
+ when /\d{2}/
47
+ '%d'
48
+ when /\d{1}/
49
+ '%e'
50
+ end
51
+ end
52
+ private :strftime_directive
53
+ end
54
+ end
55
+
56
+ Date.send(:include, ::Stamp)
@@ -0,0 +1,3 @@
1
+ module Stamp
2
+ VERSION = "0.0.1"
3
+ end
data/stamp.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "stamp/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "stamp"
7
+ s.version = Stamp::VERSION
8
+ s.authors = ["Jeremy Weiskotten"]
9
+ s.email = ["jeremy@weiskotten.com"]
10
+ s.homepage = "https://github.com/jeremyw/stamp"
11
+ s.summary = %Q{Date and time formatting for humans.}
12
+ s.description = %Q{Format dates and times based on examples, not arcane strftime directives.}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "cucumber"
20
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stamp
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Jeremy Weiskotten
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-06-29 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: cucumber
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: Format dates and times based on examples, not arcane strftime directives.
34
+ email:
35
+ - jeremy@weiskotten.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - .gitignore
44
+ - .rvmrc
45
+ - Gemfile
46
+ - Gemfile.lock
47
+ - README.md
48
+ - Rakefile
49
+ - features/stamp.feature
50
+ - features/step_definitions/stamp_steps.rb
51
+ - features/step_definitions/turtle_steps.rb
52
+ - features/support/env.rb
53
+ - lib/stamp.rb
54
+ - lib/stamp/version.rb
55
+ - stamp.gemspec
56
+ has_rdoc: true
57
+ homepage: https://github.com/jeremyw/stamp
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Date and time formatting for humans.
88
+ test_files:
89
+ - features/stamp.feature
90
+ - features/step_definitions/stamp_steps.rb
91
+ - features/step_definitions/turtle_steps.rb
92
+ - features/support/env.rb