module Wallaby::FieldUtils

Field utils

Public Class Methods

first_field_by(*conditions, fields) click to toggle source

Find the first field that meets the first condition. @example to find the possible text field

Wallaby::FieldUtils.first_field_by({ name: /name|title/ }, { type: 'string' }, fields)
# => if any field name that has `name` or `title`, return this field
# => otherwise, find the first field that has type `string`

@param conditions [Array<Hash>] @param fields [Hash] field metadata @return [String, Symbol] field name

# File lib/utils/wallaby/field_utils.rb, line 15
def first_field_by(*conditions, fields)
  return if [conditions, fields].any?(&:blank?)

  conditions.each do |condition|
    fields.each do |field_name, metadata|
      return field_name if meet? field_name, metadata.with_indifferent_access, condition
    end
  end
  nil
end

Protected Class Methods

meet?(field_name, metadata, condition) click to toggle source

@param field_name [String] @param metadata [Hash] @param condition [Hash] @return [true] if field's metadata meets the condition @return [true] otherwise

# File lib/utils/wallaby/field_utils.rb, line 33
def meet?(field_name, metadata, condition)
  condition.all? do |key, requirement|
    operator = requirement.is_a?(::Regexp) ? '=~' : '=='
    value = metadata[key]
    value ||= field_name.to_s if key.to_sym == :name
    ModuleUtils.try_to value, operator, requirement
  end
end