Module: Iry::Macros
- Defined in:
- lib/iry/macros.rb
Overview
Class-level methods available to classes executing ‘include Iry`
Instance Method Summary collapse
-
#check_constraint(key, name: nil, message: :invalid) ⇒ void
Tracks check constraint for the given key and convert constraint errors into validation errors.
-
#constraints ⇒ {String => Constraint}
Constraints by name.
-
#exclusion_constraint(key, name: nil, message: :taken) ⇒ void
Tracks exclusion constraint for the given key and convert constraint errors into validation errors.
-
#foreign_key_constraint(key_or_keys, name: nil, message: :required, error_key: nil) ⇒ void
Tracks foreign key constraint for the given key (or keys) and convert constraint errors into validation errors.
-
#unique_constraint(key_or_keys, name: nil, message: :taken, error_key: nil) ⇒ void
Tracks uniqueness constraint for the given key (or keys) and convert constraint errors into validation errors.
Instance Method Details
#check_constraint(key, name: nil, message: :invalid) ⇒ void
This method returns an undefined value.
Tracks check constraint for the given key and convert constraint errors into validation errors
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/iry/macros.rb', line 14 def check_constraint( key, name: nil, message: :invalid ) name ||= Constraint::Check.infer_name(key, table_name) if constraints.key?(name) raise ArgumentError, "Constraint already exists" end self.constraints = constraints.dup constraints[name] = Constraint::Check.new( key, message: , name: name ) end |
#exclusion_constraint(key, name: nil, message: :taken) ⇒ void
This method returns an undefined value.
Tracks exclusion constraint for the given key and convert constraint errors into validation errors
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/iry/macros.rb', line 39 def exclusion_constraint( key, name: nil, message: :taken ) name ||= Constraint::Exclusion.infer_name(key, table_name) if constraints.key?(name) raise ArgumentError, "Constraint already exists" end self.constraints = constraints.dup constraints[name] = Constraint::Exclusion.new( key, message: , name: name ) end |
#foreign_key_constraint(key_or_keys, name: nil, message: :required, error_key: nil) ⇒ void
This method returns an undefined value.
Tracks foreign key constraint for the given key (or keys) and convert constraint errors into validation errors
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/iry/macros.rb', line 66 def foreign_key_constraint( key_or_keys, name: nil, message: :required, error_key: nil ) keys = Array(key_or_keys) name ||= Constraint::ForeignKey.infer_name(keys, table_name) error_key ||= keys.first if constraints.key?(name) raise ArgumentError, "Constraint already exists" end self.constraints = constraints.dup constraints[name] = Constraint::ForeignKey.new( keys, message: , name: name, error_key: error_key ) end |
#unique_constraint(key_or_keys, name: nil, message: :taken, error_key: nil) ⇒ void
This method returns an undefined value.
Tracks uniqueness constraint for the given key (or keys) and convert constraint errors into validation errors
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/iry/macros.rb', line 97 def unique_constraint( key_or_keys, name: nil, message: :taken, error_key: nil ) keys = Array(key_or_keys) name ||= Constraint::Unique.infer_name(keys, table_name) error_key ||= keys.first if constraints.key?(name) raise ArgumentError, "Constraint already exists" end self.constraints = constraints.dup constraints[name] = Constraint::Unique.new( keys, message: , name: name, error_key: error_key ) end |