module ROM::Options

Helper module for classes with a constructor accepting option hash

This allows us to DRY up code as option hash is a very common pattern used across the codebase. It is an internal implementation detail not meant to be used outside of ROM

@example

class User
  include Options

  option :name, type: String, reader: true
  option :admin, allow: [true, false], reader: true, default: false

  def initialize(options={})
    super
  end
end

user = User.new(name: 'Piotr')
user.name # => "Piotr"
user.admin # => false

@api public

Constants

InvalidOptionKeyError
InvalidOptionValueError

Attributes

options[R]

@return [Hash<Option>] Option definitions

@api public

Public Class Methods

included(klass) click to toggle source
# File lib/rom/support/options.rb, line 36
def self.included(klass)
  klass.extend ClassMethods
  klass.option_definitions = Definitions.new
end
new(*args) click to toggle source

Initialize options provided as optional last argument hash

@example

class Commands
  include Options

  # ...

  def initialize(relations, options={})
    @relation = relation
    super
  end
end

@param [Array] args

# File lib/rom/support/options.rb, line 191
def initialize(*args)
  options = args.last ? args.last.dup : {}
  self.class.option_definitions.process(self, options)
  @options = options.freeze
end