Class: Fend::Plugins::Coercions::Coerce

Inherits:
Object
  • Object
show all
Defined in:
lib/fend/plugins/coercions.rb

Constant Summary collapse

STRICT_PREFIX =
"strict_".freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.fend_classObject

Returns the value of attribute fend_class.



300
301
302
# File 'lib/fend/plugins/coercions.rb', line 300

def fend_class
  @fend_class
end

Class Method Details

.coerce_to(type, &block) ⇒ Object



303
304
305
306
307
308
309
# File 'lib/fend/plugins/coercions.rb', line 303

def self.coerce_to(type, &block)
  method_name = "to_#{type}"

  define_method(method_name, &block)

  private method_name
end

.to(type, value) ⇒ Object



311
312
313
# File 'lib/fend/plugins/coercions.rb', line 311

def self.to(type, value)
  new.to(type, value)
end

Instance Method Details

#to(type, value, opts = {}) ⇒ Object



315
316
317
318
319
320
321
322
323
# File 'lib/fend/plugins/coercions.rb', line 315

def to(type, value, opts = {})
  type = type.to_s.sub(STRICT_PREFIX, "") if is_strict = type.to_s.start_with?(STRICT_PREFIX)

  begin
    method("to_#{type}").call(value)
  rescue ArgumentError, TypeError
   is_strict ? raise_error(value, type) : value
  end
end