Skip to content

Commit

Permalink
Merge branch 'release-1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Cory Lown committed May 28, 2020
2 parents e2eaf11 + ee091d3 commit fdd37b8
Show file tree
Hide file tree
Showing 55 changed files with 243 additions and 846 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,32 @@ require 'edtf-humanize'
=> "unknown"
```

## I18n

EDTF-humanize uses I18n to translate several date formats in the language set by I18n.locale.
At the moment instances of EDTF::Century and EDTF::Decade are not translated.

Example of a Rails application with current locale `:it`:

```
> d = Date.edtf('1973-08')
=> Wed, 01 Aug 1973
> d.humanize
=> "agosto 1973"
```

To translate instances of EDTF::Season you need to add these keys to your Rails I18n locale file:

```yaml
it:
date:
seasons:
spring: "primavera"
summer: "estate"
autumn: "autunno"
winter: "inverno"
```
## Configuration
You can configure some aspects of how dates are humanized. In a Rails application you could place the following block in an initializer to modify the default configurations. The current set of options were driven by local use cases. Please feel free to submit an issue if you have a use case not covered by the current options.
Expand Down
19 changes: 3 additions & 16 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ rescue LoadError
end

require 'rdoc/task'
require 'rspec/core/rake_task'

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
Expand All @@ -14,21 +15,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end






Bundler::GemHelper.install_tasks
RSpec::Core::RakeTask.new(:spec)

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end


task default: :test
task default: :spec
10 changes: 6 additions & 4 deletions edtf-humanize.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ Gem::Specification.new do |s|
s.description = "This gem adds a humanize method to EDTF::Decade, EDTF::Interval, EDTF::Set, EDTF::Season, EDTF::Unknown, and Date (ISO 8601 compliant) objects."
s.license = "BSD-3-Clause"

s.files = Dir["{app,config,db,lib}/**/*", "LICENSE.txt", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
s.files = Dir["{app,config,db,lib}/**/*", "LICENSE.txt", "README.rdoc"]
s.test_files = Dir["spec/**/*"]

s.add_dependency "edtf", ">= 2.3", "< 4"
s.add_dependency "activesupport", ">= 4", "< 6"
s.add_dependency "activesupport", ">= 4"

s.add_development_dependency "rails", ">= 4", "< 6"
s.add_development_dependency "rake", "~> 13.0"
s.add_development_dependency "rspec", "~> 3.9"
s.add_development_dependency "bump"
end
6 changes: 5 additions & 1 deletion lib/edtf/humanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class Configuration
:century_suffix,
:unspecified_digit_substitute,
:interval_connector,
:interval_unspecified_suffix,
:set_dates_connector,
:set_last_date_connector,
:set_two_dates_connector,
:interval_unspecified_suffix,
:set_earlier_prefix,
:set_later_prefix,
:unknown

def initialize
Expand All @@ -70,6 +72,8 @@ def initialize
@set_dates_connector = ", "
@set_last_date_connector = " or "
@set_two_dates_connector = " or "
@set_earlier_prefix = "on or before "
@set_later_prefix = "on or after "

@unknown = "unknown"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/edtf/humanize/formats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def date_precision date

# October 5, 1995
def day_precision_format date
date.strftime(Edtf::Humanize.configuration.day_precision_strftime_format)
I18n.localize(date, format: Edtf::Humanize.configuration.day_precision_strftime_format)
end

# October 1995
def month_precision_format date
date.strftime(Edtf::Humanize.configuration.month_precision_strftime_format)
I18n.localize(date, format: Edtf::Humanize.configuration.month_precision_strftime_format)
end

# 1995
Expand Down
14 changes: 13 additions & 1 deletion lib/edtf/humanize/season.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ module Season
include Edtf::Humanize::Formats

def humanize
"#{apply_if_approximate(self)}#{self.season} #{self.year}#{apply_if_uncertain(self)}"
"#{apply_if_approximate(self)}"\
"#{translate_season(self.season)} #{self.year}"\
"#{apply_if_uncertain(self)}"
end

private

def translate_season(season)
begin
I18n.translate!("date.seasons.#{self.season}")
rescue I18n::MissingTranslationData
self.season
end
end

end
Expand Down
21 changes: 20 additions & 1 deletion lib/edtf/humanize/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,26 @@ def humanize
private

def format_set_entries(dates)
dates.entries.map { |date| "#{apply_if_approximate(date)}#{simple_date_format(date)}" }
dates.entries.map.with_index { |date, index|
"#{apply_if_earlier(dates, index)}"\
"#{apply_if_later(dates, index)}"\
"#{apply_if_approximate(date)}"\
"#{simple_date_format(date)}"
}
end

# '[..1760-12-03]' => on or before December 3, 1760
def apply_if_earlier(dates, index)
if dates.earlier? && index == 0
Edtf::Humanize.configuration.set_earlier_prefix
end
end

# '[1760-12..]' => on or after December 1760
def apply_if_later(dates, index)
if dates.later? && (index + 1) == dates.size
Edtf::Humanize.configuration.set_later_prefix
end
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/edtf/humanize/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Edtf
module Humanize
VERSION = "0.0.7"
VERSION = "1.0.0"
end
end
4 changes: 0 additions & 4 deletions lib/tasks/edtf_humanize_tasks.rake

This file was deleted.

65 changes: 65 additions & 0 deletions spec/edtf_humanize_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'edtf-humanize'

RSpec.describe Edtf::Humanize do

it { is_expected.to be_a(Module) }

context 'with a decade' do
it 'should return a humanized decade string' do
expect(Date.edtf('199x').humanize).to eq('1990s')
end
end

context 'with a centrury' do
it 'should return a humanized century string' do
expect(Date.edtf('19xx').humanize).to eq('1900s')
end
end

context 'with an interval' do
it 'should return a humanized interval string' do
expect(Date.edtf('1970/1980').humanize).to eq('1970 to 1980')
end
end

context 'with an appoximate interval'
it 'should return a humanized approximate interval string' do
expect(Date.edtf('1970~/1980~').humanize).to eq('circa 1970 to circa 1980')
end

context 'with an iso date' do
it 'should return a humanized ISO date string' do
expect(Date.edtf('1975-07-01').humanize).to eq('July 1, 1975')
end
end

context 'with an uncertain iso date' do
it 'should return a humanized uncertain ISO date string' do
expect(Date.edtf('1975?').humanize).to eq('1975?')
end
end

context 'with an unspecfic year iso date' do
it 'should return a humanized unspecified year ISO date string' do
expect(Date.edtf('197u').humanize).to eq('197x')
end
end

context 'with a season' do
it 'should return a humanized season string' do
expect(Date.edtf('1975-22').humanize).to eq('summer 1975')
end
end

context 'with a set' do
it 'should return a humanized set string' do
expect(Date.edtf('[1980, 1981, 1983]').humanize).to eq('1980, 1981 or 1983')
end
end

context 'with an unknown value' do
it 'should return a humanized unknown string' do
expect(Date.edtf('uuuu').humanize).to eq('unknown')
end
end
end
101 changes: 101 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
#
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end
28 changes: 0 additions & 28 deletions test/dummy/README.rdoc

This file was deleted.

6 changes: 0 additions & 6 deletions test/dummy/Rakefile

This file was deleted.

Empty file removed test/dummy/app/assets/images/.keep
Empty file.
Loading

0 comments on commit fdd37b8

Please sign in to comment.