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.
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 installTo start using prop_initializer , you need to include the module in the class where you want to define properties.
In any class, extend PropInitializer::Properties to enable the ability to define properties:
class MyClass
extend PropInitializer::Properties
endProperties can be declared using the prop method, which generates writers and makes them available as instance variables (e.g., @name, @size, etc.).
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 argumentsYou 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
endBy 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' propertyIf you want to generate a public reader (getter) for a property, use the reader: :public option when defining the property:
prop :title, reader: :publicThis will automatically generate a public getter method, allowing you to retrieve the property like so:
my_class_instance.title # Public getter for the 'title' propertyclass 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 # => :lgKey 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_initializeromits 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.
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.
active_storage-blurhash- A plug-n-play blurhash integration for images stored in ActiveStorageavo- Build Content management systems with Ruby on Railsclass_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
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.
The gem is available as open source under the terms of the MIT License.
