predicateable 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: 941c76b3af813ee5f06646db352801907dd83a4aa5642a73ef7acfe103abac55
4
+ data.tar.gz: c5294a45ff7b2e4ac9ab0067cad3849addcf2dfe72c3939042fc55888594fa15
5
+ SHA512:
6
+ metadata.gz: 5594292717451ef97c6e4fa9492240d93ce53a99738b224ed3b077b10ec0e4dba86b6b58ed715d7bf96bb686c1da0bba292d6d9bdccfb729808be7a20c812c8a
7
+ data.tar.gz: 16917996f9ba3a785bbe10fd96b02c0f0c26f8a42cfed7998567110b052f5375fb3dcb309e13cc3a867958f73166a999bbedd43a59b29bc5ffe615161f1923eb
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Naoki Nishiguchi
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
+ # Predicateable
2
+
3
+ **Predicateable** is a Ruby mixin that dynamically defines predicate methods (like `admin?`, `young?`) based on the return value of a method. It's ideal for cleanly querying symbolic states without writing repetitive predicate methods.
4
+
5
+ ## Features
6
+
7
+ - Defines `?` predicate methods dynamically.
8
+ - Supports prefixing (e.g. `account_type_admin?`).
9
+ - `strict:` option ensures only symbols are matched.
10
+ - Works seamlessly with `respond_to?`.
11
+ - Pure Ruby, no dependencies.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your Gemfile:
16
+
17
+ ```ruby
18
+ gem "predicateable"
19
+ ````
20
+
21
+ And run:
22
+
23
+ ```sh
24
+ bundle install
25
+ ```
26
+
27
+ Or install it directly:
28
+
29
+ ```sh
30
+ gem install predicateable
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ ### Basic Example
36
+
37
+ ```ruby
38
+ class User
39
+ include Predicateable
40
+
41
+ attr_reader :age, :account_type
42
+
43
+ predicate :age_group, [:young, :middle, :old]
44
+ predicate :account_type, [:guest, :member, :admin], prefix: true
45
+
46
+ def initialize(age:, account_type:)
47
+ @age = age
48
+ @account_type = account_type
49
+ end
50
+
51
+ def age_group
52
+ case age
53
+ when 0...30 then :young
54
+ when 30...60 then :middle
55
+ else :old
56
+ end
57
+ end
58
+ end
59
+
60
+ user = User.new(age: 45, account_type: :admin)
61
+
62
+ user.middle? # => true
63
+ user.age_group # => :middle
64
+ user.account_type_admin? # => true
65
+ user.account_type_guest? # => false
66
+ ```
67
+
68
+ ### Strict Mode
69
+
70
+ If you want predicate checks to pass only when the method returns a **Symbol**, enable `strict: true`:
71
+
72
+ ```ruby
73
+ class User
74
+ include Predicateable
75
+
76
+ attr_reader :role
77
+
78
+ predicate :role, [:editor, :viewer], strict: true
79
+
80
+ def initialize(role:)
81
+ @role = role
82
+ end
83
+ end
84
+
85
+ User.new(role: :editor).editor? # => true
86
+ User.new(role: "editor").editor? # => false (strict mode)
87
+ ```
88
+
89
+ ## Behavior Summary
90
+
91
+ | Feature | Description |
92
+ | --------------- | ---------------------------------------------- |
93
+ | `prefix:` | Adds method name prefix (e.g. `account_type_`) |
94
+ | `strict:` | Only matches `Symbol` values |
95
+ | `respond_to?` | Works with predicate methods |
96
+ | `NoMethodError` | Raised when calling undefined predicates |
97
+
98
+ ## Testing
99
+
100
+ This gem uses [Minitest](https://github.com/minitest/minitest). To run tests:
101
+
102
+ ```sh
103
+ bundle exec rake test
104
+ ```
105
+
106
+ ## Contributing
107
+
108
+ Bug reports and pull requests are welcome! If you have ideas for improvement or questions, feel free to open an issue.
109
+
110
+ ## License
111
+
112
+ This project is licensed under the MIT License. See the [LICENSE.txt](LICENSE.txt) file for details.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Predicateable
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "predicateable/version"
4
+
5
+ module Predicateable
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def predicate(name, values, prefix: false, strict: false)
12
+ values.each do |value|
13
+ method = prefix ? "#{name}_#{value}?" : "#{value}?"
14
+
15
+ define_method(method) do
16
+ actual = send(name)
17
+
18
+ if strict
19
+ actual == value
20
+ else
21
+ actual.to_s == value.to_s
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ module Predicateable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: predicateable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Naoki Nishiguchi
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rake
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '13.0'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '13.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: minitest
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.22'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '5.22'
40
+ description: Predicateable allows you to define predicate methods (like `admin?`)
41
+ based on a method that returns a Symbol. Similar to Rails enums, with optional strict
42
+ checking and prefix support.
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE.txt
48
+ - README.md
49
+ - Rakefile
50
+ - lib/predicateable.rb
51
+ - lib/predicateable/version.rb
52
+ - sig/predicateable.rbs
53
+ homepage: https://github.com/nsgc/predicateable
54
+ licenses:
55
+ - MIT
56
+ metadata:
57
+ homepage_uri: https://github.com/nsgc/predicateable
58
+ source_code_uri: https://github.com/nsgc/predicateable.git
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.1.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.6.7
74
+ specification_version: 4
75
+ summary: Dynamically defines predicate methods based on symbolic return values.
76
+ test_files: []