Module: Fend::Plugins::ValueHelpers::ParamMethods
- Defined in:
- lib/fend/plugins/value_helpers.rb
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Returns ‘true` when:.
-
#dig(*path) ⇒ Object
Enables easier fetching of nested data values.
-
#empty_string? ⇒ Boolean
Returns ‘true` when value is an empty string (space, tab, newline, carriage_return, etc…).
-
#present? ⇒ Boolean
Returns ‘true` if value is present/not blank.
-
#type_of?(type_ref) ⇒ Boolean
Returns ‘true` if value is of specified type.
Instance Method Details
#blank? ⇒ Boolean
Returns ‘true` when:
-
‘value.empty?`
-
‘value.nil?`
-
‘value == false`
-
‘value.empty_string?`
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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) ⇒ Object
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
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# 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? ⇒ Boolean
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
85 86 87 88 89 90 91 |
# 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? ⇒ Boolean
Returns ‘true` if value is present/not blank
94 95 96 |
# File 'lib/fend/plugins/value_helpers.rb', line 94 def present? !blank? end |
#type_of?(type_ref) ⇒ Boolean
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)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# 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 |