Minitwin

Minitwin Logowidth=20%

What is Minitwin?

It is a tiny presentation layer with a small DSL to define properties, collections, light type coercion via dry-types, and optional ActiveModel validations. It's designed to be framework-friendly but not framework-bound.

Dependencies

  • Ruby >= 3.3
  • dry-types (optional) — enables the type: coercion option on properties
  • activesupport (optional)to_hash returns HashWithIndifferentAccess when available, otherwise a plain Hash
  • activemodel (optional) — enables the validates: DSL and valid?; without it, twins are always considered valid

Getting Started

Add to your Gemfile:

gem "minitwin"

Or build and install locally:

gem build minitwin.gemspec && gem install minitwin-*.gem

Then define your first Twin:

class UserTwin < Minitwin
  property :id, type: Types::Params::Integer.lax
  property :name, validates: { presence: true }
  property :active, type: Types::Params::Bool.lax
end

user = UserTwin.from_hash(
  id: "42",
  name: "Alex",
  active: "1"
)
user.id     #=> 42 (coerced)
user.name   #=> "Alex"
user.active #=> true (coerced)

user.to_json
#=> '{"id":42,"name":"Alex","active":true}'

See USAGE for further examples.

RBS

Minitwin comes with basic RBS signature files for its public interface. If you want to use them in your project, you have to declare the dependency explicitly in your rbs_collection.yaml like so:

  gems:
    - name: minitwin

Furthermore, Minitwin ships with a rake task, which generates the RBS signature files for all your classes inheriting from Minitwin.

If you use Rails, the task is automatically loaded. Just run:

rails minitwin:generate_rbs

If you work with plain Ruby, you have to load the task in your Rakefile:

load Gem.find_files("tasks/minitwin.rake").first

Then run the task:

rake minitwin:generate_rbs

By default, the task will output the rbs files in sig/generated/. You can adjust this by setting a task argument or an ENV var MINITWIN_RBS_DIR. If both is set, the argument will be used.

rake minitwin:generate_rbs[sig/custom_path]

MINITWIN_RBS_DIR=sig/custom_path rake minitwin:generate_rbs

Inspiration

Minitwin was inspired by Disposable, a gem for building twin objects as a decorator layer on top of your domain models.

License

Minitwin is licensed under the MIT License. See LICENSE for more details.