Module: ROM::Options

Included in:
ClassBuilder
Defined in:
lib/rom/support/options.rb

Overview

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

Examples:

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

Defined Under Namespace

Modules: ClassMethods, Transformers Classes: Definitions, Option

Constant Summary collapse

InvalidOptionValueError =
Class.new(StandardError)
InvalidOptionKeyError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsHash<Option> (readonly)

Returns Option definitions.

Returns:

  • (Hash<Option>)

    Option definitions



34
35
36
# File 'lib/rom/support/options.rb', line 34

def options
  @options
end

Class Method Details

.included(klass) ⇒ Object



36
37
38
39
# File 'lib/rom/support/options.rb', line 36

def self.included(klass)
  klass.extend ClassMethods
  klass.option_definitions = Definitions.new
end

Instance Method Details

#initialize(*args) ⇒ Object

Initialize options provided as optional last argument hash

Examples:

class Commands
  include Options

  # ...

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

Parameters:

  • args (Array)


191
192
193
194
195
# 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