Class: ForeignKeyValidation::Collector
- Inherits:
-
Object
- Object
- ForeignKeyValidation::Collector
- Defined in:
- lib/foreign_key_validation/collector.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #check! ⇒ Object
-
#initialize(opt = {}) ⇒ Collector
constructor
A new instance of Collector.
- #klass ⇒ Object
- #reflection_names ⇒ Object
- #reflections ⇒ Object
- #validate_against ⇒ Object
- #validate_against_key ⇒ Object
- #validate_with ⇒ Object
Constructor Details
#initialize(opt = {}) ⇒ Collector
Returns a new instance of Collector.
6 7 8 |
# File 'lib/foreign_key_validation/collector.rb', line 6 def initialize(opt={}) self. = opt end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/foreign_key_validation/collector.rb', line 4 def @options end |
Instance Method Details
#check! ⇒ Object
10 11 12 13 14 15 |
# File 'lib/foreign_key_validation/collector.rb', line 10 def check! raise Errors::NoReleationFoundError.new(klass.name) if reflection_names.empty? raise Errors::NoForeignKeyFoundError.new(validate_against, klass.table_name) unless reflection_names.include?(validate_against) raise Errors::UnknownRelationError.new(validate_with) unless validate_with.all? {|k| reflection_names.include?(k)} true end |
#klass ⇒ Object
17 18 19 |
# File 'lib/foreign_key_validation/collector.rb', line 17 def klass @klass ||= [:klass] end |
#reflection_names ⇒ Object
33 34 35 |
# File 'lib/foreign_key_validation/collector.rb', line 33 def reflection_names @reflection_names ||= reflections.map {|r| r.name.to_s } end |
#reflections ⇒ Object
29 30 31 |
# File 'lib/foreign_key_validation/collector.rb', line 29 def reflections @reflections ||= klass.reflect_on_all_associations(:belongs_to) end |
#validate_against ⇒ Object
21 22 23 |
# File 'lib/foreign_key_validation/collector.rb', line 21 def validate_against @validate_against ||= ([:on] || ForeignKeyValidation.configuration.validate_against).to_s end |
#validate_against_key ⇒ Object
37 38 39 |
# File 'lib/foreign_key_validation/collector.rb', line 37 def validate_against_key @validate_against_key ||= reflections.select {|r| r.name.to_s == validate_against}.first.try(:foreign_key) end |
#validate_with ⇒ Object
25 26 27 |
# File 'lib/foreign_key_validation/collector.rb', line 25 def validate_with @validate_with ||= ((Array([:with]).map(&:to_s) if [:with]) || reflection_names).reject {|n| n == validate_against} end |