Class: LogStash::Setting::Coercible

Inherits:
LogStash::Setting show all
Defined in:
lib/logstash/settings.rb

Instance Attribute Summary

Attributes inherited from LogStash::Setting

#default, #name

Instance Method Summary collapse

Methods inherited from LogStash::Setting

#==, #reset, #set?, #strict?, #to_hash, #validate_value, #value

Constructor Details

#initialize(name, klass, default = nil, strict = true, &validator_proc) ⇒ Coercible

Returns a new instance of Coercible.



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/logstash/settings.rb', line 286

def initialize(name, klass, default=nil, strict=true, &validator_proc)
  @name = name
  unless klass.is_a?(Class)
    raise ArgumentError.new("Setting \"#{@name}\" must be initialized with a class (received #{klass})")
  end
  @klass = klass
  @validator_proc = validator_proc
  @value = nil
  @value_is_set = false

  if strict
    coerced_default = coerce(default)
    validate(coerced_default)
    @default = coerced_default
  else
    @default = default
  end
end

Instance Method Details

#coerce(value) ⇒ Object

Raises:

  • (NotImplementedError)


313
314
315
# File 'lib/logstash/settings.rb', line 313

def coerce(value)
  raise NotImplementedError.new("Please implement #coerce for #{self.class}")
end

#set(value) ⇒ Object



305
306
307
308
309
310
311
# File 'lib/logstash/settings.rb', line 305

def set(value)
  coerced_value = coerce(value)
  validate(coerced_value)
  @value = coerce(coerced_value)
  @value_is_set = true
  @value
end