attr_chainable 1.0.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: 354c7786fc972e705ae212ebbf431f5d5473a3f4dad70bd74a4797442d2b822d
4
+ data.tar.gz: d01bbcbed9988acfd06f34dc47be7edeead63bf847194d769ecf3d2348ea5085
5
+ SHA512:
6
+ metadata.gz: 4980d98a4230d9dedd436772b9f06827a40e281dd79fe6d184b5b91698f570f9feb84d9515a625fa9c5a722eb7a251dda4d2c70de1cc21d88f8c30d7d3aec19f
7
+ data.tar.gz: 53cfeb9cddeb1cf0bad658f6fce5decf595ef25a48ae2ebea37bc5c432044f56b4e80cb8a78e9d3a01f9a94137bf2b2f9cb24470274b44c9508750d341a6deb7
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.0] - 2025-04-13
9
+
10
+ ### Added
11
+
12
+ - Initial release of `attr_chainable`.
13
+ - Provides `attr_chainable` class method to define chainable attribute setters.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # attr_chainable
2
+
3
+ `attr_chainable` provides a simple way to define attributes on a Ruby class that support method chaining for setting values. This allows for a fluent interface when configuring objects.
4
+
5
+ When adding this gem, it will attach `attr_chainable` methods to your class, enabling you to set multiple attributes in a single line of code.
6
+
7
+ A constant `Nothing` is also defined in the global scope, which can be used as a placeholder for optional attributes.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'attr_chainable'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```bash
20
+ $ bundle install
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```bash
26
+ $ gem install attr_chainable
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ Use the `attr_chainable` class method to define chainable attributes:
32
+
33
+ ```ruby
34
+ class MyClass
35
+ # Define chainable attributes :name and :age
36
+ attr_chainable :name, :age
37
+ end
38
+
39
+ # Create an instance
40
+ instance = MyClass.new
41
+
42
+ # Set attributes using chaining
43
+ instance.name("Alice").age(30)
44
+
45
+ # Get attribute values
46
+ puts instance.name # Output: Alice
47
+ puts instance.age # Output: 30
48
+
49
+ # Setting an attribute returns the object itself, allowing further chaining
50
+ another_instance = MyClass.new
51
+ result = another_instance.name("Bob")
52
+ puts result.object_id == another_instance.object_id # Output: true
53
+ result.age(25)
54
+ puts another_instance.age # Output: 25
55
+ ```
56
+
57
+ When the generated methods are called with an argument, they set the corresponding instance variable and return `self`. When called without an argument, they return the current value of the instance variable.
58
+
59
+ ## Development
60
+
61
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
62
+
63
+ 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).
64
+
65
+ ## Contributing
66
+
67
+ Bug reports and pull requests are welcome on GitLab at [https://gitlab.com/your_username/attr_chainable](https://gitlab.com/your_username/attr_chainable).
68
+
69
+ ## License
70
+
71
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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]
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ Nothing = Object.new
4
+ class << Nothing
5
+ def to_s = "Nothing"
6
+ def inspect = "Nothing"
7
+ end
8
+ Nothing.freeze
9
+
10
+ Object.define_singleton_method(
11
+ :attr_chainable
12
+ ) do |*attributes|
13
+ attributes.each do |attribute|
14
+ instance_variable = "@#{attribute}".to_sym
15
+
16
+ define_method(attribute) do |value = Nothing|
17
+ return instance_variable_get(instance_variable) if value == Nothing
18
+
19
+ instance_variable_set(instance_variable, value)
20
+
21
+ self
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ module AttrChainable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attr_chainable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yacine Petitprez
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-04-13 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: "`attr_chainable` provides a simple way to define attributes on a Ruby
13
+ class that support method chaining for setting values. This allows for a fluent
14
+ interface when configuring objects."
15
+ email:
16
+ - anykeyh@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - README.md
25
+ - Rakefile
26
+ - lib/attr_chainable.rb
27
+ - sig/attr_chainable.rbs
28
+ homepage: https://gitlab.com/anykeyh/attr_chainable
29
+ licenses: []
30
+ metadata:
31
+ allowed_push_host: https://rubygems.org
32
+ homepage_uri: https://gitlab.com/anykeyh/attr_chainable
33
+ source_code_uri: https://gitlab.com/anykeyh/attr_chainable
34
+ changelog_uri: https://gitlab.com/anykeyh/attr_chainable/-/blob/main/CHANGELOG.md
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.1.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.6.3
50
+ specification_version: 4
51
+ summary: Define chainable attribute setters for Ruby classes.
52
+ test_files: []