class Fend::Plugins::Coercions::Coerce
Constants
- STRICT_PREFIX
Attributes
fend_class[RW]
Public Class Methods
coerce_to(type, &block)
click to toggle source
# 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)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 311 def self.to(type, value) new.to(type, value) end
Public Instance Methods
to(type, value, opts = {})
click to toggle source
# 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
Private Instance Methods
empty_string?(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 428 def empty_string?(input) return false unless input.is_a?(String) || input.is_a?(Symbol) !(/\A[[:space:]]*\z/.match(input).nil?) end
fend_class()
click to toggle source
# File lib/fend/plugins/coercions.rb, line 434 def fend_class self.class.fend_class end
raise_error(input, type)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 421 def raise_error(input, type) message = fend_class.opts[:coercions_strict_error_message] || "cannot coerce #{input.inspect} to #{type}" message = message.is_a?(String) ? message : message.call(input, type) raise CoercionError, message end
to_any(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 327 def to_any(input) return if empty_string?(input) input end
to_array(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 405 def to_array(input) return [] if empty_string?(input) return input if input.is_a?(Array) raise ArgumentError end
to_boolean(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 394 def to_boolean(input) return if empty_string?(input) case input when true, 1, /\A(?:1|t(?:rue)?|y(?:es)?|on)\z/i then true when false, 0, /\A(?:0|f(?:alse)?|no?|off)\z/i then false else raise ArgumentError end end
to_date(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 370 def to_date(input) return if empty_string?(input) raise ArgumentError unless input.respond_to?(:to_str) ::Date.parse(input) end
to_date_time(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 378 def to_date_time(input) return if empty_string?(input) raise ArgumentError unless input.respond_to?(:to_str) ::DateTime.parse(input) end
to_decimal(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 364 def to_decimal(input) return if empty_string?(input) to_float(input).to_d end
to_float(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 358 def to_float(input) return if empty_string?(input) ::Kernel.Float(input) end
to_hash(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 412 def to_hash(input) return {} if empty_string?(input) return input if input.is_a?(Hash) raise ArgumentError end
to_integer(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 352 def to_integer(input) return if empty_string?(input) ::Kernel.Integer(input) end
to_string(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 333 def to_string(input) return if empty_string?(input) || input.nil? case input when String then input when Numeric, Symbol then input.to_s else raise ArgumentError end end
to_symbol(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 344 def to_symbol(input) return if empty_string?(input) || input.nil? return input.to_sym if input.respond_to?(:to_sym) raise ArgumentError end
to_time(input)
click to toggle source
# File lib/fend/plugins/coercions.rb, line 386 def to_time(input) return if empty_string?(input) raise ArgumentError unless input.respond_to?(:to_str) ::Time.parse(input) end