module Fend::Plugins::ValueHelpers::ParamMethods
Public Instance Methods
blank?()
click to toggle source
Returns `true` when:
-
`value.empty?`
-
`value.nil?`
-
`value == false`
-
`value.empty_string?`
# File lib/fend/plugins/value_helpers.rb, line 29 def blank? case value when Array, Hash value.empty? when NilClass, FalseClass true when Integer, Float, Numeric, Time, TrueClass, Symbol false when String empty_string? else value.respond_to?(:empty?) ? !!value.empty? : !value end end
dig(*path)
click to toggle source
Enables easier fetching of nested data values. Works with hashes and arrays.
validate do |i| # { user: { address: { city: "Amsterdam" } } } i.dig(:user, :address, :city) #=> "Amsterdam" i.dig(:user, :profile, :username) #=> nil # { tags: [ { id: 2, name: "JS" }, { id: 3, name: "Ruby" }] } i.dig(:tags, 1, :name) #=> "Ruby" i.dig(:tags, 5, :id) #=> nil i.param(:accounts) do |accounts| accounts.dig(0, :transactions, 3) #=> "$100.00" end end
# File lib/fend/plugins/value_helpers.rb, line 60 def dig(*path) result = value path.each do |point| break if result.is_a?(Array) && !point.is_a?(Integer) result = result.is_a?(Enumerable) ? result[point] : nil break if result.nil? end result end
empty_string?()
click to toggle source
Returns `true` when value is an empty string (space, tab, newline, carriage_return, etc…)
# email.value #=> "" # email.value #=> " " # email.value #=> "\n" # email.value #=> "\r" # email.value #=> "\t" # email.value #=> "\n\r\t" email.empty_string? #=> true
# File lib/fend/plugins/value_helpers.rb, line 85 def empty_string? return false unless value.is_a?(String) || value.is_a?(Symbol) regex = /\A[[:space:]]*\z/ !regex.match(value).nil? end
present?()
click to toggle source
Returns `true` if value is present/not blank
# File lib/fend/plugins/value_helpers.rb, line 94 def present? !blank? end
type_of?(type_ref)
click to toggle source
Returns `true` if value is of specified type. Accepts constants, their string representations and symbols:
email.type_of?(String) # or email.type_of?("string") # or email.type_of?(:string)
Additional examples:
# these are all checking the same thing user.type_of?(AdminUser) user.type_of?(:admin_user) user.type_of?("admin_user")
Provides a convenient way for checking if value is boolean, decimal or nil:
# true if value is TrueClass or FalseClass confirmed.type_of?(:boolean) # true if value is Float or BigDecimal amount.type_of?(:decimal) # true if value is nil/NilClass email.type_of?(:nil)
# File lib/fend/plugins/value_helpers.rb, line 129 def type_of?(type_ref) return value.is_a?(type_ref) unless type_ref.is_a?(String) || type_ref.is_a?(Symbol) case type_ref.to_s when "boolean" then !!value == value when "decimal" then value.is_a?(Float) || value.is_a?(BigDecimal) when "nil" then value.is_a?(NilClass) else camelized_type_ref = type_ref.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:\A|_)(.)/) { $1.upcase } type_class = Object.const_get(camelized_type_ref) value.is_a?(type_class) end end