Lab42::DataClass
A dataclass with an immutable API (you can still change the state of the object with metaprogramming and lab42_immutable is not ready yet!)
Usage
gem install lab42_data_class
With bundler
gem 'lab42_data_class'
In your code
require 'lab42/data_class'
So what does it do?
Well let us speculate about it to find out:
Context: DataClass function
Given
let(:my_data_class) { DataClass(:name, email: nil) }
let(:my_instance) { my_data_class.new(name: "robert") }
Then we can access its fields
expect(my_instance.name).to eq("robert")
expect(my_instance.email).to be_nil
But we cannot access undefined fields
expect{ my_instance.undefined }.to raise_error(NoMethodError)
And we need to provide values to fields without defaults
expect{ my_data_class.new(email: "[email protected]") }
.to raise_error(ArgumentError, "missing initializers for [:name]")
And we can extract the values
expect(my_instance.to_h).to eq(name: "robert", email: nil)
Context: Immutable
Given
let(:other_instance) { my_instance.merge(email: "[email protected]") }
Then we have a new instance with the old instance unchanged
expect(other_instance.to_h).to eq(name: "robert", email: "[email protected]")
expect(my_instance.to_h).to eq(name: "robert", email: nil)
Context: Defining behavior with blocks
Given
let :my_data_class do
DataClass :value, prefix: "<", suffix: ">" do
def show
[prefix, value, suffix].join
end
end
end
let(:my_instance) { my_data_class.new(value: 42) }
Then I have defined a method on my dataclass
expect(my_instance.show).to eq("<42>")
Context: Making a dataclass from a class
Given
class DC
dataclass x: 1, y: 41
def sum; x + y end
end
Then we can define methods on it
expect(DC.new.sum).to eq(42)
And we have a nice name for our instances
expect(DC.new.class.name).to eq("DC")
LICENSE
Copyright 2022 Robert Dober [email protected]
Apache-2.0 c.f LICENSE