rice_bubble 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: 4e81d5917da322d30c15b8def2c989d5b725daae03a2b843cb88534f273b6f49
4
+ data.tar.gz: acf5daa7c14f1a059db017ffa3a2dddf91d674b6b5694a5b0b6506e3abe8159e
5
+ SHA512:
6
+ metadata.gz: c6311322cd18622ebf65dc0dc0e78cf9e89844e5cd3cc9f3564165400a3d9af70d447073025093532cdbdfeadbcc84008756a61d628c239f0de2756710f6dea2
7
+ data.tar.gz: 5c025fa2266a0ba755d946a6fc259164609f6549d5f5720fcb36c09790ca86f2c8ebd0a1c27e6dd37d01b3c08c6858c32866f644c67eacbb03c39fb709c97081
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,36 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+ - rubocop-factory_bot
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 3.2.2
8
+ NewCops: enable
9
+
10
+ Layout/FirstHashElementIndentation:
11
+ EnforcedStyle: consistent
12
+
13
+ Layout/LineLength:
14
+ Max: 80
15
+ AllowedPatterns:
16
+ - '^\s*#'
17
+
18
+ Layout/MultilineMethodCallIndentation:
19
+ EnforcedStyle: indented
20
+
21
+ RSpec/NestedGroups:
22
+ Enabled: false
23
+
24
+ Style/Documentation:
25
+ Enabled: false
26
+
27
+ Style/FrozenStringLiteralComment:
28
+ Enabled: false
29
+
30
+ Style/StringLiterals:
31
+ Enabled: true
32
+ EnforcedStyle: single_quotes
33
+
34
+ Style/StringLiteralsInInterpolation:
35
+ Enabled: true
36
+ EnforcedStyle: double_quotes
@@ -0,0 +1,3 @@
1
+ {
2
+ "editor.rulers": [80]
3
+ }
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-04
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at matt.powell@usabilityhub.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rice_bubble.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rice_bubble (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ docile (1.4.0)
12
+ json (2.6.3)
13
+ parallel (1.23.0)
14
+ parser (3.2.2.1)
15
+ ast (~> 2.4.1)
16
+ rainbow (3.1.1)
17
+ rake (13.0.6)
18
+ regexp_parser (2.8.0)
19
+ rexml (3.2.5)
20
+ rspec (3.12.0)
21
+ rspec-core (~> 3.12.0)
22
+ rspec-expectations (~> 3.12.0)
23
+ rspec-mocks (~> 3.12.0)
24
+ rspec-core (3.12.2)
25
+ rspec-support (~> 3.12.0)
26
+ rspec-expectations (3.12.3)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-its (1.3.0)
30
+ rspec-core (>= 3.0.0)
31
+ rspec-expectations (>= 3.0.0)
32
+ rspec-mocks (3.12.5)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-support (3.12.0)
36
+ rubocop (1.50.2)
37
+ json (~> 2.3)
38
+ parallel (~> 1.10)
39
+ parser (>= 3.2.0.0)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml (>= 3.2.5, < 4.0)
43
+ rubocop-ast (>= 1.28.0, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 2.4.0, < 3.0)
46
+ rubocop-ast (1.28.1)
47
+ parser (>= 3.2.1.0)
48
+ rubocop-capybara (2.18.0)
49
+ rubocop (~> 1.41)
50
+ rubocop-factory_bot (2.22.0)
51
+ rubocop (~> 1.33)
52
+ rubocop-rake (0.6.0)
53
+ rubocop (~> 1.0)
54
+ rubocop-rspec (2.22.0)
55
+ rubocop (~> 1.33)
56
+ rubocop-capybara (~> 2.17)
57
+ rubocop-factory_bot (~> 2.22)
58
+ ruby-progressbar (1.13.0)
59
+ simplecov (0.21.2)
60
+ docile (~> 1.1)
61
+ simplecov-html (~> 0.11)
62
+ simplecov_json_formatter (~> 0.1)
63
+ simplecov-html (0.12.3)
64
+ simplecov_json_formatter (0.1.4)
65
+ unicode-display_width (2.4.2)
66
+
67
+ PLATFORMS
68
+ arm64-darwin-22
69
+
70
+ DEPENDENCIES
71
+ rake (~> 13.0)
72
+ rice_bubble!
73
+ rspec (~> 3.0)
74
+ rspec-its (~> 1.3)
75
+ rubocop (~> 1.21)
76
+ rubocop-factory_bot (~> 2.22.0)
77
+ rubocop-rake (~> 0.6.0)
78
+ rubocop-rspec (~> 2.22.0)
79
+ simplecov (~> 0.21.2)
80
+
81
+ BUNDLED WITH
82
+ 2.4.12
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Matt Powell
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,137 @@
1
+ # RiceBubble
2
+
3
+ Simple serialization with a sprinkling of type safety. Part of your complete breakfast.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add rice_bubble
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install rice_bubble
14
+
15
+ ## Usage
16
+
17
+ Create a subclass of `RiceBubble::Serializer` and add some attributes:
18
+
19
+ ```ruby
20
+ class CharacterSerializer < RiceBubble::Serializer
21
+ attributes(
22
+ name: string,
23
+ player_name: optional(string),
24
+ class: enum('fighter', 'wizard', 'thief'),
25
+ stats: object(
26
+ strength: integer,
27
+ dexterity: integer,
28
+ constitution: integer,
29
+ intelligence: integer,
30
+ wisdom: integer,
31
+ charisma: integer,
32
+ )
33
+ )
34
+ end
35
+ ```
36
+
37
+ Then you can use the new serializer to convert your data to a JSON-friendly representation:
38
+
39
+ ```ruby
40
+ json = CharacterSerializer.call(my_character) # => { name: "...", ... }
41
+ ```
42
+
43
+ ### Attribute types
44
+
45
+ | Name | Expects | Options | Example |
46
+ | ------------ | -------------------------- | ---------------------------------------- | ------------------------------------------------ |
47
+ | `any` | any of the specified types | array of attribute types | `any(string, integer)` |
48
+ | `boolean` | `true` or `false` | | `boolean` |
49
+ | `date` | `Date` or `#to_date` | | `date` |
50
+ | `enum` | matching string | array of string values | `enum('red', 'blue', 'green')` |
51
+ | `integer` | `Integer` or `#to_i` | `max`, `min` | `integer(min: 3, max: 20)` |
52
+ | `literal` | exactly matching value | literal | `literal('foo')` |
53
+ | `number` | `Numeric` | `max`, `min` | `number(min: 3, max: 20)` |
54
+ | `object` | `Object` or `Hash` | hash of object names and attribute types | `object(name: string, age: integer)` |
55
+ | `optional` | value or `nil` | attribute type | `optional(string)` |
56
+ | `serialized` | `Object` or `Hash` | `Serializer` class | `serialized(SkillSerializer)` |
57
+ | `string` | `String` or `#to_s` | `max`, `min`, `format` | `string(min: 3, max: 20, format: /\A[a-z]+\z/i)` |
58
+ | `time` | `Time` or `#to_time` | | `time` |
59
+
60
+ In this way, you can recursively build up quite complex nested structures of attributes in your serializer.
61
+
62
+ ### `optional`
63
+
64
+ All attributes are required unless marked as optional. You can either do this with
65
+ the `optional` attribute type, or by appending `.optional` to the attribute definition.
66
+ That is, the following are equivalent:
67
+
68
+ ```ruby
69
+ optional(string(min: 3))
70
+ string(min: 3).optional
71
+ ```
72
+
73
+ ### Implementing your own attribute types
74
+
75
+ The attribute types are looked up automatically in the namespace
76
+ `RiceBubble::Attributes`. Any subclass of `RiceBubble::Attributes::Base` located there
77
+ will automatically be found by the serializer.
78
+
79
+ ```ruby
80
+ class RiceBubble::Serializer::Wrap < RiceBubble::Attributes::Base
81
+ def initialize(with: %w([ ]), &)
82
+ super(&)
83
+ @before, @after = with
84
+ end
85
+
86
+ def coerce(value)
87
+ "#{before}#{value}#{after}"
88
+ end
89
+ end
90
+
91
+ class WrappingSerializer < RiceBubble::Serializer
92
+ attributes(
93
+ name: wrap(with: %w({ }))
94
+ )
95
+ end
96
+
97
+ WrappingSerializer.call({ name: 'Spicy Beef' }) # => { name: '{Spicy Beef}' }
98
+ ```
99
+
100
+ ### Fetching values from an object
101
+
102
+ By default, attributes know how to fetch their values from the object being serialized
103
+ by either:
104
+
105
+ - invoking an instance method (on an object); or
106
+ - looking up a hash key (on a hash)
107
+
108
+ You can change how a value is retrieved by passing a block to the attribute:
109
+
110
+ ```ruby
111
+ class FullNameSerializer
112
+ attributes(
113
+ full_name: string { |o, _| [o.first_name, o.last_name].join(' ') }
114
+ )
115
+ end
116
+ ```
117
+
118
+ The block takes two values: the object being serialized, and (optionally)
119
+ the name of the key being retrieved.
120
+
121
+ ## Development
122
+
123
+ 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.
124
+
125
+ 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).
126
+
127
+ ## Contributing
128
+
129
+ Bug reports and pull requests are welcome on GitLab at https://gitlab.com/fauxparse/rice_bubble. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://gitlab.com/fauxparse/rice_bubble/blob/main/CODE_OF_CONDUCT.md).
130
+
131
+ ## License
132
+
133
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
134
+
135
+ ## Code of Conduct
136
+
137
+ Everyone interacting in the RiceBubble project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rice_bubble/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ require 'rubocop/rake_task'
7
+
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop]
@@ -0,0 +1,33 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Any < Base
4
+ attr_reader :members
5
+
6
+ def initialize(*members, &)
7
+ super(&)
8
+ @members = members.flatten
9
+ end
10
+
11
+ def valid?(value)
12
+ !which(value).nil?
13
+ end
14
+
15
+ def validate!(value, path:, **)
16
+ member = which(value)
17
+ return unless member.nil?
18
+
19
+ expected = "one of [#{members.map(&:description).join(", ")}]"
20
+ raise ValidationError,
21
+ "#{path} expected #{expected} but received #{value.inspect}"
22
+ end
23
+
24
+ def coerce(value)
25
+ which(value)&.coerce(value) || value
26
+ end
27
+
28
+ def which(value)
29
+ members.find { |member| member.valid?(value) }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Array < Base
4
+ attr_reader :members
5
+
6
+ def initialize(members, &)
7
+ super(&)
8
+ @members = instantiate(members)
9
+ end
10
+
11
+ def valid?(value)
12
+ return false unless value.respond_to?(:all?)
13
+
14
+ value.all? do |child|
15
+ members.valid?(child)
16
+ end
17
+ end
18
+
19
+ def validate!(_value, coerced:, path:)
20
+ coerced.each.with_index do |child, index|
21
+ members.validate!(
22
+ child,
23
+ coerced: members.coerce(child),
24
+ path: "#{path}[#{index}]"
25
+ )
26
+ end
27
+ end
28
+
29
+ def coerce(value)
30
+ value.respond_to?(:to_a) ? value.to_a : value
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,62 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Base
4
+ def initialize(&block)
5
+ @fetcher = block
6
+ end
7
+
8
+ def fetch(object, name)
9
+ if @fetcher
10
+ @fetcher.call(object, name)
11
+ elsif object.respond_to?(name)
12
+ object.public_send(name)
13
+ else
14
+ object[name]
15
+ end
16
+ end
17
+
18
+ def valid?(_value)
19
+ true
20
+ end
21
+
22
+ def coerce(value)
23
+ value
24
+ end
25
+
26
+ def validate!(value, coerced:, path:)
27
+ return if valid?(coerced)
28
+
29
+ raise ValidationError,
30
+ "#{path} expected #{description} but received #{value.inspect}"
31
+ end
32
+
33
+ def call(value, path: '')
34
+ coerced = coerce(value)
35
+ validate!(value, coerced:, path:)
36
+ coerced
37
+ end
38
+
39
+ def optional
40
+ Optional.new(self)
41
+ end
42
+
43
+ def description
44
+ soft_name = self.class.name.split('::').last
45
+ .gsub(/([a-z])([A-Z])/, '\1 \2')
46
+ .downcase
47
+ article = soft_name.start_with?(/[aeiou]/) ? 'an' : 'a'
48
+ "#{article} #{soft_name}"
49
+ end
50
+
51
+ private
52
+
53
+ def instantiate(class_or_instance)
54
+ if class_or_instance.is_a?(Class) && class_or_instance < Serializer
55
+ class_or_instance.new
56
+ else
57
+ class_or_instance
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,13 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Boolean < Base
4
+ def validate!(value, path:, **)
5
+ super(value, coerced: value, path:)
6
+ end
7
+
8
+ def valid?(value)
9
+ [true, false].include?(value)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ require 'date'
2
+
3
+ module RiceBubble
4
+ class Attributes
5
+ class Date < Base
6
+ def valid?(value)
7
+ value.respond_to?(:to_date)
8
+ end
9
+
10
+ def coerce(value)
11
+ value.respond_to?(:to_date) ? value.to_date : value
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Enum < Any
4
+ def initialize(*members, &)
5
+ super(*members.flatten.map { |m| Literal.new(m) }, &)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Integer < Number
4
+ attr_reader :min, :max
5
+
6
+ def valid?(value)
7
+ value.is_a?(::Integer) && super
8
+ end
9
+
10
+ def coerce(value)
11
+ case value
12
+ when nil then nil
13
+ when ::String then value.match?(/\A-?\d+\z/) ? value.to_i : value
14
+ else value.respond_to?(:to_i) ? value.to_i : value
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Literal < Base
4
+ attr_reader :literal
5
+
6
+ def initialize(literal, &)
7
+ super(&)
8
+ @literal = literal
9
+ end
10
+
11
+ def valid?(value)
12
+ value == literal
13
+ end
14
+
15
+ def description
16
+ literal.inspect
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,33 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Number < Base
4
+ attr_reader :min, :max
5
+
6
+ def initialize(min: nil, max: nil, &)
7
+ super(&)
8
+ @min = min
9
+ @max = max
10
+ end
11
+
12
+ def valid?(value)
13
+ value.is_a?(::Numeric) &&
14
+ !min&.send(:>, value) &&
15
+ !max&.send(:<, value)
16
+ end
17
+
18
+ def description
19
+ result = super
20
+
21
+ if min && max
22
+ "#{result} between #{min} and #{max}"
23
+ elsif min
24
+ "#{result} greater than or equal to #{min}"
25
+ elsif max
26
+ "#{result} less than or equal to #{max}"
27
+ else
28
+ result
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Object < Base
4
+ attr_reader :children
5
+
6
+ def initialize(children = {}, &)
7
+ super(&)
8
+ @children = Attributes.new(
9
+ children.transform_values(&method(:instantiate))
10
+ )
11
+ end
12
+
13
+ def valid?(value)
14
+ children.all? do |name, attr|
15
+ attr.valid?(value[name])
16
+ end
17
+ end
18
+
19
+ def validate!(value, path:, **)
20
+ children.each do |name, attr|
21
+ child = attr.fetch(value, name)
22
+ coerced = attr.coerce(child)
23
+ attr.validate!(child, coerced:, path: "#{path}.#{name}")
24
+ end
25
+ end
26
+
27
+ def coerce(value)
28
+ children.map { |name, attr| attr.coerce(attr.fetch(value, name)) }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,28 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Optional < Base
4
+ attr_reader :child
5
+
6
+ def initialize(child, &)
7
+ super(&)
8
+ @child = child
9
+ end
10
+
11
+ def valid?(value)
12
+ value.nil? || child.valid?(child.coerce(value))
13
+ end
14
+
15
+ def call(value, path: '')
16
+ value && child.call(value, path:)
17
+ end
18
+
19
+ def coerce(value)
20
+ value && child.coerce(value)
21
+ end
22
+
23
+ def description
24
+ "#{child.description} (optional)"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Serialized < Base
4
+ attr_reader :serializer
5
+
6
+ def initialize(serializer, &)
7
+ super(&)
8
+ @serializer = serializer.new
9
+ end
10
+
11
+ def valid?(value)
12
+ serializer.valid?(value)
13
+ end
14
+
15
+ def validate!(value, path:, **)
16
+ serializer.validate!(value, path:)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,45 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class String < Base
4
+ attr_reader :min, :max, :format
5
+
6
+ def initialize(min: nil, max: nil, format: nil, &)
7
+ super(&)
8
+ @min = min
9
+ @max = max
10
+ @format = format
11
+ end
12
+
13
+ def valid?(value)
14
+ value.is_a?(::String) &&
15
+ !min&.send(:>, value.length) &&
16
+ !max&.send(:<, value.length) &&
17
+ (!format || format.match?(value))
18
+ end
19
+
20
+ def call(value, path: '')
21
+ super(value.to_s, path:)
22
+ end
23
+
24
+ def coerce(value)
25
+ value.respond_to?(:to_s) ? value.to_s : value
26
+ end
27
+
28
+ def description
29
+ result = super
30
+
31
+ if min && max
32
+ result = "#{result} between #{min} and #{max} characters long"
33
+ elsif min
34
+ result = "#{result} of at least #{min} characters"
35
+ elsif max
36
+ result = "#{result} of up to #{max} characters"
37
+ end
38
+
39
+ result = "#{result} matching #{format.inspect}" if format
40
+
41
+ result
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,27 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ class Time < Base
4
+ def valid?(value)
5
+ return false unless valid_time?(value)
6
+
7
+ value.respond_to?(:to_time)
8
+ end
9
+
10
+ def coerce(value)
11
+ return nil unless valid_time?(value)
12
+
13
+ value.respond_to?(:to_time) ? value.to_time : value
14
+ end
15
+
16
+ private
17
+
18
+ def valid_time?(value)
19
+ case value
20
+ when ::DateTime then true
21
+ when ::Date then false
22
+ else value.respond_to?(:to_time)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,58 @@
1
+ module RiceBubble
2
+ class Attributes
3
+ attr_reader :attributes
4
+
5
+ include Enumerable
6
+
7
+ def initialize(attrs = {})
8
+ @attributes = {}
9
+ attrs.each do |name, attr|
10
+ self[name] = attr
11
+ end
12
+ end
13
+
14
+ def initialize_copy(other)
15
+ super
16
+ @attributes = other.attributes.dup
17
+ end
18
+
19
+ def each(&)
20
+ if block_given?
21
+ attributes.each(&)
22
+ else
23
+ to_enum(:each)
24
+ end
25
+ end
26
+
27
+ def include?(name)
28
+ attributes.key?(name.to_sym)
29
+ end
30
+
31
+ def [](name)
32
+ attributes[name.to_sym]
33
+ end
34
+
35
+ def []=(name, attr)
36
+ attributes[name] = attr
37
+ end
38
+
39
+ def map(&)
40
+ attributes.keys.each.with_object({}) do |name, hash|
41
+ hash[name] = yield(name, attributes[name])
42
+ end
43
+ end
44
+
45
+ def self.[](type)
46
+ @types ||= {}
47
+ @types[type] ||=
48
+ begin
49
+ class_name = type.to_s.gsub(/(^|_)(\w)/) do
50
+ ::Regexp.last_match(2).upcase
51
+ end
52
+ Object.const_get "RiceBubble::Attributes::#{class_name}"
53
+ end
54
+ rescue NameError
55
+ nil
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,69 @@
1
+ module RiceBubble
2
+ class Serializer
3
+ def call(object, path: nil)
4
+ attributes.map do |name, attr|
5
+ attr.call(attr.fetch(object, name), path: path || self.class.name)
6
+ end
7
+ end
8
+
9
+ def attributes
10
+ self.class.attributes
11
+ end
12
+
13
+ def valid?(object)
14
+ attributes.all? do |name, attr|
15
+ attr.valid?(attr.fetch(object, name))
16
+ end
17
+ end
18
+
19
+ def validate!(object, path: '', **)
20
+ path = self.class.name if path.empty?
21
+
22
+ attributes.each do |name, attr|
23
+ value = attr.fetch(object, name)
24
+ attr.validate!(
25
+ value,
26
+ coerced: attr.coerce(value),
27
+ path: "#{path}.#{name}"
28
+ )
29
+ end
30
+ end
31
+
32
+ def coerce(object)
33
+ object
34
+ end
35
+
36
+ class << self
37
+ def call(...)
38
+ new.call(...)
39
+ end
40
+
41
+ def attributes(attrs = nil, &)
42
+ @attributes ||=
43
+ if superclass == Object
44
+ Attributes.new
45
+ else
46
+ superclass.attributes.dup
47
+ end
48
+
49
+ @attributes.instance_eval(&) if block_given?
50
+ attrs&.each_pair { |name, attr| @attributes[name] = attr }
51
+ @attributes
52
+ end
53
+
54
+ def respond_to_missing?(name, include_private = false)
55
+ !Attributes[name].nil? || super
56
+ end
57
+
58
+ def method_missing(name, *args, **kwargs, &)
59
+ Attributes[name]&.new(*args, **kwargs, &) || super
60
+ end
61
+
62
+ def as(...)
63
+ serialized(...)
64
+ end
65
+
66
+ alias of as
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module RiceBubble
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'rice_bubble/version'
2
+ require_relative 'rice_bubble/attributes'
3
+ require_relative 'rice_bubble/attributes/base'
4
+ require_relative 'rice_bubble/attributes/any'
5
+ require_relative 'rice_bubble/attributes/array'
6
+ require_relative 'rice_bubble/attributes/boolean'
7
+ require_relative 'rice_bubble/attributes/date'
8
+ require_relative 'rice_bubble/attributes/enum'
9
+ require_relative 'rice_bubble/attributes/number'
10
+ require_relative 'rice_bubble/attributes/integer'
11
+ require_relative 'rice_bubble/attributes/literal'
12
+ require_relative 'rice_bubble/attributes/object'
13
+ require_relative 'rice_bubble/attributes/optional'
14
+ require_relative 'rice_bubble/attributes/serialized'
15
+ require_relative 'rice_bubble/attributes/string'
16
+ require_relative 'rice_bubble/attributes/time'
17
+ require_relative 'rice_bubble/serializer'
18
+
19
+ module RiceBubble
20
+ class ValidationError < StandardError; end
21
+ # Your code goes here...
22
+ end
@@ -0,0 +1,4 @@
1
+ module RiceBubble
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rice_bubble
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Powell
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-its
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-factory_bot
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.22.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.22.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.6.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.6.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.22.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.22.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.21.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.21.2
125
+ description: Simple serialization for Ruby objects. Part of your complete breakfast.
126
+ email:
127
+ - matt.powell@usabilityhub.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".rspec"
133
+ - ".rubocop.yml"
134
+ - ".vscode/settings.json"
135
+ - CHANGELOG.md
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - Gemfile.lock
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - lib/rice_bubble.rb
143
+ - lib/rice_bubble/attributes.rb
144
+ - lib/rice_bubble/attributes/any.rb
145
+ - lib/rice_bubble/attributes/array.rb
146
+ - lib/rice_bubble/attributes/base.rb
147
+ - lib/rice_bubble/attributes/boolean.rb
148
+ - lib/rice_bubble/attributes/date.rb
149
+ - lib/rice_bubble/attributes/enum.rb
150
+ - lib/rice_bubble/attributes/integer.rb
151
+ - lib/rice_bubble/attributes/literal.rb
152
+ - lib/rice_bubble/attributes/number.rb
153
+ - lib/rice_bubble/attributes/object.rb
154
+ - lib/rice_bubble/attributes/optional.rb
155
+ - lib/rice_bubble/attributes/serialized.rb
156
+ - lib/rice_bubble/attributes/string.rb
157
+ - lib/rice_bubble/attributes/time.rb
158
+ - lib/rice_bubble/serializer.rb
159
+ - lib/rice_bubble/version.rb
160
+ - sig/rice_bubble.rbs
161
+ homepage: https://gitlab.com/fauxparse/rice_bubble
162
+ licenses:
163
+ - MIT
164
+ metadata:
165
+ allowed_push_host: https://rubygems.org
166
+ homepage_uri: https://gitlab.com/fauxparse/rice_bubble
167
+ source_code_uri: https://gitlab.com/fauxparse/rice_bubble
168
+ rubygems_mfa_required: 'true'
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: 3.2.0
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubygems_version: 3.4.12
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: Simple serialization for Ruby objects.
188
+ test_files: []