interpreted_date 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,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rvmrc ADDED
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
+ environment_id="ruby-1.9.3@interpreted-date"
8
+
9
+ #
10
+ # Uncomment following line if you want options to be set only for given project.
11
+ #
12
+ # PROJECT_JRUBY_OPTS=( --1.9 )
13
+
14
+ #
15
+ # First we attempt to load the desired environment directly from the environment
16
+ # file. This is very fast and efficient compared to running through the entire
17
+ # CLI and selector. If you want feedback on which environment was used then
18
+ # insert the word 'use' after --create as this triggers verbose mode.
19
+ #
20
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
21
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
22
+ then
23
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
24
+
25
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
26
+ then
27
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
28
+ fi
29
+ else
30
+ # If the environment file has not yet been created, use the RVM CLI to select.
31
+ if ! rvm --create use "$environment_id"
32
+ then
33
+ echo "Failed to create RVM environment '${environment_id}'."
34
+ exit 1
35
+ fi
36
+ fi
37
+
38
+ #
39
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
40
+ # it be automatically loaded. Uncomment the following and adjust the filename if
41
+ # necessary.
42
+ #
43
+ # filename=".gems"
44
+ # if [[ -s "$filename" ]]
45
+ # then
46
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
47
+ # fi
48
+
49
+ # If you use bundler, this might be useful to you:
50
+ # if command -v bundle && [[ -s Gemfile ]]
51
+ # then
52
+ # bundle install
53
+ # fi
54
+
55
+
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in interpreted_date.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brett Huneycutt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # InterpretedDate
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'interpreted_date'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install interpreted_date
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'interpreted_date/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "interpreted_date"
8
+ gem.version = InterpretedDate::VERSION
9
+ gem.authors = ["Brett Huneycutt"]
10
+ gem.email = ["brett@1000memories.com"]
11
+ gem.description = %q{Human readable dates from incomplete dates}
12
+ gem.summary = %q{Provide one, some or all of decade, year, month and day and get back a human readable date.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,3 @@
1
+ class InterpretedDate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,93 @@
1
+ require "interpreted_date/version"
2
+
3
+ class InterpretedDate
4
+ attr_accessor :decade, :year, :month, :day
5
+
6
+ # Allows date strings to be generated with Ruby even for dates w/o years
7
+ DEFAULT_YEAR = -1
8
+
9
+ def initialize(attributes = {})
10
+ self.decade = attributes[:decade]
11
+ self.year = attributes[:year]
12
+ self.month = attributes[:month]
13
+ self.day = attributes[:day]
14
+ end
15
+
16
+ def self.from_date(date)
17
+ new(year: date.year, month: date.month, day: date.day)
18
+ end
19
+
20
+ def interpreted_year
21
+ @interpreted_year ||= year || (decade && (decade + 5)) || DEFAULT_YEAR
22
+ end
23
+
24
+ def date_attributes
25
+ return @date_attributes if @date_attributes
26
+
27
+ @date_attributes = [interpreted_year, month]
28
+ @date_attributes << day if month
29
+ @date_attributes.compact!
30
+ @date_attributes
31
+ end
32
+
33
+ def date
34
+ @date ||= Date.new(*date_attributes) if !date_attributes.empty?
35
+ rescue Exception => e
36
+ nil
37
+ end
38
+
39
+ def to_i
40
+ date.to_time.to_i if date && (decade || year)
41
+ end
42
+
43
+ def to_s(options = {})
44
+ preposition = options.fetch(:preposition) { false }
45
+ if day && month && year && date
46
+ date.strftime("%B %-d, %Y")
47
+ elsif month && year && date
48
+ date.strftime("%B %Y")
49
+ elsif year && date
50
+ output = preposition ? "in " : ""
51
+ output << date.strftime("%Y")
52
+ output
53
+ elsif decade
54
+ output = preposition ? "in the " : ""
55
+ output << decade.to_s
56
+ output << "s"
57
+ output
58
+ elsif day && month && date
59
+ date.strftime("%B %-d")
60
+ end
61
+ end
62
+
63
+ def as_json(options = {})
64
+ {
65
+ 'string' => self.to_s,
66
+ 'integer' => self.to_i,
67
+ 'decade' => decade,
68
+ 'year' => year,
69
+ 'month' => month,
70
+ 'day' => day,
71
+ }
72
+ end
73
+
74
+ def self.interpret_date(date_string)
75
+ date, specificity = DateTools.date_and_specificity(date_string)
76
+
77
+ parts = {}
78
+
79
+ return unless specificity
80
+
81
+ parts[:day] = date.day if specificity <= 1
82
+ parts[:month] = date.month if specificity <= 2
83
+ parts[:year] = date.year if specificity <= 3
84
+ parts[:decade] = date.year - (date.year % 10) if specificity <= 5
85
+
86
+ new(parts)
87
+ end
88
+
89
+ def present?
90
+ [decade, year, month, day].any?(&:present?)
91
+ end
92
+
93
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ describe InterpretedDate do
4
+
5
+ describe "with no date attributes" do
6
+ subject { InterpretedDate.new }
7
+
8
+ it "returns nil string" do
9
+ subject.to_s.should be_nil
10
+ end
11
+
12
+ it "returns nil integer" do
13
+ subject.to_i.should be_nil
14
+ end
15
+ end
16
+
17
+ describe "with decade only" do
18
+ subject { InterpretedDate.new({:decade => 1950}) }
19
+
20
+ it "returns the right string" do
21
+ subject.to_s.should eq("1950s")
22
+ end
23
+
24
+ it "optionally returns a string with a preposition" do
25
+ subject.to_s(preposition: true).should eq("in the 1950s")
26
+ end
27
+
28
+ it "returns the right integer" do
29
+ subject.to_i.should eq(Date.new(1955).to_time.to_i)
30
+ end
31
+ end
32
+
33
+ describe "with decade and year" do
34
+ subject { InterpretedDate.new({:decade => 1950, :year => 1954}) }
35
+
36
+ it "returns the right string" do
37
+ subject.to_s.should eq("1954")
38
+ end
39
+
40
+ it "optionally returns a string with a preposition" do
41
+ subject.to_s(preposition: true).should eq("in 1954")
42
+ end
43
+
44
+ it "returns the right integer" do
45
+ subject.to_i.should eq(Date.new(1954).to_time.to_i)
46
+ end
47
+ end
48
+
49
+ describe "with decade, year and month" do
50
+ subject { InterpretedDate.new({:decade => 1950, :year => 1955, :month => 3}) }
51
+
52
+ it "returns the right string" do
53
+ subject.to_s.should eq("March 1955")
54
+ end
55
+
56
+ it "returns the right integer" do
57
+ subject.to_i.should eq(Date.new(1955, 3).to_time.to_i)
58
+ end
59
+ end
60
+
61
+ describe "with decade, year, month and day" do
62
+ subject { InterpretedDate.new({:decade => 1950, :year => 1955, :month => 3, :day => 1}) }
63
+
64
+ it "returns the right string" do
65
+ subject.to_s.should eq("March 1, 1955")
66
+ end
67
+
68
+ it "returns the right integer" do
69
+ subject.to_i.should eq(Date.new(1955, 3, 1).to_time.to_i)
70
+ end
71
+ end
72
+
73
+ describe "with month and day" do
74
+ subject { InterpretedDate.new({:month => 10, :day => 31}) }
75
+
76
+ it "returns the right string" do
77
+ subject.to_s.should eq("October 31")
78
+ end
79
+
80
+ it "returns nil integer" do
81
+ subject.to_i.should be_nil
82
+ end
83
+ end
84
+
85
+ describe "with invalid date" do
86
+ subject { InterpretedDate.new({:year => 2012, :month => 2, :day => 31}) }
87
+
88
+ it "returns nil string" do
89
+ subject.to_s.should be_nil
90
+ end
91
+
92
+ it "returns nil integer" do
93
+ subject.to_i.should be_nil
94
+ end
95
+ end
96
+
97
+ end
@@ -0,0 +1,20 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require './lib/interpreted_date'
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+
15
+ # Run specs in random order to surface order dependencies. If you find an
16
+ # order dependency and want to debug it, you can fix the order by providing
17
+ # the seed, which is printed after each run.
18
+ # --seed 1234
19
+ config.order = 'random'
20
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: interpreted_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brett Huneycutt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Human readable dates from incomplete dates
15
+ email:
16
+ - brett@1000memories.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - .rvmrc
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - interpreted_date.gemspec
29
+ - lib/interpreted_date.rb
30
+ - lib/interpreted_date/version.rb
31
+ - spec/interpreted_date_spec.rb
32
+ - spec/spec_helper.rb
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Provide one, some or all of decade, year, month and day and get back a human
57
+ readable date.
58
+ test_files:
59
+ - spec/interpreted_date_spec.rb
60
+ - spec/spec_helper.rb