Class: ActsAsTable::ForeignKeyMap

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/acts_as_table/foreign_key_map.rb

Overview

ActsAsTable foreign key map.

Instance Attribute Summary collapse

Belongs to collapse

Instance Method Summary collapse

Instance Attribute Details

#extendedBoolean

Returns true if the source value for this ActsAsTable foreign key map is an extended regular expression. Otherwise, returns false.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/acts_as_table/foreign_key_map.rb', line 34

class ForeignKeyMap < ::ActiveRecord::Base
  # @!parse
  #   include ActsAsTable::ValueProvider
  #   include ActsAsTable::ValueProviderAssociationMethods

  self.table_name = ActsAsTable.foreign_key_maps_table

  # Returns the ActsAsTable foreign key for this map.
  belongs_to :foreign_key, **{
    class_name: 'ActsAsTable::ForeignKey',
    inverse_of: :foreign_key_maps,
    required: true,
  }

  # validates :extended, **{}

  # validates :ignore_case, **{}

  # validates :multiline, **{}

  validates :position, **{
    numericality: {
      greater_than_or_equal_to: 1,
      only_integer: true,
    },
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  # validates :regexp, **{}

  validates :source_value, **{
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  validates :target_value, **{
    presence: true,
  }

  validate :source_value_as_regexp_must_be_valid, **{
    if: ::Proc.new { |foreign_key_map| foreign_key_map.source_value.present? },
  }

  # Returns the source value for this ActsAsTable foreign key map as a regular expression.
  #
  # @return [Regexp]
  # @raise [RegexpError]
  def source_value_as_regexp
    # @return [String]
    pattern = self.source_value.to_s

    unless self.regexp?
      pattern = "\\A#{::Regexp.quote(pattern)}\\z"
    end

    # @return [Integer]
    flags = {
      :extended => :EXTENDED,
      :ignore_case => :IGNORECASE,
      :multiline => :MULTILINE,
    }.each_pair.inject(0) { |acc, pair|
      method_name, const_name = *pair

      self.send(:"#{method_name}?") ? (acc | ::Regexp.const_get(const_name, false)) : acc
    }

    ::Regexp.new(pattern, flags)
  end

  private

  # @return [void]
  def source_value_as_regexp_must_be_valid
    begin
      self.source_value_as_regexp
    rescue ::RegexpError
      self.errors.add('source_value', :invalid)
    end

    return
  end
end

#ignore_caseBoolean

Returns true if the source value for this ActsAsTable foreign key map is a regular expression that ignores character case. Otherwise, returns false.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/acts_as_table/foreign_key_map.rb', line 34

class ForeignKeyMap < ::ActiveRecord::Base
  # @!parse
  #   include ActsAsTable::ValueProvider
  #   include ActsAsTable::ValueProviderAssociationMethods

  self.table_name = ActsAsTable.foreign_key_maps_table

  # Returns the ActsAsTable foreign key for this map.
  belongs_to :foreign_key, **{
    class_name: 'ActsAsTable::ForeignKey',
    inverse_of: :foreign_key_maps,
    required: true,
  }

  # validates :extended, **{}

  # validates :ignore_case, **{}

  # validates :multiline, **{}

  validates :position, **{
    numericality: {
      greater_than_or_equal_to: 1,
      only_integer: true,
    },
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  # validates :regexp, **{}

  validates :source_value, **{
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  validates :target_value, **{
    presence: true,
  }

  validate :source_value_as_regexp_must_be_valid, **{
    if: ::Proc.new { |foreign_key_map| foreign_key_map.source_value.present? },
  }

  # Returns the source value for this ActsAsTable foreign key map as a regular expression.
  #
  # @return [Regexp]
  # @raise [RegexpError]
  def source_value_as_regexp
    # @return [String]
    pattern = self.source_value.to_s

    unless self.regexp?
      pattern = "\\A#{::Regexp.quote(pattern)}\\z"
    end

    # @return [Integer]
    flags = {
      :extended => :EXTENDED,
      :ignore_case => :IGNORECASE,
      :multiline => :MULTILINE,
    }.each_pair.inject(0) { |acc, pair|
      method_name, const_name = *pair

      self.send(:"#{method_name}?") ? (acc | ::Regexp.const_get(const_name, false)) : acc
    }

    ::Regexp.new(pattern, flags)
  end

  private

  # @return [void]
  def source_value_as_regexp_must_be_valid
    begin
      self.source_value_as_regexp
    rescue ::RegexpError
      self.errors.add('source_value', :invalid)
    end

    return
  end
end

#multilineBoolean

Returns true if the source value for this ActsAsTable foreign key map is a multiline regular expression. Otherwise, returns false.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/acts_as_table/foreign_key_map.rb', line 34

class ForeignKeyMap < ::ActiveRecord::Base
  # @!parse
  #   include ActsAsTable::ValueProvider
  #   include ActsAsTable::ValueProviderAssociationMethods

  self.table_name = ActsAsTable.foreign_key_maps_table

  # Returns the ActsAsTable foreign key for this map.
  belongs_to :foreign_key, **{
    class_name: 'ActsAsTable::ForeignKey',
    inverse_of: :foreign_key_maps,
    required: true,
  }

  # validates :extended, **{}

  # validates :ignore_case, **{}

  # validates :multiline, **{}

  validates :position, **{
    numericality: {
      greater_than_or_equal_to: 1,
      only_integer: true,
    },
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  # validates :regexp, **{}

  validates :source_value, **{
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  validates :target_value, **{
    presence: true,
  }

  validate :source_value_as_regexp_must_be_valid, **{
    if: ::Proc.new { |foreign_key_map| foreign_key_map.source_value.present? },
  }

  # Returns the source value for this ActsAsTable foreign key map as a regular expression.
  #
  # @return [Regexp]
  # @raise [RegexpError]
  def source_value_as_regexp
    # @return [String]
    pattern = self.source_value.to_s

    unless self.regexp?
      pattern = "\\A#{::Regexp.quote(pattern)}\\z"
    end

    # @return [Integer]
    flags = {
      :extended => :EXTENDED,
      :ignore_case => :IGNORECASE,
      :multiline => :MULTILINE,
    }.each_pair.inject(0) { |acc, pair|
      method_name, const_name = *pair

      self.send(:"#{method_name}?") ? (acc | ::Regexp.const_get(const_name, false)) : acc
    }

    ::Regexp.new(pattern, flags)
  end

  private

  # @return [void]
  def source_value_as_regexp_must_be_valid
    begin
      self.source_value_as_regexp
    rescue ::RegexpError
      self.errors.add('source_value', :invalid)
    end

    return
  end
end

#positionInteger

Returns the position of this ActsAsTable foreign key map.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/acts_as_table/foreign_key_map.rb', line 34

class ForeignKeyMap < ::ActiveRecord::Base
  # @!parse
  #   include ActsAsTable::ValueProvider
  #   include ActsAsTable::ValueProviderAssociationMethods

  self.table_name = ActsAsTable.foreign_key_maps_table

  # Returns the ActsAsTable foreign key for this map.
  belongs_to :foreign_key, **{
    class_name: 'ActsAsTable::ForeignKey',
    inverse_of: :foreign_key_maps,
    required: true,
  }

  # validates :extended, **{}

  # validates :ignore_case, **{}

  # validates :multiline, **{}

  validates :position, **{
    numericality: {
      greater_than_or_equal_to: 1,
      only_integer: true,
    },
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  # validates :regexp, **{}

  validates :source_value, **{
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  validates :target_value, **{
    presence: true,
  }

  validate :source_value_as_regexp_must_be_valid, **{
    if: ::Proc.new { |foreign_key_map| foreign_key_map.source_value.present? },
  }

  # Returns the source value for this ActsAsTable foreign key map as a regular expression.
  #
  # @return [Regexp]
  # @raise [RegexpError]
  def source_value_as_regexp
    # @return [String]
    pattern = self.source_value.to_s

    unless self.regexp?
      pattern = "\\A#{::Regexp.quote(pattern)}\\z"
    end

    # @return [Integer]
    flags = {
      :extended => :EXTENDED,
      :ignore_case => :IGNORECASE,
      :multiline => :MULTILINE,
    }.each_pair.inject(0) { |acc, pair|
      method_name, const_name = *pair

      self.send(:"#{method_name}?") ? (acc | ::Regexp.const_get(const_name, false)) : acc
    }

    ::Regexp.new(pattern, flags)
  end

  private

  # @return [void]
  def source_value_as_regexp_must_be_valid
    begin
      self.source_value_as_regexp
    rescue ::RegexpError
      self.errors.add('source_value', :invalid)
    end

    return
  end
end

#regexpBoolean

Returns true if the source value for this ActsAsTable foreign key map is a regular expression. Otherwise, returns false.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/acts_as_table/foreign_key_map.rb', line 34

class ForeignKeyMap < ::ActiveRecord::Base
  # @!parse
  #   include ActsAsTable::ValueProvider
  #   include ActsAsTable::ValueProviderAssociationMethods

  self.table_name = ActsAsTable.foreign_key_maps_table

  # Returns the ActsAsTable foreign key for this map.
  belongs_to :foreign_key, **{
    class_name: 'ActsAsTable::ForeignKey',
    inverse_of: :foreign_key_maps,
    required: true,
  }

  # validates :extended, **{}

  # validates :ignore_case, **{}

  # validates :multiline, **{}

  validates :position, **{
    numericality: {
      greater_than_or_equal_to: 1,
      only_integer: true,
    },
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  # validates :regexp, **{}

  validates :source_value, **{
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  validates :target_value, **{
    presence: true,
  }

  validate :source_value_as_regexp_must_be_valid, **{
    if: ::Proc.new { |foreign_key_map| foreign_key_map.source_value.present? },
  }

  # Returns the source value for this ActsAsTable foreign key map as a regular expression.
  #
  # @return [Regexp]
  # @raise [RegexpError]
  def source_value_as_regexp
    # @return [String]
    pattern = self.source_value.to_s

    unless self.regexp?
      pattern = "\\A#{::Regexp.quote(pattern)}\\z"
    end

    # @return [Integer]
    flags = {
      :extended => :EXTENDED,
      :ignore_case => :IGNORECASE,
      :multiline => :MULTILINE,
    }.each_pair.inject(0) { |acc, pair|
      method_name, const_name = *pair

      self.send(:"#{method_name}?") ? (acc | ::Regexp.const_get(const_name, false)) : acc
    }

    ::Regexp.new(pattern, flags)
  end

  private

  # @return [void]
  def source_value_as_regexp_must_be_valid
    begin
      self.source_value_as_regexp
    rescue ::RegexpError
      self.errors.add('source_value', :invalid)
    end

    return
  end
end

#source_valueBoolean

Returns the source value for this ActsAsTable foreign key map.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/acts_as_table/foreign_key_map.rb', line 34

class ForeignKeyMap < ::ActiveRecord::Base
  # @!parse
  #   include ActsAsTable::ValueProvider
  #   include ActsAsTable::ValueProviderAssociationMethods

  self.table_name = ActsAsTable.foreign_key_maps_table

  # Returns the ActsAsTable foreign key for this map.
  belongs_to :foreign_key, **{
    class_name: 'ActsAsTable::ForeignKey',
    inverse_of: :foreign_key_maps,
    required: true,
  }

  # validates :extended, **{}

  # validates :ignore_case, **{}

  # validates :multiline, **{}

  validates :position, **{
    numericality: {
      greater_than_or_equal_to: 1,
      only_integer: true,
    },
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  # validates :regexp, **{}

  validates :source_value, **{
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  validates :target_value, **{
    presence: true,
  }

  validate :source_value_as_regexp_must_be_valid, **{
    if: ::Proc.new { |foreign_key_map| foreign_key_map.source_value.present? },
  }

  # Returns the source value for this ActsAsTable foreign key map as a regular expression.
  #
  # @return [Regexp]
  # @raise [RegexpError]
  def source_value_as_regexp
    # @return [String]
    pattern = self.source_value.to_s

    unless self.regexp?
      pattern = "\\A#{::Regexp.quote(pattern)}\\z"
    end

    # @return [Integer]
    flags = {
      :extended => :EXTENDED,
      :ignore_case => :IGNORECASE,
      :multiline => :MULTILINE,
    }.each_pair.inject(0) { |acc, pair|
      method_name, const_name = *pair

      self.send(:"#{method_name}?") ? (acc | ::Regexp.const_get(const_name, false)) : acc
    }

    ::Regexp.new(pattern, flags)
  end

  private

  # @return [void]
  def source_value_as_regexp_must_be_valid
    begin
      self.source_value_as_regexp
    rescue ::RegexpError
      self.errors.add('source_value', :invalid)
    end

    return
  end
end

#target_valueBoolean

Note:

If the source value for this ActsAsTable foreign key map is a regular expression, then the target value may reference any capture groups.

Returns the target value for this ActsAsTable foreign key map.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/acts_as_table/foreign_key_map.rb', line 34

class ForeignKeyMap < ::ActiveRecord::Base
  # @!parse
  #   include ActsAsTable::ValueProvider
  #   include ActsAsTable::ValueProviderAssociationMethods

  self.table_name = ActsAsTable.foreign_key_maps_table

  # Returns the ActsAsTable foreign key for this map.
  belongs_to :foreign_key, **{
    class_name: 'ActsAsTable::ForeignKey',
    inverse_of: :foreign_key_maps,
    required: true,
  }

  # validates :extended, **{}

  # validates :ignore_case, **{}

  # validates :multiline, **{}

  validates :position, **{
    numericality: {
      greater_than_or_equal_to: 1,
      only_integer: true,
    },
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  # validates :regexp, **{}

  validates :source_value, **{
    presence: true,
    uniqueness: {
      scope: ['foreign_key_id'],
    },
  }

  validates :target_value, **{
    presence: true,
  }

  validate :source_value_as_regexp_must_be_valid, **{
    if: ::Proc.new { |foreign_key_map| foreign_key_map.source_value.present? },
  }

  # Returns the source value for this ActsAsTable foreign key map as a regular expression.
  #
  # @return [Regexp]
  # @raise [RegexpError]
  def source_value_as_regexp
    # @return [String]
    pattern = self.source_value.to_s

    unless self.regexp?
      pattern = "\\A#{::Regexp.quote(pattern)}\\z"
    end

    # @return [Integer]
    flags = {
      :extended => :EXTENDED,
      :ignore_case => :IGNORECASE,
      :multiline => :MULTILINE,
    }.each_pair.inject(0) { |acc, pair|
      method_name, const_name = *pair

      self.send(:"#{method_name}?") ? (acc | ::Regexp.const_get(const_name, false)) : acc
    }

    ::Regexp.new(pattern, flags)
  end

  private

  # @return [void]
  def source_value_as_regexp_must_be_valid
    begin
      self.source_value_as_regexp
    rescue ::RegexpError
      self.errors.add('source_value', :invalid)
    end

    return
  end
end

Instance Method Details

#foreign_keyActsAsTable::ForeignKey

Returns the ActsAsTable foreign key for this map.



42
43
44
45
46
# File 'app/models/acts_as_table/foreign_key_map.rb', line 42

belongs_to :foreign_key, **{
  class_name: 'ActsAsTable::ForeignKey',
  inverse_of: :foreign_key_maps,
  required: true,
}

#source_value_as_regexpRegexp

Returns the source value for this ActsAsTable foreign key map as a regular expression.

Raises:

  • (RegexpError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/acts_as_table/foreign_key_map.rb', line 86

def source_value_as_regexp
  # @return [String]
  pattern = self.source_value.to_s

  unless self.regexp?
    pattern = "\\A#{::Regexp.quote(pattern)}\\z"
  end

  # @return [Integer]
  flags = {
    :extended => :EXTENDED,
    :ignore_case => :IGNORECASE,
    :multiline => :MULTILINE,
  }.each_pair.inject(0) { |acc, pair|
    method_name, const_name = *pair

    self.send(:"#{method_name}?") ? (acc | ::Regexp.const_get(const_name, false)) : acc
  }

  ::Regexp.new(pattern, flags)
end