Class: Specificator::Expectation::Validation

Inherits:
Object
  • Object
show all
Defined in:
lib/specificator/expectation/validation.rb

Constant Summary collapse

OPTIONS_MAPPING =
{
  absence: {
    on: :on,
    message: :with_message,
  },

  acceptance: {
    on: :on,
    message: :with_message,
  },

  confirmation: {
    on: :on,
    message: :with_message,
  },

  exclusion: {
    on: :on,
    message: :with_message,
  },

  inclusion: {
    on: :on,
    message: :with_message,
    # allow_nil: :allow_nil, Still some issues
    # allow_blank: :allow_blank, Still some issues
    in: {
      Range => :in_range,
      Array => :in_array
    }
  },

  length: {
    on: :on,
    minimum: :is_at_least,
    maximum: :is_at_most,
    in: :in,
    is: :is_equal_to,
    message: :with_message,
    too_short: :with_short_message,
    long_message: :with_long_message,
    # allow_nil: :allow_nil, Still some issues
  },

  numericality: {
    on: :on,
    only_integer: :only_integer,
    less_than: :is_less_than,
    less_than_or_equal_to: :is_less_than_or_equal_to,
    greater_than_or_equal_to: :is_greater_than_or_equal_to,
    equal_to: :is_equal_to,
    greater_than: :is_greater_than,
    even: :even,
    odd: :odd,
    allow_nil: :allow_nil
  },

  presence: {
    on: :on,
    message: :with_message
  },

  uniqueness: {
    on: :on,
    message: :with_message,
    scope: :scoped_to,
    # allow_nil: :allow_nil, Still some issues
    # allow_blank: :allow_blank, Still some issues
  }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(validator) ⇒ Validation

Returns a new instance of Validation.



81
82
83
# File 'lib/specificator/expectation/validation.rb', line 81

def initialize(validator)
  @validator = validator
end

Instance Attribute Details

#validatorObject

Returns the value of attribute validator.



75
76
77
# File 'lib/specificator/expectation/validation.rb', line 75

def validator
  @validator
end

Class Method Details

.for(validator) ⇒ Object



77
78
79
# File 'lib/specificator/expectation/validation.rb', line 77

def self.for(validator)
  new(validator).call
end

Instance Method Details

#callObject



85
86
87
88
89
# File 'lib/specificator/expectation/validation.rb', line 85

def call
  return unless valid?

  expectations.map{ |expectation| "it { should #{expectation} } #{Specificator::Generator::WATERMARK}" }
end

#valid?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/specificator/expectation/validation.rb', line 91

def valid?
  validator_kind.in?(OPTIONS_MAPPING.keys)
end