Struct++
- This repository is archived
- No longer maintained
- I didn't remove old versions on https://rubygems.org/gems/striuct, but I no longer plan to update them.
Tested in Ruby 3.2 or 3.3
Add this line to your Gemfile
gem 'striuct', '~> 0.10.1'
Then add below code into your Ruby code
require 'striuct'
require 'striuct'
class Person < Striuct
member :full_name, AND(String, /\A.+\z/) # Flexible Validation
alias_member :name, :full_name # Use other name
end
# Inheritable
class User < Person
member :id, Integer, # Looks typed validation
default_proc: ->{User.next_id} # With default value
@id = 0
def self.next_id
@id += 1
end
end
john = User.new 'john'
john[:name] #=> 'john'
john.name = '' #=> Exception # Validate with setter
john.id #=> 1
ken = User[name: 'ken'] # Construct from hash
ken.id #=> 2
class Foo < Striuct
member :foo
member :bar, Numeric
member :with_adjuster, Integer,
&->v{Integer v} # Use adjuster before a setter
end
foo = Foo.new
# nil <-> unassigned
foo.foo #=> nil
foo.assigned?(:foo) #=> false
foo.foo = nil
foo.assigned?(:foo) #=> true
# Lock to a member
foo.lock(:foo)
foo.foo = nil #=> error
foo.bar = 1.2 #=> pass # memorize 1.2's class is Float
foo.bar = 1 #=> error # 1 is not Float
# With adjuster
foo.with_adjuster = '5'
foo.with_adjuster #=> 5 # Casted via adjuster
class UseMustOption < Striuct
member :foo, Integer, must: true
end
UseMustOption.new #=> InvalidOperationError "`foo` require a value under `must` option "
- Easy and Flexible Validations
- Basically use
#===
for the validation - The pattern builder DSL is just using eqq
- When passed the Proc, it will be evaluated in the instance context
- Basically use
- Prevent to conflict member names
- Lock setters for each member
- Hook just before setters
- Default value
- Member aliasing
- Inheritable
- Handling between nil <-> unassigned
- Similar API for Hash
- Base API looks like Struct
- Pure Ruby :)