Class: Redpear::Schema::Column

Inherits:
String
  • Object
show all
Defined in:
lib/redpear/schema/column.rb

Direct Known Subclasses

Index

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, type = nil) ⇒ Column

Creates a new column.

Parameters:

  • model (Redpear::Model)

    the model the column is associated with

  • name (String)

    the column name

  • type (Symbol) (defaults to: nil)

    the column type (:string (default), :counter, :integer, :float, :timestamp)



11
12
13
14
15
# File 'lib/redpear/schema/column.rb', line 11

def initialize(model, name, type = nil)
  super name.to_s
  @model = model
  @type  = type.to_sym if type
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



2
3
4
# File 'lib/redpear/schema/column.rb', line 2

def model
  @model
end

#typeObject (readonly)

Returns the value of attribute type.



2
3
4
# File 'lib/redpear/schema/column.rb', line 2

def type
  @type
end

Instance Method Details

#encode_value(value) ⇒ Object

Encodes a value, for storage

Returns:

  • (Object)

    the encoded value



40
41
42
43
44
45
46
47
# File 'lib/redpear/schema/column.rb', line 40

def encode_value(value)
  case value
  when Time
    value.to_i
  else
    value
  end
end

#nameString

Returns the column name.

Returns:

  • (String)

    the column name



50
51
52
# File 'lib/redpear/schema/column.rb', line 50

def name
  to_s
end

#readable?Boolean

Returns true if the column is readable.

Returns:

  • (Boolean)

    true if the column is readable



55
56
57
# File 'lib/redpear/schema/column.rb', line 55

def readable?
  true
end

#type_cast(value, type = self.type) ⇒ Object

Casts a value to a type

Parameters:

  • value

    the value to cast

  • type (Symbol, #optional) (defaults to: self.type)

    the type to cast to, defaults to the column type

Returns:

  • the casted value



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/redpear/schema/column.rb', line 22

def type_cast(value, type = self.type)
  case type
  when :counter
    type_cast(value, :integer).to_i
  when :integer
    Kernel::Integer(value) rescue nil if value
  when :float
    Kernel::Float(value) rescue nil if value
  when :timestamp
    value = type_cast(value, :integer)
    Time.at(value) if value
  else
    value
  end
end

#writable?Boolean

Returns true if the column is writable.

Returns:

  • (Boolean)

    true if the column is writable



60
61
62
# File 'lib/redpear/schema/column.rb', line 60

def writable?
  true
end