Module: Validator::Matchers::ValidatorSpecHelper
- Defined in:
- lib/validator/matchers/validator_spec_helper.rb
Overview
TODO: Document and cleanup.
Constant Summary collapse
- DEFAULT_ATTRIBUTE_NAME =
:value
Class Method Summary collapse
-
.included(base) ⇒ Object
Auto extend validator specs when included.
Class Method Details
.included(base) ⇒ Object
Auto extend validator specs when included
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 |
# File 'lib/validator/matchers/validator_spec_helper.rb', line 16 def self.included(base) base.instance_eval do let(:validator_name) do RSpec.current_example.full_description.match(/\A[\w:]+Validator/)[0] end let(:validator_class) { Object.const_get(validator_name) } let(:validator_type) do if validator_class.ancestors.include? ActiveModel::EachValidator ActiveModel::EachValidator else ActiveModel::Validator end end let(:validation_name) do validator_name.underscore.gsub(/_validator\Z/, '') end let(:attribute_names) { [DEFAULT_ATTRIBUTE_NAME] } let(DEFAULT_ATTRIBUTE_NAME) { nil } let(:options) { nil } # This smells like it can be extracted into two objects. let(:model_class) do example_group = self Struct.new(*attribute_names) do include ActiveModel::Validations def self.name 'ValidatorModelMock' end if example_group.validator_type == ActiveModel::EachValidator args = { example_group.validation_name.to_sym => ( example_group. || true ) } validates DEFAULT_ATTRIBUTE_NAME, args else if example_group..nil? validates_with example_group.validator_class else validates_with example_group.validator_class, example_group. end end end end let(:validator_model_mock) do attributes = attribute_names.map { |name| eval("#{name}") } model_class.new(*attributes) end end end |