-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Types::Data structs #13
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
So that if not undefined, we get invalid result
Data structs coerce themselves from a Hash, or return instances as-is
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Types::Data
Types:: Data
provides a superclass to define inmutable structs or value objects with typed / coercible attributes.These classes can be instantiated normally, and expose
#valid?
and#errors
Note that these instances cannot be mutated (there's no attribute setters), but they can be copied with partial attributes with
#with
It supports nested attributes:
Or arrays of nested attributes:
Or use struct classes defined separately:
Arrays and other types support composition and helpers. Ex.
#default
.Passing a named struct class AND a block will subclass the struct and extend it with new attributes:
The same works with arrays:
Note that this does NOT work with union'd or piped structs.
Optional Attributes
Using
attribute?
allows for optional attributes. If the attribute is not present, it will be set tonil
.Struct Inheritance
Structs can inherit from other structs. This is useful for defining a base struct with common attributes.
Equality with
#==
#==
is implemented to compare attributes, recursively.[]
SyntaxThe
[]
syntax can be used as a shorthand.Like
Plumb::Types::Hash``, suffixing a key with
?` makes it optional.This syntax creates subclasses too.
Struct composition
Types::Data
supports all the composition operators and helpers.Note however that, once you wrap a struct in a composition, you can't instantiate it with
.new
anymore (but you can still use#parse
or#resolve
like any other Plumb type).Recursive struct definitions
You can use
#defer
like with any other type definitions.How
This PR introduces a
Plumb::Attributes
module that provides the.attribute
class macro.It also makes it possible to make classes composable with
extend Plumb::Composable
(as opposed toinclude Plumb::Composable
for other core Plumb types.So,
Types::Data
is this:This means you can have classes that are composable but don't have the
.attribute
macro, and classes that have typed attributes, but do not include the composable methods (Plumb will still wrap them and make them composable, as long as they implement.call(result) => result
).This refactor makes it possible to have other custom classes act like composable types without wrapping:
This PR also adds a
#to_json_schema
method to all Composables, including struct classes.