Class: LIVR::Rules::String::Like

Inherits:
LIVR::Rule show all
Defined in:
lib/livr/rules/string.rb

Instance Method Summary collapse

Constructor Details

#initialize(re_str, flags = nil) ⇒ Like

Returns a new instance of Like.



126
127
128
129
# File 'lib/livr/rules/string.rb', line 126

def initialize(re_str, flags=nil)
  @re_str = re_str
  @flags = flags
end

Instance Method Details

#call(value, user_data, field_results) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/livr/rules/string.rb', line 131

def call(value, user_data, field_results)
  return if value.in? [nil, ""]
  return 'FORMAT_ERROR' unless is_primitive(value)

  value = value.to_s
  regexp = Regexp.compile(@re_str, @flags)

  unless regexp.match(value)
    return 'WRONG_FORMAT'
  end

  field_results << value

  return
end