Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mblumtritt/invariable
The Invariable data class for Ruby.
https://github.com/mblumtritt/invariable
data-structures ruby ruby-gem
Last synced: about 2 months ago
JSON representation
The Invariable data class for Ruby.
- Host: GitHub
- URL: https://github.com/mblumtritt/invariable
- Owner: mblumtritt
- License: bsd-3-clause
- Created: 2021-12-26T20:47:46.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-27T17:42:07.000Z (about 2 years ago)
- Last Synced: 2024-04-25T10:20:32.496Z (9 months ago)
- Topics: data-structures, ruby, ruby-gem
- Language: Ruby
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Invariable
An Invariable bundles a number of read-only attributes.
It can be used like a Hash as well as an Array. It supports subclassing and pattern matching.An Invariable can be created explicitly as a Class like a Struct. Or existing classes can easily be extended to an Invariable.
- Gem: [rubygems.org](https://rubygems.org/gems/invariable)
- Source: [github.com](https://github.com/mblumtritt/invariable)
- Help: [rubydoc.info](https://rubydoc.info/gems/invariable)## Sample
```ruby
require 'invariable'class Person
include Invariable
attributes :name, :last_name
attribute address: Invariable.new(:city, :zip, :street)def full_name
"#{name} #{last_name}"
end
end
...
john = Person.new(name: 'John', last_name: 'Doe')
john.full_name #=> "John Doe"
john.address.city #=> nil
john = john.update(
address: { street: '123 Main St', city: 'Anytown', zip: '45678' }
)
john.dig(:address, :city) #=> "Anytown"```
For more samples see [the samples dir](./examples)
## Installation
Use [Bundler](http://gembundler.com/) to use Invariiable in your own project:
Include in your `Gemfile`:
```ruby
gem 'invariable'
```and install it by running Bundler:
```bash
bundle
```To install the gem globally use:
```bash
gem install invariable
```After that you need only a single line of code in your project to have all tools on board:
```ruby
require 'invariable'
```