Skip to content

avo-hq/prop_initializer

Repository files navigation

Gem Version

Prop Initializer

Overview

The prop_initializer gem is a flexible tool for defining properties on Ruby classes.

It's a fork of the Literal Gem, with a few tweaks. We sincerely thank Joel Drapper for the inspiration and base code that made this possible.

With prop_initializer, you can easily declare properties for any class, giving you flexible options for default values and more. However, the scope is narrowed down by removing strict typing requirements, providing a more lightweight and adaptable interface.

Installation

To use prop_initializer in your Ruby project, add the following to your Gemfile:

gem "prop_initializer"

Then, run the bundle command to install it:

bundle install

Usage

To start using prop_initializer , you need to include the module in the class where you want to define properties.

Step 1: Extend the Properties Module

In any class, extend PropInitializer::Properties to enable the ability to define properties:

class MyClass
  extend PropInitializer::Properties
end

Step 2: Define Properties

Properties can be declared using the prop method, which generates writers and makes them available as instance variables (e.g., @name, @size, etc.).

Basic Syntax:

prop :name                # A simple property, accessible via @name
prop :size, default: :md  # A property with a default value of :md
prop :args, kind: :*      # A property for handling splat arguments
prop :kwargs, kind: :**   # A property for handling keyword arguments

Custom Property with Block:

You can define a custom processing block when declaring a property, for example:

prop :icon do |value|
  value&.to_sym       # Converts the property value to a symbol
end

Step 3: Accessing Properties

By default, PropInitializer generates a writer for each declared property. The properties are stored as instance variables, which can be accessed within the class:

@name    # Accesses the value of the 'name' property
@size    # Accesses the value of the 'size' property

Public Reader:

If you want to generate a public reader (getter) for a property, use the reader: :public option when defining the property:

prop :title, reader: :public

This will automatically generate a public getter method, allowing you to retrieve the property like so:

my_class_instance.title  # Public getter for the 'title' property

Example:

class MyComponent
  extend PropInitializer::Properties

  prop :name, reader: :public
  prop :size, default: :md, reader: :public
  prop :args, kind: :*
  prop :kwargs, kind: :**
  prop :icon do |value|
    value&.to_sym
  end
end

component = MyComponent.new(name: "Button", size: :lg)
component.name # => "Button"
component.size # => :lg

Key Differences from Literal

While prop_initializer is based on the Literal Gem, there are some important differences:

  • No Type Requirement: Literal's properties system enforces types, while prop_initializer omits them for flexibility. You can define properties without needing to specify a type.

  • Simplified Initializer: The initialization process has been modified to avoid requiring types at the time of property definition.

Acknowledgements

Special thanks to the team at Literal for their pioneering work on property-based object initialization. This gem builds upon the foundation laid by their work.

If you're looking for a more type-strict approach to property initialization, we encourage you to check out the original Literal Gem.

Thank you for using prop_initializer! We hope this tool helps make your property management more efficient and adaptable in your Ruby projects.

Open Source

  • active_storage-blurhash - A plug-n-play blurhash integration for images stored in ActiveStorage
  • avo - Build Content management systems with Ruby on Rails
  • class_variants - Easily configure styles and apply them as classes. Very useful when you're implementing Tailwind CSS components and call them with different states.
  • stimulus-confetti - The easiest way to add confetti to your StimulusJS app

Try Avo ⭐️

If you enjoyed this gem try out Avo. It helps developers build Internal Tools, Admin Panels, CMSes, CRMs, and any other type of Business Apps 10x faster on top of Ruby on Rails.

License

The gem is available as open source under the terms of the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •