Class: Proteus::ClassParser

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

Overview

Provides functions for parsing component definitions (for component classes).

Constant Summary collapse

@@CLASS_RE =

The regex for valid class names

/^([A-Z][a-zA-Z_0-9]*)(?:[\s]*<[\s]*([A-Z][a-zA-Z_0-9]*))?$/
@@CHILDREN =
"children"

Instance Method Summary collapse

Instance Method Details

#parse_yaml(yaml, new_class = nil) ⇒ Object

Parses YAML and returns the resultant component class.

yaml: the YAML to parse new_class: the class instance to parse



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ClassParser.rb', line 53

def parse_yaml( yaml, new_class = nil )
  result = new_class || ComponentClass.new
  
  # Must be a hash of length one containing a hash
  if not ( yaml.is_a?(Hash) and yaml.length == 1 and
    (yaml.values[0].nil? or yaml.values[0].is_a?(Hash)) ) then
    
    raise Exceptions::DefinitionMalformed, "Class definition malformed"
  end
  
  # Parse the class name
  name = yaml.keys[0]
  match = @@CLASS_RE.match( name )
  
  # If there's no match, the definition is malformed
  if match.nil?
    raise Exceptions::DefinitionMalformed,
      "Class identifier '" + name + "' malformed."
  end
  
  # Set properties of class
  result.name = match[1]
  result.parent = match[2]
  result.properties = yaml.values[0] || {}
  result.children = result.properties.delete(@@CHILDREN) || []
  
  return result
end