Class: Kiss::Format

Inherits:
Object show all
Defined in:
lib/kiss/format.rb

Overview

This class is used to validate and parse strings into Sequel-ready values.

Direct Known Subclasses

AlphaNum, DateTime, EmailAddress, Integer, URL, Word

Defined Under Namespace

Classes: AlphaNum, Date, DateTime, Decimal, EmailAddress, Integer, MonthYear, NegativeInteger, PositiveInteger, Time, URL, UnsignedInteger, ValidateError, Word

Constant Summary collapse

@@symbols =
{
  :integer => Integer,
  
  :integer_positive => PositiveInteger,
  :positive_integer => PositiveInteger,
  :id => PositiveInteger,
  
  :integer_unsigned => UnsignedInteger,
  :unsigned_integer => UnsignedInteger,
  :id_or_zero => UnsignedInteger,
  :id_zero => UnsignedInteger,
  
  :integer_negative => NegativeInteger,
  :negative_integer => NegativeInteger,
  
  :decimal => Decimal,
  
  :word => Word,
  :alphanum => AlphaNum,
  
  :email => EmailAddress,
  :email_address => EmailAddress,
  :url => URL,
  
  :datetime => DateTime,
  :date => Date,
  :time => Time,
  :month_year => MonthYear
}

Class Method Summary collapse

Class Method Details

.inherited(subclass) ⇒ Object

copy Format class instance variables to their subclasses



16
17
18
19
20
# File 'lib/kiss/format.rb', line 16

def inherited(subclass)
  self.instance_variables.each do |var|
    subclass.instance_variable_set(var, instance_variable_get(var))
  end
end

.lookup(format) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/kiss/format.rb', line 231

def self.lookup(format)
  format = format.to_sym if format.is_a?(String)
  format.is_a?(Symbol) ?
    # if symbol, lookup format by symbol
    @@symbols[format] || self :
    # else return format, or Kiss::Format if format not defined
    format || self
end

.parse(value, context = {}) ⇒ Object



22
23
24
# File 'lib/kiss/format.rb', line 22

def parse(value, context = {})
  value
end

.validate(value, context = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/kiss/format.rb', line 30

def validate(value, context = {})
  if !value.blank? && @_regexp && value.to_s !~ @_regexp
    raise Kiss::Format::ValidateError, @_error
  end
  parse(value, context)
end

.value_to_s(value, context = {}) ⇒ Object



26
27
28
# File 'lib/kiss/format.rb', line 26

def value_to_s(value, context = {})
  value.to_s
end