ruckt 0.1.1

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: f40c0a659f8c3fa90cb6e4baa38f2a7d87e39d816370416ea8da3ac15bfc79a9
4
+ data.tar.gz: e31ac3cd16321e27c790e1717533f7b0de917288dcf79fd3468d55f87e54e565
5
+ SHA512:
6
+ metadata.gz: 6e4b897df7da094dd111e93eabd94172f442b1a92f4d7689b2e4dd963761718ba1e5b52ada3ef18ff5cf9d9df07586a05d4895a5bf95c2307d87cc6dea1cc233
7
+ data.tar.gz: 163a5e57fb3b9b66b03923b56ad6e42f5d555a7842419e98be5c5af41bee133e49f41dec8c6ec0b9e8d41819c0836f6e5bf07efca73c0d4797e6c224017d1a16
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,12 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.1] - 2024-03-27
4
+
5
+ - Fixed type validation for boolean values
6
+ - Improved nested structure handling
7
+ - Added comprehensive test coverage
8
+ - Added detailed documentation
9
+
10
+ ## [0.1.0] - 2024-03-26
11
+
12
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 TODO: Write your name
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,98 @@
1
+ # Ruckt
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/ruck.svg)](https://badge.fury.io/rb/ruck)
4
+ [![RSpec](https://github.com/OkayDave/ruck/actions/workflows/rspec.yml/badge.svg)](https://github.com/OkayDave/ruck/actions/workflows/rspec.yml)
5
+ [![Coverage Status](https://coveralls.io/repos/github/dave/ruck/badge.svg?branch=main)](https://coveralls.io/github/dave/ruck?branch=main)
6
+
7
+ A flexible, type-safe struct generator for Ruby that automatically infers and creates Ruby Struct-like classes from arbitrary data sources with runtime type validation.
8
+
9
+ ## Features
10
+
11
+ - Dynamic Struct Generation: Classes auto-created from hashes/data samples
12
+ - Type Safety: Runtime type validation for all attributes
13
+ - Nested Structures: Automatic handling of nested hashes
14
+ - Query Methods: Automatic boolean attribute methods (e.g., `active?`)
15
+ - Clear Error Messages: Descriptive errors for type mismatches
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'ruckt'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ ```bash
28
+ $ bundle install
29
+ ```
30
+
31
+ Or install it yourself as:
32
+
33
+ ```bash
34
+ $ gem install ruckt
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ### Basic Example
40
+
41
+ ```ruby
42
+ require 'ruckt'
43
+
44
+ # Create a struct from a hash
45
+ data = {
46
+ name: "Dave",
47
+ age: 40,
48
+ active: true
49
+ }
50
+
51
+ user = Ruckt.new(data)
52
+
53
+ # Access attributes
54
+ user.name # => "Dave"
55
+ user.age # => 40
56
+ user.active? # => true
57
+
58
+ # Set attributes
59
+ user.name = "John" # => works fine
60
+ user.age = "forty" # => raises TypeError
61
+ ```
62
+
63
+ ### Nested Structures
64
+
65
+ ```ruby
66
+ data = {
67
+ name: "Dave",
68
+ location: {
69
+ city: "Sheffield",
70
+ postcode: "S2"
71
+ }
72
+ }
73
+
74
+ person = Ruckt.new(data)
75
+
76
+ # Access nested attributes
77
+ person.location.city # => "Sheffield"
78
+ person.location.postcode # => "S2"
79
+
80
+ # Type validation works for nested structures too
81
+ person.location.city = 123 # => raises TypeError
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`.
89
+
90
+ Please ensure that any additions are fully covered by RSpec.
91
+
92
+ ## Contributing
93
+
94
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dave/ruck.
95
+
96
+ ## License
97
+
98
+ 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,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruckt
4
+ # The StructGenerator class is responsible for dynamically creating struct-like classes
5
+ # from hash data. It provides type safety and validation while maintaining a simple interface.
6
+ class StructGenerator
7
+ class << self
8
+ # Generates a new struct-like class from a hash of data
9
+ # @param data [Hash] The hash containing the data structure to model
10
+ # @return [Class] A new class with typed attributes matching the data structure
11
+ def generate(data)
12
+ # If input isn't a hash, return it as-is (handles non-hash edge cases)
13
+ return data unless data.is_a?(Hash)
14
+
15
+ # Create a new anonymous class that will become our struct
16
+ Class.new do
17
+ # Include type validation functionality
18
+ include TypeValidation
19
+
20
+ # Store type information for all attributes
21
+ # This will be used for validation when setting values
22
+ @type_info = {}
23
+
24
+ class << self
25
+ # Make type information accessible to instances
26
+ attr_reader :type_info
27
+
28
+ # Determines the type of a value for validation purposes
29
+ # @param value [Object] The value to infer the type from
30
+ # @return [Class, Array<Class>] The inferred type(s)
31
+ def infer_type(value)
32
+ case value
33
+ when Hash
34
+ # Empty hashes stay as Hash, non-empty become nested structs
35
+ value.empty? ? Hash : StructGenerator.generate(value)
36
+ when TrueClass, FalseClass
37
+ # Booleans can be either true or false, so we accept both types
38
+ [TrueClass, FalseClass]
39
+ else
40
+ # For all other values, use their class as the type
41
+ value.class
42
+ end
43
+ end
44
+ end
45
+
46
+ # Iterate through the data hash to define our struct's attributes
47
+ data.each do |key, value|
48
+ # Store the inferred type for this attribute
49
+ @type_info[key] = infer_type(value)
50
+
51
+ # Define a getter method that returns the instance variable
52
+ define_method(key) do
53
+ instance_variable_get("@#{key}")
54
+ end
55
+
56
+ # Define a setter method with type validation
57
+ define_method("#{key}=") do |val|
58
+ # Special handling for nested hash updates
59
+ if val.is_a?(Hash) && self.class.type_info[key].is_a?(Class) &&
60
+ self.class.type_info[key].respond_to?(:type_info)
61
+ # Create a temporary instance to validate the nested data
62
+ # This ensures all nested values have correct types
63
+ temp_instance = self.class.type_info[key].new
64
+ val.each do |k, v|
65
+ temp_instance.send("#{k}=", v)
66
+ end
67
+ instance_variable_set("@#{key}", temp_instance)
68
+ else
69
+ # For non-nested values, validate and set directly
70
+ validate_type(key, val)
71
+ instance_variable_set("@#{key}", val)
72
+ end
73
+ end
74
+
75
+ # For boolean attributes, add a convenience query method
76
+ # that ends with '?' (e.g., active?)
77
+ if value.is_a?(TrueClass) || value.is_a?(FalseClass)
78
+ define_method("#{key}?") { instance_variable_get("@#{key}") }
79
+ end
80
+
81
+ # For non-empty hashes, create a nested struct class
82
+ if value.is_a?(Hash) && !value.empty?
83
+ nested_class = StructGenerator.generate(value)
84
+ const_set(key.to_s.capitalize, nested_class)
85
+ end
86
+ end
87
+
88
+ # Define the initialize method that sets up a new instance
89
+ define_method(:initialize) do |values = {}|
90
+ values.each do |key, value|
91
+ # Special handling for nested structs
92
+ if self.class.type_info[key].is_a?(Class) && value.is_a?(Hash) &&
93
+ self.class.type_info[key].respond_to?(:type_info)
94
+ instance_variable_set("@#{key}", self.class.type_info[key].new(value))
95
+ else
96
+ # For non-nested values, use the setter for validation
97
+ send("#{key}=", value)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ # Module containing type validation functionality
106
+ # This is mixed into generated struct classes
107
+ module TypeValidation
108
+ # Validates that a value matches the expected type for an attribute
109
+ # @param key [Symbol] The attribute name
110
+ # @param value [Object] The value to validate
111
+ # @raise [TypeError] If the value doesn't match the expected type
112
+ def validate_type(key, value)
113
+ expected_type = self.class.type_info[key]
114
+
115
+ # Handle cases where multiple types are allowed (e.g., true/false)
116
+ if expected_type.is_a?(Array)
117
+ return if expected_type.any? { |type| value.is_a?(type) }
118
+ raise TypeError, "Expected #{key} to be one of #{expected_type.join(', ')}, got #{value.class}"
119
+ end
120
+
121
+ # Handle single type validation
122
+ return if value.is_a?(expected_type)
123
+ raise TypeError, "Expected #{key} to be #{expected_type}, got #{value.class}"
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruckt
4
+ VERSION = "0.1.1"
5
+ end
data/lib/ruckt.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ruckt/version"
4
+ require_relative "ruckt/struct_generator"
5
+
6
+ module Ruckt
7
+ class Error < StandardError; end
8
+
9
+ class << self
10
+ def new(data)
11
+ StructGenerator.generate(data)
12
+ end
13
+ end
14
+ end
data/sig/ruckt.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Ruckt
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruckt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dave Russell
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-03-27 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rspec
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '3.12'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '3.12'
26
+ - !ruby/object:Gem::Dependency
27
+ name: rubocop
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.57'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.57'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rubocop-rspec
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.27'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.27'
54
+ description: Automatically infer and create Ruby Struct-like classes from arbitrary
55
+ data sources with runtime type validation
56
+ email:
57
+ - dave.kerr@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - ".rubocop.yml"
64
+ - CHANGELOG.md
65
+ - CODE_OF_CONDUCT.md
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/ruckt.rb
70
+ - lib/ruckt/struct_generator.rb
71
+ - lib/ruckt/version.rb
72
+ - sig/ruckt.rbs
73
+ homepage: https://github.com/dave/ruckt
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ allowed_push_host: https://rubygems.org
78
+ homepage_uri: https://github.com/dave/ruckt
79
+ source_code_uri: https://github.com/dave/ruckt
80
+ changelog_uri: https://github.com/dave/ruckt/blob/main/CHANGELOG.md
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 3.1.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.6.2
96
+ specification_version: 4
97
+ summary: A flexible, type-safe struct generator for Ruby
98
+ test_files: []