Module: PgEventstore::Extensions::OptionsExtension

Overview

A very simple extension that implements a DSL for adding attr_accessors with default values, and assigning their values during object initialization. Example. Let's say you frequently do something like this:

class SomeClass
attr_accessor :attr1, :attr2, :attr3, :attr4

def initialize(opts = {})
  @attr1 = opts[:attr1] || 'Attr 1 value'
  @attr2 = opts[:attr2] || 'Attr 2 value'
  @attr3 = opts[:attr3] || do_some_calc
  @attr4 = opts[:attr4]
end

def do_some_calc
  "Some calculations"
end
end

SomeClass.new(attr1: 'hihi', attr4: 'byebye')

You can replace the code above using the OptionsExtension:

class SomeClass
include PgEventstore::Extensions::OptionsExtension

option(:attr1) { 'Attr 1 value' }
option(:attr2) { 'Attr 2 value' }
option(:attr3) { do_some_calc }
option(:attr4)

def do_some_calc
  "Some calculations"
end
end

SomeClass.new(attr1: 'hihi', attr4: 'byebye')

Defined Under Namespace

Classes: Option, Options, ReadonlyAttributeError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Parameters:

  • klass (Object)

Returns:

  • (Object)


161
162
163
164
165
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 161

def self.included(klass)
  klass.singleton_class.attr_accessor(:options)
  klass.options = Options.new.freeze
  klass.extend(ClassMethods)
end

Instance Method Details

#init_default_values(options) ⇒ void

This method returns an undefined value.

@param options

Parameters:

  • options (::Hash[untyped, untyped])


210
211
212
213
214
215
216
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 210

def init_default_values(options)
  self.class.options.each do |option|
    # init default values of options
    value = options.key?(option.name) ? options[option.name] : public_send(option.name)
    public_send("#{option.name}=", value)
  end
end

#initialize(**options) ⇒ OptionsExtension

Returns a new instance of OptionsExtension.

Parameters:

  • options (Object)

Returns:



167
168
169
170
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 167

def initialize(**options)
  @readonly = Set.new
  init_default_values(options)
end

#options_hash::Hash[untyped, untyped] Also known as: attributes_hash

Construct a hash from options, where key is the option's name and the value is option's value

Returns:

  • (::Hash[untyped, untyped])


175
176
177
178
179
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 175

def options_hash
  self.class.options.to_h do |option|
    [option.name, public_send(option.name)]
  end
end

#readonly!(opt_name) ⇒ Boolean

@param opt_name

Parameters:

  • opt_name (Symbol)

Returns:

  • (Boolean)


184
185
186
187
188
189
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 184

def readonly!(opt_name)
  return false unless self.class.options.include?(Option.new(opt_name))

  @readonly.add(opt_name)
  true
end

#readonly?(opt_name) ⇒ Boolean

@param opt_name

Parameters:

  • opt_name (Symbol)

Returns:

  • (Boolean)


193
194
195
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 193

def readonly?(opt_name)
  @readonly.include?(opt_name)
end

#readonly_error(opt_name) ⇒ void

This method returns an undefined value.

@param opt_name

Parameters:

  • opt_name (Symbol)


202
203
204
205
206
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 202

def readonly_error(opt_name)
  raise(
    ReadonlyAttributeError, "#{opt_name.inspect} attribute was marked as read only. You can no longer modify it."
  )
end