Sha256: 8f431bc5e3a213468234c242ec247c777204c6a57bc98145a55684f33db7143f

Contents?: true

Size: 1.48 KB

Versions: 12

Compression:

Stored size: 1.48 KB

Contents

module Spira
  module Resource

    ##
    # Instance methods relating to validations for a Spira resource.  This
    # includes the default assertions.
    #
    # @see Spira::Resource::InstanceMethods
    # @see Spira::Resource::ClassMethods
    # @see Spira::Resource::DSL
    module Validations

      ##
      # Assert a fact about this instance.  If the given expression is false,
      # an error will be noted.
      #
      # @example Assert that a title is correct
      #     assert(title == 'xyz', :title, 'bad title')
      # @param  [Any] boolean The expression to evaluate
      # @param  [Symbol] property The property or has_many to mark as incorrect on failure
      # @param  [String] message The message to record if this assertion fails
      # @return [Void]
      def assert(boolean, property, message)
        errors.add(property, message) unless boolean
      end

      ##
      # A default helper assertion.  Asserts that a given property is set.
      #
      # @param  [Symbol] name The property to check
      # @return [Void]
      def assert_set(name)
        assert(!(self.send(name).nil?), name, "#{name.to_s} cannot be nil")
      end

      ##
      # A default helper assertion.  Asserts that a given property is numeric.
      #
      # @param  [Symbol] name The property to check
      # @return [Void]
      def assert_numeric(name)
        assert(self.send(name).is_a?(Numeric), name, "#{name.to_s} must be numeric (was #{self.send(name)})")
      end

    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
spira-0.0.12 lib/spira/resource/validations.rb
spira-0.0.11 lib/spira/resource/validations.rb
spira-0.0.10 lib/spira/resource/validations.rb
spira-0.0.9 lib/spira/resource/validations.rb
spira-0.0.8 lib/spira/resource/validations.rb
spira-0.0.7 lib/spira/resource/validations.rb
spira-0.0.6 lib/spira/resource/validations.rb
spira-0.0.5 lib/spira/resource/validations.rb
spira-0.0.4 lib/spira/resource/validations.rb
spira-0.0.3 lib/spira/resource/validations.rb
spira-0.0.2 lib/spira/resource/validations.rb
spira-0.0.1 lib/spira/resource/validations.rb