coolbelt 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cadd1f6a55c3554de18373e445c8ffc17194dce173d0129a5c90814f12c637ca
4
+ data.tar.gz: 59918d59014c42e1c4e472696a8cf7485aaf6573bbc512803e234d4efb793fd1
5
+ SHA512:
6
+ metadata.gz: f70e1083e1651331956ee6b68d04b4bef69550dce7ba81126b82ae4aebb1483b6217c105b13325a1375cceef590e81f861fcf5eb2f005660def8eb133a3667a5
7
+ data.tar.gz: 5e67a4fd60a36563e8d070ff65a6049417b0d902b6662f674c0799858c34f9f45aa949aa6527fe6cbd8fcc53967459be11ec93291c79db7658b9ae0d0ad98b97
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2026-11-04
4
+
5
+ - Initial release
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "coolbelt" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at ["antulik@gmail.com"](mailto:"antulik@gmail.com").
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Anton Katunin
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,43 @@
1
+ # Coolbelt
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/coolbelt`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/coolbelt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/coolbelt/blob/main/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Coolbelt project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/coolbelt/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,46 @@
1
+ module Coolbelt::AttributesFrom
2
+ extend ActiveSupport::Concern
3
+
4
+ class_methods do
5
+ def attributes_from(name, &block)
6
+ attributes_from_x = "attributes_from_#{name}".to_sym
7
+ attributes_from_x_proc = "#{attributes_from_x}_proc".to_sym
8
+ set_attributes_from = "set_#{attributes_from_x}"
9
+
10
+ define_method("#{name}=") do |value|
11
+ super(value)
12
+ send(set_attributes_from)
13
+ end
14
+
15
+ # attributes_from_X_proc
16
+ class_attribute(attributes_from_x_proc)
17
+ send("#{attributes_from_x_proc}=", block)
18
+
19
+ # attributes_from_X=
20
+ define_method("#{attributes_from_x}=") do |data|
21
+ assign_attributes(send(attributes_from_x_proc).call(data))
22
+ end
23
+
24
+ # set_attributes_from_X
25
+ define_method(set_attributes_from) do
26
+ public_send("#{attributes_from_x}=", public_send(name))
27
+ end
28
+
29
+ # attributes_from_X!
30
+ define_method("#{attributes_from_x}!") do
31
+ send(set_attributes_from)
32
+ save!
33
+ end
34
+
35
+ # self.attributes_from_X
36
+ define_singleton_method(attributes_from_x) do |data|
37
+ send(attributes_from_x_proc).call(data)
38
+ end
39
+
40
+ # self.update_all_attributes_from_X
41
+ define_singleton_method("update_all_#{attributes_from_x}") do
42
+ find_each(&"#{attributes_from_x}!".to_sym)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,72 @@
1
+ module Coolbelt::HasTimeRange
2
+ extend ActiveSupport::Concern
3
+
4
+ module TimeParser
5
+ def self.parse_time_text(value)
6
+ return if value.blank?
7
+
8
+ parsed_time = Time.strptime(value.to_s, "%H:%M") # parse in any time zone
9
+ h = parsed_time.hour
10
+ m = parsed_time.min
11
+ s = m == 59 ? 59 : 0
12
+ h.hours + m.minutes + s.seconds
13
+ rescue ArgumentError
14
+ nil
15
+ end
16
+
17
+ def self.format_time_text(seconds)
18
+ Time.at(seconds).utc.strftime("%H:%M") if seconds
19
+ end
20
+ end
21
+
22
+ class_methods do
23
+ def has_time_range
24
+ class_eval do
25
+ attribute(:start_seconds, :integer, default: 0)
26
+ attribute(:end_seconds, :integer, default: 86_399)
27
+
28
+ validates(:start_seconds, :end_seconds, presence: true,
29
+ numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than: 86_400 },)
30
+ validates(:end_seconds, comparison: { greater_than: :start_seconds })
31
+
32
+ after_validation do
33
+ errors.add(:start_time_text, :invalid) if errors[:start_seconds].present?
34
+ errors.add(:end_time_text, :invalid) if errors[:end_seconds].present?
35
+ end
36
+
37
+ def start_time_text
38
+ TimeParser.format_time_text(start_seconds)
39
+ end
40
+
41
+ def end_time_text
42
+ TimeParser.format_time_text(end_seconds)
43
+ end
44
+
45
+ def start_time_utc = Time.at(start_seconds).utc
46
+ def end_time_utc = Time.at(end_seconds).utc
47
+
48
+ def seconds_includes?(second)
49
+ start_seconds <= second && second <= end_seconds
50
+ end
51
+
52
+ def start_time(datetime)
53
+ time = start_time_utc
54
+ datetime.change({ hour: time.hour, min: time.min, sec: time.sec })
55
+ end
56
+
57
+ def end_time(datetime)
58
+ time = end_time_utc
59
+ datetime.change({ hour: time.hour, min: time.min, sec: time.sec })
60
+ end
61
+
62
+ def start_time_text=(value)
63
+ self.start_seconds = TimeParser.parse_time_text(value)
64
+ end
65
+
66
+ def end_time_text=(value)
67
+ self.end_seconds = TimeParser.parse_time_text(value)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Coolbelt
4
+ VERSION = "0.1.0"
5
+ end
data/lib/coolbelt.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "coolbelt/version"
4
+
5
+ module Coolbelt
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
data/sig/coolbelt.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Coolbelt
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coolbelt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Katunin
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A personal collection of useful extensions for Ruby and Rails
13
+ email:
14
+ - antulik@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CHANGELOG.md
20
+ - CODE_OF_CONDUCT.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - Rakefile
24
+ - lib/coolbelt.rb
25
+ - lib/coolbelt/attributes_from.rb
26
+ - lib/coolbelt/has_time_range.rb
27
+ - lib/coolbelt/version.rb
28
+ - sig/coolbelt.rbs
29
+ homepage: https://github.com/antulik/coolbelt
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/antulik/coolbelt
34
+ source_code_uri: https://github.com/antulik/coolbelt
35
+ changelog_uri: https://github.com/antulik/coolbelt/blob/main/CHANGELOG.md
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.2.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 4.0.20
51
+ specification_version: 4
52
+ summary: A personal collection of useful extensions for Ruby and Rails
53
+ test_files: []