class Versionomy::Schema::FieldBuilder

These methods are available in a schema field definition block.

Public Instance Methods

default_value(value_) click to toggle source

Provide a default value.

# File lib/versionomy/schema/field.rb, line 408
def default_value(value_)
  @field._set_default_value(value_)
end
field(name_, opts_={}, &block_) click to toggle source

Add a child field.

Recognized options include:

:only

The child should be available only for the given values of this field. See below for ways to specify this constraint.

:type

Type of field. This should be :integer, :string, or :symbol. Default is :integer.

:default_value

Default value for the field if no value is explicitly set. Default is 0 for an integer field, the empty string for a string field, or the first symbol added for a symbol field.

You may provide an optional block. Within the block, you may call methods of this class again to customize the child.

Raises Versionomy::Errors::IllegalValueError if the given default value is not legal.

The :only constraint may be specified in one of the following ways:

  • A single value (integer, string, or symbol)

  • The result of calling range() to define an inclusive range of integers, strings, or symbols. In this case, either element may be nil, specifying an open end of the range. If the field type is symbol, the ordering of symbols for the range is defined by the order in which the symbols were added to this schema.

  • A Range object defining a range of integers or strings. Only inclusive, not exclusive, ranges are supported.

  • An array of the above.

Raises Versionomy::Errors::RangeSpecificationError if the given ranges are not legal.

Raises Versionomy::Errors::RangeOverlapError if the given ranges overlap previously specified ranges, or more than one default schema is specified.

# File lib/versionomy/schema/field.rb, line 483
def field(name_, opts_={}, &block_)
  only_ = opts_.delete(:only)
  opts_.merge!(:master_builder => @master_builder)
  @field.add_child(Schema::Field.new(name_, opts_, &block_), only_)
end
range(first_, last_) click to toggle source

Define a range for the :only parameter to child.

This creates an object that child interprets like a standard ruby Range. However, it is customized for the use of child in the following ways:

  • It supports only inclusive, not exclusive ranges.

  • It supports open-ended ranges by setting either endpoint to nil.

  • It supports symbol ranges under Ruby 1.8.

# File lib/versionomy/schema/field.rb, line 499
def range(first_, last_)
  [first_, last_]
end
symbol(symbol_, opts_={}) click to toggle source

Define the given symbol.

Recognized options include:

:bump

The symbol to transition to when “bump” is called. Default is to remain on the same value.

Raises Versionomy::Errors::TypeMismatchError if called when the current field is not of type :symbol.

Raises Versionomy::Errors::SymbolRedefinedError if the given symbol name is already defined.

# File lib/versionomy/schema/field.rb, line 401
def symbol(symbol_, opts_={})
  @field._add_symbol(symbol_, opts_)
end
to_bump(&block_) click to toggle source

Provide a “bump” procedure. The given block should take a value, and return the value to transition to. If you return nil, the value will remain the same.

# File lib/versionomy/schema/field.rb, line 417
def to_bump(&block_)
  @field._set_bump_proc(block_)
end
to_canonicalize(&block_) click to toggle source

Provide a “canonicalize” procedure. The given block should take a value and return a canonicalized value. Return nil if the given value is illegal.

# File lib/versionomy/schema/field.rb, line 437
def to_canonicalize(&block_)
  @field._set_canonicalize_proc(block_)
end
to_compare(&block_) click to toggle source

Provide a “compare” procedure. The given block should take two values and compare them. It should return a negative integer if the first is less than the second, a positive integer if the first is greater than the second, or 0 if the two values are equal. If the values cannot be compared, return nil.

# File lib/versionomy/schema/field.rb, line 428
def to_compare(&block_)
  @field._set_compare_proc(block_)
end