Class: Versionomy::Schema::FieldBuilder

Inherits:
Object
  • Object
show all
Includes:
Blockenspiel::DSL
Defined in:
lib/versionomy/schema/field.rb

Overview

These methods are available in a schema field definition block.

Instance Method Summary collapse

Constructor Details

#initialize(field_, master_builder_) ⇒ FieldBuilder

:nodoc:



381
382
383
384
# File 'lib/versionomy/schema/field.rb', line 381

def initialize(field_, master_builder_)  # :nodoc:
  @field = field_
  @master_builder = master_builder_
end

Instance Method Details

#default_value(value_) ⇒ Object

Provide a default value.



408
409
410
# File 'lib/versionomy/schema/field.rb', line 408

def default_value(value_)
  @field._set_default_value(value_)
end

#field(name_, opts_ = {}, &block_) ⇒ Object

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.



483
484
485
486
487
# 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_) ⇒ Object

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.



499
500
501
# File 'lib/versionomy/schema/field.rb', line 499

def range(first_, last_)
  [first_, last_]
end

#symbol(symbol_, opts_ = {}) ⇒ Object

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.



401
402
403
# File 'lib/versionomy/schema/field.rb', line 401

def symbol(symbol_, opts_={})
  @field._add_symbol(symbol_, opts_)
end

#to_bump(&block_) ⇒ Object

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.



417
418
419
# File 'lib/versionomy/schema/field.rb', line 417

def to_bump(&block_)
  @field._set_bump_proc(block_)
end

#to_canonicalize(&block_) ⇒ Object

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



437
438
439
# File 'lib/versionomy/schema/field.rb', line 437

def to_canonicalize(&block_)
  @field._set_canonicalize_proc(block_)
end

#to_compare(&block_) ⇒ Object

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.



428
429
430
# File 'lib/versionomy/schema/field.rb', line 428

def to_compare(&block_)
  @field._set_compare_proc(block_)
end