class Raml::PropertiesNode

Attributes

optional[RW]

@!attribute [rw] optional

@return [Boolean] whether the property is optional. Only valid
  for decendant nodes a {Trait::Instance} or {ResourceType::Instance}.
  Indicated by a trailing "?" on the property name in the RAML source.

Public Class Methods

new(name, properties, parent) click to toggle source
Calls superclass method Raml::Node::new
# File lib/raml/node.rb, line 90
def initialize(name, properties, parent)
  if name.is_a? String and name.end_with? '?'
    allow_optional? parent
    name = name.dup.chomp! '?'
    @optional = true
  else
    @optional = false
  end

  super name, parent

  @children ||= []
  parse_and_validate_props properties
end

Private Class Methods

_property(type, *properties) click to toggle source
# File lib/raml/node.rb, line 68
def _property(type, *properties)
  properties.map(&:to_s).each { |prop| type << prop unless type.include? prop }
end
inherit_class_attributes() click to toggle source
# File lib/raml/node.rb, line 54
def inherit_class_attributes
  self.scalar_properties     = self.scalar_properties.dup
  self.non_scalar_properties = self.non_scalar_properties.dup
end
non_scalar_property(*properties) click to toggle source
# File lib/raml/node.rb, line 64
def non_scalar_property(*properties)
  _property(non_scalar_properties, *properties)
end
regexp_property(regexp, parse) click to toggle source
# File lib/raml/node.rb, line 72
def regexp_property(regexp, parse)
  self._regexp_property = [ regexp, parse ]
end
scalar_property(*properties) click to toggle source
# File lib/raml/node.rb, line 59
def scalar_property(*properties)
  attr_accessor(*properties.map(&:to_sym))
  _property(scalar_properties, *properties)
end

Public Instance Methods

_regexp_property() click to toggle source

@private

# File lib/raml/node.rb, line 82
def _regexp_property     ; self.class._regexp_property     ; end
non_scalar_properties() click to toggle source

@private

# File lib/raml/node.rb, line 80
def non_scalar_properties; self.class.non_scalar_properties; end
scalar_properties() click to toggle source

@private

# File lib/raml/node.rb, line 78
def scalar_properties    ; self.class.scalar_properties    ; end

Private Instance Methods

allow_optional?(parent) click to toggle source
# File lib/raml/node.rb, line 107
def allow_optional?(parent)
  until parent == parent.parent or parent.is_a? Root
    return if parent.is_a? Trait::Instance or parent.is_a? ResourceType::Instance
    parent = parent.parent
  end
  raise InvalidProperty, 'Optional properties are only allowed within a trait or resource type specification.'
end
initialize_clone(other) click to toggle source
Calls superclass method
# File lib/raml/node.rb, line 151
def initialize_clone(other)
  super
  @children = @children.clone
  @children.map! do |child|
    child = child.clone
    child.parent = self
    child
  end
end
maybe_exec(method, *args) click to toggle source
# File lib/raml/node.rb, line 147
def maybe_exec(method, *args)
  send(method,*args) if respond_to? method, true
end
parse_and_validate_props(properties) click to toggle source
# File lib/raml/node.rb, line 115
def parse_and_validate_props(properties)
  maybe_exec :validate_name
  maybe_exec :validate_parent

  if !properties.nil?
    properties.each do |prop_name, prop_value|
      prop_name       = prop_name.to_s
      under_prop_name = underscore prop_name

      if scalar_properties.include? under_prop_name
        send "#{under_prop_name}=", prop_value
        maybe_exec "validate_#{under_prop_name}"

      elsif non_scalar_properties.include? under_prop_name
        parsed = send "parse_#{under_prop_name}", prop_value
        parsed = [ parsed ] unless parsed.is_a? Array
        @children += parsed

      elsif _regexp_property and _regexp_property[0].match prop_name
        parsed = self.instance_exec(prop_name, prop_value, &_regexp_property[1])
        parsed = [ parsed ] unless parsed.is_a? Array
        @children += parsed

      else
        raise UnknownProperty, "#{prop_name} is an unknown property with value of #{prop_value}."
      end
    end
  end

  validate if respond_to? :validate, true
end