Extant

Sponsored by NCC Group Domain Services

Extant provides attribute management for Ruby objects. It is somewhat similar to attr_accessor, except it does many additional things like, type coercion, change tracking (dirty attributes), and most importantly tracking of existence.

Installation

$ gem install extant

or in your Gemfile

gem 'extant'

Getting Started

class Widget
  include Extant::Attributes

  attribute :name, String
  attribute :price, Integer
  attribute :production_ready, Extant::Coercers::Boolean, default: false
end

Basic Usage

widget = Widget.new(
  name: 'Doodad',
  price: 100
)

widget.name              # => 'Doodad'
widget.price             # => 100
widget.production_ready  # => false

widget.attributes  # => { name: 'Doodad', price: 100, production_ready: false }

Coercion

widget = Widget.new(
  price: 'not a number'
)

widget.extant_attributes[:price].coerced?          # => false
widget.price                                       # => nil
widget.extant_attributes[:price].pre_coerce_value  # => 'not a number'

Existence

widget = Widget.new

widget.name                           # => nil
widget.extant_attributes[:name].set?  # => false

widget = Widget.new(
  name: nil
)

widget.name                               # => nil
widget.extant_attributes[:name].set?      # => true
widget.extant_attributes[:name].coerced?  # => true

Contributing

  1. Fork
  2. Make changes
  3. Pull request
  4. Celebrate