Module: PgEventstore::Extensions::OptionsExtension
- Included in:
- BasicConfig, Chunks::SubscriptionCheckpointChunk::Checkpoint, Chunks::SubscriptionEventsIndexChunk::RawEventWithSubscriptionPosition, Commands::RevisionCheck::CurrentRevision::EventTypeRevisions, Commands::RevisionCheck::CurrentRevision::StreamRevision, Commands::RevisionCheck::ExpectedRevision::EventTypeRevision, Commands::RevisionCheck::ExpectedRevision::EventTypeRevisionWithMarkers, Commands::RevisionCheck::ExpectedRevision::EventTypeRevisions, Commands::RevisionCheck::ExpectedRevision::MarkersRevision, Commands::RevisionCheck::ExpectedRevision::StreamRevision, PgEventstore::Event, PgEventstore::EventGlobalIndex::ReadApiRepr, PgEventstore::EventGlobalIndex::RevisionCheckRepr, PgEventstore::EventGlobalIndex::SubscriptionRepr, PgEventstore::EventGlobalIndex::WriteApiRepr, PgEventstore::EventsSubscriptionPositionWorker::ReindexTime, FeatureMarker, Partition, QueryBuilders::ReadCursor::StreamCursor::AllStreamCursor, QueryBuilders::ReadCursor::StreamCursor::RegularStreamCursor, Subscription, SubscriptionsSet, WrongExpectedTypesRevisionError::Verdict
- Defined in:
- lib/pg_eventstore/extensions/options_extension.rb,
sig/pg_eventstore/extensions/options_extension.rbs
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
-
#init_default_values(options) ⇒ void
@param
options. -
#initialize(**options) ⇒ OptionsExtension
A new instance of OptionsExtension.
-
#options_hash ⇒ ::Hash[untyped, untyped]
(also: #attributes_hash)
Construct a hash from options, where key is the option's name and the value is option's value.
-
#readonly!(opt_name) ⇒ Boolean
@param
opt_name. -
#readonly?(opt_name) ⇒ Boolean
@param
opt_name. -
#readonly_error(opt_name) ⇒ void
@param
opt_name.
Class Method Details
Instance Method Details
#init_default_values(options) ⇒ void
This method returns an undefined value.
@param options
210 211 212 213 214 215 216 |
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 210 def init_default_values() self.class..each do |option| # init default values of options value = .key?(option.name) ? [option.name] : public_send(option.name) public_send("#{option.name}=", value) end end |
#initialize(**options) ⇒ OptionsExtension
Returns a new instance of OptionsExtension.
167 168 169 170 |
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 167 def initialize(**) @readonly = Set.new init_default_values() 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
175 176 177 178 179 |
# File 'lib/pg_eventstore/extensions/options_extension.rb', line 175 def self.class..to_h do |option| [option.name, public_send(option.name)] end end |
#readonly!(opt_name) ⇒ Boolean
@param opt_name
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..include?(Option.new(opt_name)) @readonly.add(opt_name) true end |
#readonly?(opt_name) ⇒ Boolean
@param opt_name
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
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 |