Class: Stannum::Contracts::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/stannum/contracts/definition.rb

Overview

Struct that encapsulates a constraint definition on a contract.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Definition

Returns a new instance of Definition.

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes for the definition.

Options Hash (attributes):



15
16
17
18
19
# File 'lib/stannum/contracts/definition.rb', line 15

def initialize(attributes = {})
  attributes.each do |key, value|
    send(:"#{key}=", value)
  end
end

Instance Attribute Details

#constraintStannum::Constraints::Base

Returns the defined constraint.

Returns:



23
24
25
# File 'lib/stannum/contracts/definition.rb', line 23

def constraint
  @constraint
end

#contractStannum::Contracts::Base

Returns the contract containing the constraint.

Returns:



28
29
30
# File 'lib/stannum/contracts/definition.rb', line 28

def contract
  @contract
end

#optionsHash<Symbol, Object>

Returns the options defined for the constraint.

Returns:

  • (Hash<Symbol, Object>)

    the options defined for the constraint.



32
33
34
# File 'lib/stannum/contracts/definition.rb', line 32

def options
  @options
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the other object with the definition.

Parameters:

  • other (Object)

    The object to compare.

Returns:

  • (Boolean)

    true if the other object is a Definition with the same attributes; otherwise false.



40
41
42
43
44
45
# File 'lib/stannum/contracts/definition.rb', line 40

def ==(other)
  other.is_a?(self.class) &&
    other.constraint == constraint &&
    other.contract.equal?(contract) &&
    other.options == options
end

#concatenatable?Boolean

Indicates whether the defined constraint is inherited via concatenation.

Returns:

  • (Boolean)

    true if options is set to a truthy value; otherwise false.



51
52
53
# File 'lib/stannum/contracts/definition.rb', line 51

def concatenatable?
  !!options.fetch(:concatenatable, true)
end

#propertynil, ...

Returns the property scope of the constraint.

Returns:

  • (nil, String, Symbol, Array<String, Symbol>)

    the property scope of the constraint.



57
58
59
# File 'lib/stannum/contracts/definition.rb', line 57

def property
  options[:property]
end

#property_namenil, ...

Returns the property name of the constraint, used for generating errors. If not given, defaults to the value of #property.

Returns:

  • (nil, String, Symbol, Array<String, Symbol>)

    the property name of the constraint, used for generating errors. If not given, defaults to the value of #property.



64
65
66
# File 'lib/stannum/contracts/definition.rb', line 64

def property_name
  options.fetch(:property_name, options[:property])
end

#sanity?Boolean

Returns true if options is set to a truthy value; otherwise false.

Returns:

  • (Boolean)

    true if options is set to a truthy value; otherwise false.



70
71
72
# File 'lib/stannum/contracts/definition.rb', line 70

def sanity?
  !!options[:sanity]
end