Class: ActiveRecord::Embedded::Field::Regexp

Inherits:
ActiveRecord::Embedded::Field show all
Defined in:
lib/active_record/embedded/field/regexp.rb

Overview

Store a regular expression pattern as an object field in the database.

Constant Summary

Constants inherited from ActiveRecord::Embedded::Field

PREFIX

Instance Attribute Summary

Attributes inherited from ActiveRecord::Embedded::Field

#default, #name

Instance Method Summary collapse

Methods inherited from ActiveRecord::Embedded::Field

#default?, #default_method_name, find, #initialize, types

Constructor Details

This class inherits a constructor from ActiveRecord::Embedded::Field

Instance Method Details

#cast(value) ⇒ Object


9
10
11
12
13
14
15
16
# File 'lib/active_record/embedded/field/regexp.rb', line 9

def cast(value)
  return value if value.is_a? ::Hash

  {
    '$pattern' => value.source,
    '$options' => value.options
  }
end

#coerce(value) ⇒ Object

read from the database


19
20
21
22
23
24
25
26
# File 'lib/active_record/embedded/field/regexp.rb', line 19

def coerce(value)
  return value if value.is_a? ::Regexp

  pattern = value['$pattern']
  options = value['$options']

  ::Regexp.new(pattern, options)
end