Class: Ripple::Property

Inherits:
Object show all
Defined in:
ripple/lib/ripple/properties.rb

Overview

Encapsulates a single property on your Ripple::Document class.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Property) initialize(key, type, options = {})

Create a new document property.

Parameters:

  • key (String, Symbol)

    the key of the property

  • type (Class)

    the Ruby type of the property. Use Boolean for true or false types.

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

    configuration options

Options Hash (options):

  • :default (Object, Proc) — default: nil

    a default value for the property, or a lambda to evaluate when providing the default.



55
56
57
58
59
# File 'ripple/lib/ripple/properties.rb', line 55

def initialize(key, type, options={})
  @options = options.to_options
  @key = key.to_sym
  @type = type
end

Instance Attribute Details

- (Symbol) key (readonly)

The key of this property in the Document

Returns:

  • (Symbol)

    the key of this property in the Document



44
45
46
# File 'ripple/lib/ripple/properties.rb', line 44

def key
  @key
end

- (Hash) options (readonly)

Configuration options

Returns:

  • (Hash)

    configuration options



48
49
50
# File 'ripple/lib/ripple/properties.rb', line 48

def options
  @options
end

- (Class) type (readonly)

The Ruby type of property.

Returns:

  • (Class)

    the Ruby type of property.



46
47
48
# File 'ripple/lib/ripple/properties.rb', line 46

def type
  @type
end

Instance Method Details

- (Object) default

The default value for this property if defined, or nil.

Returns:

  • (Object)

    The default value for this property if defined, or nil.



62
63
64
65
66
67
# File 'ripple/lib/ripple/properties.rb', line 62

def default
  default = options[:default]

  return nil if default.nil?
  type_cast(default.respond_to?(:call) ? default.call : default)
end

- (Object) type_cast(value)

Attempt to coerce the passed value into this property’s type

Parameters:

  • value (Object)

    the value to coerce

Returns:

  • (Object)

    the value coerced into this property’s type

Raises:



78
79
80
81
82
83
84
# File 'ripple/lib/ripple/properties.rb', line 78

def type_cast(value)
  if @type.respond_to?(:ripple_cast)
    @type.ripple_cast(value)
  else
    value
  end
end

- (Hash) validation_options

Options appropriate for the validates class method

Returns:

  • (Hash)

    options appropriate for the validates class method



70
71
72
# File 'ripple/lib/ripple/properties.rb', line 70

def validation_options
  @options.dup.except(:default)
end