Class: Kali::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/kali/type.rb

Overview

Public: Type classes are used to convert ruby objects from and to their iCalendar representations.

Examples:

type = Kali::Type::Date.new
type.encode(Date.today) #=> "20130712"
type.decode("20130712") #=> Date.new(2013, 7, 12)

type = Kali::Type::Integer.new(0..9)
type.encode(3) #=> "3"
type.encode(15) #=> ArgumentError
type.decode("7") #=> 7

type = Kali::Type::Text.new(->(s) { s.size > 3 })
type.encode("x") #=> ArgumentError
type.encode("long") #=> "long"

type = Kali::Type::Text.new(Enum["ACTIVE", "INACTIVE"])
type.encode("ACTIVE") #=> "ACTIVE"
type.encode("nope") #=> ArgumentError

Direct Known Subclasses

Boolean, DateTime, Duration, Float, Geo, Integer, List, Quoted, Text, URI

Defined Under Namespace

Classes: Boolean, CalAddress, Date, DateTime, Duration, Float, Geo, Integer, List, Quoted, Text, Time, URI

Instance Method Summary collapse

Constructor Details

#initialize(restriction = nil) ⇒ Type

Public: Initialize the Type.

restriction - An object that implenents the #=== operator. When we

validate that a certain value is valid for this type, we
will check the value against `restriction.===`. If it's
`nil` then we don't restrict the value.


31
32
33
# File 'lib/kali/type.rb', line 31

def initialize(restriction = nil)
  @restriction = restriction
end

Instance Method Details

#decode(obj) ⇒ Object

Public: Decode an iCalendar-compatible String into an Object that matches this Type.

Requires a subclass to implement #decode!

obj - A String that matches the iCalendar representation for this Type..

Returns an Object that matches this Type.



64
65
66
# File 'lib/kali/type.rb', line 64

def decode(obj)
  validate(decode!(obj))
end

#encode(obj) ⇒ Object

Public: Encode an object into an iCalendar-compatible String using the format specific to this Type.

Requires a subclass to implement #encode!

obj - An Object that matches this Type.

Returns a String that matches the iCalendar representation of this Type.



52
53
54
# File 'lib/kali/type.rb', line 52

def encode(obj)
  encode!(validate(obj))
end

#parametersObject

Public: Parameters added by deafult to properties that are of this type.

Returns an Array where each entry is a Hash keyed by a subclass of Kali::Parameter, and the value is a Symbol with the name of the method for that property.



40
41
42
# File 'lib/kali/type.rb', line 40

def parameters
  []
end