class Property::Column

The Column class is used to hold information about a Property declaration, such as name, type and options. It is also used to typecast from strings to the proper type (date, integer, float, etc).

Constants

SAFE_NAMES_REGEXP

Attributes

caster[RW]
index[RW]

Public Class Methods

new(name, default, type, options={}) click to toggle source
Calls superclass method
# File lib/property/column.rb, line 13
def initialize(name, default, type, options={})
  name = name.to_s
  if type.kind_of?(Class)
    @klass = type
  end

  super(name, default, type, options)
  extract_property_options(options)
end

Public Instance Methods

default_for(owner) click to toggle source
# File lib/property/column.rb, line 37
def default_for(owner)
  default = self.default
  if default.kind_of?(Proc)
    default.call
  elsif default.kind_of?(Symbol)
    owner.send(default)
  else
    default
  end
end
index_proc() click to toggle source

Return the Proc to use to build index (usually nil).

# File lib/property/column.rb, line 75
def index_proc
  @index_proc
end
indexed?() click to toggle source
# File lib/property/column.rb, line 33
def indexed?
  @index
end
klass() click to toggle source

Return the class related to the type of property stored (String, Integer, etc).

Calls superclass method
# File lib/property/column.rb, line 49
def klass
  @klass || super
end
role() click to toggle source

Return the role in which the column was originally defined.

# File lib/property/column.rb, line 54
def role
  @role
end
should_create_accessors?() click to toggle source
# File lib/property/column.rb, line 29
def should_create_accessors?
  name =~ SAFE_NAMES_REGEXP
end
type_cast(value) click to toggle source
Calls superclass method
# File lib/property/column.rb, line 61
def type_cast(value)
  if @caster && val = @caster.type_cast(value)
    val
  elsif type == :string
    value = value.to_s
    value.blank? ? nil : value
  elsif @klass
    value
  else
    super
  end
end
validate(value, errors) click to toggle source
# File lib/property/column.rb, line 23
def validate(value, errors)
  if @klass && !value.kind_of?(@klass)
    errors.add(name, "cannot cast #{value.class} to #{@klass}")
  end
end

Private Instance Methods

extract_default(default) click to toggle source
# File lib/property/column.rb, line 96
def extract_default(default)
  (default.kind_of?(Proc) || default.kind_of?(Symbol)) ? default : type_cast(default)
end
extract_property_options(options) click to toggle source
# File lib/property/column.rb, line 80
def extract_property_options(options)
  @index = options.delete(:index) || options.delete(:indexed)
  @role  = options.delete(:role)
  @caster= options.delete(:orig)
  if @index == true
    @index = (options.delete(:index_group) || ptype).to_s
  elsif @index.kind_of?(Proc)
    @index_proc = @index
    @index = (options.delete(:index_group) || ptype).to_s
  elsif @index.blank?
    @index = nil
  else
    @index = @index.to_s
  end
end