Class: CARPS::Sheet::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/carps/mod/sheet/type.rb

Overview

Types available in character sheets

Subclasses must provide a method called valid, which takes a value and determines if it is valid.

Subclasses may provide a method called coercion, which should return a symbol referring to a method which coerces a value into a different type (example, :to_s)

Subclasses may provide a method called empty which should determine if a value is empty

Direct Known Subclasses

Bool, Choice, Int, Text

Instance Method Summary collapse

Constructor Details

#initialize(optional) ⇒ Type

Returns a new instance of Type.



33
34
35
# File 'lib/carps/mod/sheet/type.rb', line 33

def initialize optional
   @optional = optional
end

Instance Method Details

#coercionObject

No coercion



68
69
# File 'lib/carps/mod/sheet/type.rb', line 68

def coercion
end

#empty(val) ⇒ Object

Can’t be empty



72
73
74
# File 'lib/carps/mod/sheet/type.rb', line 72

def empty val
   false
end

#verify(val) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/carps/mod/sheet/type.rb', line 37

def verify val
   if val == nil
      if @optional
         return [true, nil]
      end
   end
   ok = false
   if coercion
      ok = valid(val) && val.respond_to?(coercion)
   else
      ok = valid(val)
   end

   if ok
      if empty(val) and @optional
         return [true, nil]
      else
         coerced = nil
         if coercion
            coerced = val.send coercion
         else
            coerced = val
         end
         [true, coerced]
      end
   else
      [false, nil]
   end
end