Class: CARPS::Sheet::Schema

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

Overview

This isn’t really the schema, but an object that wraps it and uses it to perform syntactic validations on character sheets

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schema

Create a new schema



27
28
29
# File 'lib/carps/mod/sheet/schema.rb', line 27

def initialize schema
   @schema = schema
end

Instance Method Details

#create_sheet_text(user_sheet) ⇒ Object

Create sheet text for the user to edit



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/carps/mod/sheet/schema.rb', line 53

def create_sheet_text user_sheet
   current = {}
   if user_sheet
      current = user_sheet.attributes
   end

   @schema.each_key do |field|
      unless current.include? field
         current[field] = nil
      end
   end
   sheet = "# Character sheet\n"
   current.each do |field, value|
      type = @schema[field]
      sheet += "# #{field} is #{type}\n"
      sheet += "#{field}: #{value}\n"
   end
   return sheet
end

#produce_errors(sheet) ⇒ Object

Verify the sheet’s syntax.

Produce errors if it’s invalid, nil if it’s valid



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/carps/mod/sheet/schema.rb', line 35

def produce_errors sheet
   errs = []
   @schema.each do |field, type|
      valid, coerced = verify_type sheet[field], type
      if valid
         sheet[field] = coerced
      else
         errs.push field + " was not " + type
      end
   end
   if errs.empty?
      nil
   else
      errs
   end
end