i18n_date_range 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eddd20b29da075a3aa8e3f4c274c14a942887039bd1b85f28b25cf85945d815f
4
+ data.tar.gz: 032275f6d7c6ed1dd90525d6545730502b7358a4085409167e640b8c55a94cb2
5
+ SHA512:
6
+ metadata.gz: f33b04aa867d4dd0152301e8d7beaaf04f2a6a7aca1610982a64e96e858fbac7d6296dea2defe17974132ab49268b6087be5e361c5162b78c88bd6ad418f862b
7
+ data.tar.gz: bcabbaf98c0f77319b99e3957758d60f269fb5dd6479b970c325b9da76aa5ea0e00aae8852a524c4eb6a9a31ead3c5149de4cc1d99c07d38836c0c798ed2fdc4
data/.DS_Store ADDED
Binary file
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2023-09-08
2
+
3
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in i18n_date_range.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 pabois
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # I18nDateRange
2
+
3
+ The purpose of this gem is to add a helper needed to display proper Date range informations.
4
+ For example: "Du 3 au 5 octobre 2024" or "Du 3 octobre 2023 au 12 janvier 2024"
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'i18n_date_range'
12
+ ```
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ ## Usage
18
+
19
+ This gem comes with an integrated view helper: `date_range_i18n` which takes 3 parameters:
20
+ - date_from
21
+ - date_to (optional)
22
+ - format (optional)
23
+
24
+ Format can be `:short` or `:long` (default is :short)
25
+
26
+ "Short" format means without the explicit day name (September 8, 2024).
27
+ "Long" format means with it (Friday, September 8, 2024).
28
+
29
+ ## Examples (in French)
30
+
31
+ // Example 1
32
+ <% date_from = Date.today %>
33
+ <%= date_range_i18n(date_from) %>
34
+
35
+ => 8 septembre 2023
36
+
37
+ // Example 1 bis
38
+ <% date_from = Date.today %>
39
+ <%= date_range_i18n(date_from, nil, :long) %>
40
+
41
+ => vendredi 8 septembre 2023
42
+
43
+ // Example 2
44
+ <%
45
+ date_from = Date.today
46
+ date_to = Date.today + 1.day
47
+ %>
48
+ <%= date_range_i18n(date_from, date_to) %>
49
+
50
+ => Du 8 au 9 septembre 2023
51
+
52
+ // Example 2 bis
53
+ <%
54
+ date_from = Date.today
55
+ date_to = Date.today + 1.day
56
+ %>
57
+ <%= date_range_i18n(date_from, date_to, :long) %>
58
+
59
+ => Du vendredi 8 au samedi 9 septembre 2023
60
+
61
+ // Example 3
62
+ <%
63
+ date_from = Date.today
64
+ date_to = Date.today + 1.month
65
+ %>
66
+ <%= date_range_i18n(date_from, date_to) %>
67
+
68
+ => Du 8 septembre au 8 octobre 2023
69
+
70
+ // Example 3 bis
71
+ <%
72
+ date_from = Date.today
73
+ date_to = Date.today + 1.month
74
+ %>
75
+ <%= date_range_i18n(date_from, date_to, :long) %>
76
+
77
+ => Du vendredi 8 septembre au dimanche 8 octobre 2023
78
+
79
+ // Example 4
80
+ <%
81
+ date_from = Date.today
82
+ date_to = Date.today + 1.year
83
+ %>
84
+ <%= date_range_i18n(date_from, date_to) %>
85
+
86
+ => Du 8 septembre 2023 au 8 septembre 2024
87
+
88
+ // Example 4 bis
89
+ <%
90
+ date_from = Date.today
91
+ date_to = Date.today + 1.year
92
+ %>
93
+ <%= date_range_i18n(date_from, date_to, :long) %>
94
+
95
+ => Du vendredi 8 septembre 2023 au dimanche 8 septembre 2024
96
+
97
+ ## Change texts or formats
98
+
99
+ This gem is based on I18n. Feel free to overwrite any key you want.
100
+ Refer to [config/locales/fr.yml](config/locales/fr.yml) to see what can be edited.
101
+
102
+ ## Contributing
103
+
104
+ Bug reports and pull requests are welcome on GitHub at https://github.com/noesya/i18n_date_range.
105
+
106
+ ## License
107
+
108
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
109
+
110
+ ## Bibliography
111
+
112
+ https://coderwall.com/p/fkg-ng/display-date-ranges-in-text-in-rails
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "i18n_date_range"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ en-GB:
2
+ date_range:
3
+ with_to: "From %{from} to %{to}"
4
+ without_to: "%{from}"
5
+
6
+ date:
7
+ formats:
8
+ short:
9
+ day: "%e"
10
+ day_month: "%e %B"
11
+ day_month_year: "%e %B %Y"
12
+ long:
13
+ day: "%A %e"
14
+ day_month: "%A, %e %B"
15
+ day_month_year: "%A, %e %B %Y"
@@ -0,0 +1,15 @@
1
+ en:
2
+ date_range:
3
+ with_to: "From %{from} to %{to}"
4
+ without_to: "%{from}"
5
+
6
+ date:
7
+ formats:
8
+ short:
9
+ day: "%e"
10
+ day_month: "%B %e"
11
+ day_month_year: "%B %e, %Y"
12
+ long:
13
+ day: "%A %e"
14
+ day_month: "%A, %B %e"
15
+ day_month_year: "%A, %B %e, %Y"
@@ -0,0 +1,15 @@
1
+ fr:
2
+ date_range:
3
+ with_to: "Du %{from} au %{to}"
4
+ without_to: "%{from}"
5
+
6
+ date:
7
+ formats:
8
+ short:
9
+ day: "%e"
10
+ day_month: "%e %B"
11
+ day_month_year: "%e %B %Y"
12
+ long:
13
+ day: "%A %e"
14
+ day_month: "%A %e %B"
15
+ day_month_year: "%A %e %B %Y"
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/i18n_date_range/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "i18n_date_range"
7
+ spec.version = I18nDateRange::VERSION
8
+ spec.authors = ["pabois"]
9
+ spec.email = ["pierreandre.boissinot@noesya.coop"]
10
+
11
+ spec.summary = "Add helper to display a date range"
12
+ spec.description = "Add a new helper to display properly a date range (ex: From 8 to 9 September 2023)"
13
+ spec.homepage = "https://github.com/noesya/i18n_date_range"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "https://github.com/noesya/i18n_date_range/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # For more information and examples about making a new gem, checkout our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
@@ -0,0 +1,56 @@
1
+ class I18nDateRangeGenerator
2
+ attr_reader :from_date, :to_date, :format
3
+
4
+ def self.generate(from_date, to_date, format)
5
+ new(from_date, to_date, format).to_s
6
+ end
7
+
8
+ def initialize(from_date, to_date, format)
9
+ @from_date = from_date
10
+ @to_date = to_date
11
+ @format = format
12
+ end
13
+
14
+ def to_s
15
+ to_date.blank? ? without_to : with_to
16
+ end
17
+
18
+ protected
19
+
20
+ def without_to
21
+ I18n.t(
22
+ 'date_range.without_to',
23
+ from: format_date(from_date, :day_month_year)
24
+ )
25
+ end
26
+
27
+ def with_to
28
+ I18n.t(
29
+ 'date_range.with_to',
30
+ from: format_date(from_date, from_format),
31
+ to: format_date(to_date, :day_month_year)
32
+ )
33
+ end
34
+
35
+ def from_format
36
+ return :day_month_year if different_year?
37
+ return :day_month if different_month?
38
+ :day
39
+ end
40
+
41
+ def format_date(date, key)
42
+ I18n.l(
43
+ date,
44
+ format: "#{format}.#{key}".to_sym
45
+ )
46
+ end
47
+
48
+ def different_year?
49
+ from_date.year != to_date.year
50
+ end
51
+
52
+ def different_month?
53
+ from_date.month != to_date.month
54
+ end
55
+
56
+ end
@@ -0,0 +1,7 @@
1
+ module I18nDateRangeHelper
2
+
3
+ def date_range_i18n(from, to = nil, format = :short)
4
+ I18nDateRangeGenerator.generate(from, to, format)
5
+ end
6
+
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I18nDateRange
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "i18n_date_range/version"
4
+ require_relative "i18n_date_range/i18n_date_range_generator"
5
+ require_relative "i18n_date_range/i18n_date_range_helper"
6
+
7
+ module I18nDateRange
8
+ class Error < StandardError; end
9
+
10
+ class Engine < ::Rails::Engine
11
+ isolate_namespace I18nDateRange
12
+
13
+ ActiveSupport.on_load( :action_view ){ include I18nDateRangeHelper }
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n_date_range
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - pabois
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Add a new helper to display properly a date range (ex: From 8 to 9 September
14
+ 2023)'
15
+ email:
16
+ - pierreandre.boissinot@noesya.coop
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".DS_Store"
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - CHANGELOG.md
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - config/locales/en-GB.yml
32
+ - config/locales/en.yml
33
+ - config/locales/fr.yml
34
+ - i18n_date_range.gemspec
35
+ - lib/i18n_date_range.rb
36
+ - lib/i18n_date_range/i18n_date_range_generator.rb
37
+ - lib/i18n_date_range/i18n_date_range_helper.rb
38
+ - lib/i18n_date_range/version.rb
39
+ homepage: https://github.com/noesya/i18n_date_range
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/noesya/i18n_date_range
44
+ source_code_uri: https://github.com/noesya/i18n_date_range
45
+ changelog_uri: https://github.com/noesya/i18n_date_range/CHANGELOG.md
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.6.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.1.4
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Add helper to display a date range
65
+ test_files: []