class Interface

ActionScript 3 Interface Parser. Tokenises the contents of an interface into hashes.

All methods and properties are stored in Hashes. Details of methods can be obtained via :name String, :arguments Array, return: String. Properies have the keys :name String, :gets Boolean, :sets Boolean, and :type String.

Attributes

class_name[R]
methods[R]
name[R]
package[R]
properties[R]

Public Class Methods

new(string) click to toggle source
# File lib/shed/interface.rb, line 18
def initialize(string)
  @doc = Stripper.ecma_comments(string)

  @properties, @methods = {}, {}
  @package, @name = '', ''

  raise "Document is not an interface" unless is_valid
  parse
end

Public Instance Methods

get_method(name) click to toggle source

Returns the method hash associated with request name.

# File lib/shed/interface.rb, line 31
def get_method(name)
  @methods[name]
end
get_property(name) click to toggle source

Returns the proptert hash associated with request name.

# File lib/shed/interface.rb, line 38
def get_property(name)
  @properties[name]
end

Private Instance Methods

accessor_regexp(type='get|set') click to toggle source

Constructs the regular expression used when finding accessors.

# File lib/shed/interface.rb, line 146
def accessor_regexp(type='get|set')
  /^\s*function\s+\b(#{type})\b\s+\b(\w+)\b\s*\(([^)\n]*)(\)(\s*:\s*(\w+|\*))?)?/
end
add_method(name,arguments,returns) click to toggle source

Adds a method hash to the list of methods.

# File lib/shed/interface.rb, line 123
def add_method(name,arguments,returns)
  @methods[name] = {
    :name => name,
    :arguments => parameterize(arguments),
    :return => returns
  }
end
create_prop(name) click to toggle source

Creates a default property hash.

# File lib/shed/interface.rb, line 153
def create_prop(name)
  unless @properties.has_key? name
    @properties[name] = {
      :gets => false,
      :sets => false,
      :name => name
    }
  end
  @properties[name]
end
find_getters() click to toggle source

Finds all getters defined in the interface.

# File lib/shed/interface.rb, line 97
def find_getters
  regexp = accessor_regexp('get')

  @doc.scan(regexp).each do |line|
    prop = create_prop(line[1])
    prop[:type] = line[5]
    prop[:gets] = true;
  end
end
find_methods() click to toggle source

Finds all methods defined in the interface.

# File lib/shed/interface.rb, line 86
def find_methods
  regexp = /^\s*function\s+\b([a-z]\w+)\b\s*\((([^)\n]*)|(?m:[^)]+))\)\s*:\s*((\w+|\*))/

  @doc.scan(regexp).each do |line|
    add_method(line[0],line[1],line[3])
  end
end
find_name() click to toggle source

Finds the name of the interface.

# File lib/shed/interface.rb, line 67
def find_name
  regexp = /^(\s+)?public\s+interface\s+(\w+)\b/
  @doc.scan(regexp).each { |line|
    @name = line[1]
    @class_name = @name.sub(/^I/,'')
  }
end
find_package() click to toggle source

Finds the package of the interface.

# File lib/shed/interface.rb, line 78
def find_package
  regexp = /^(\s+)?package\s+([A-Za-z0-9.]+)/
  @doc.scan(regexp).each { |line| @package = line[1] }
end
find_setters() click to toggle source

Finds all setters defined in the interface.

# File lib/shed/interface.rb, line 110
def find_setters
  regexp = accessor_regexp('set')

  @doc.scan(regexp).each do |line|
    prop = create_prop(line[1])
    prop[:type] = line[2].split(':')[1]
    prop[:sets] = true;
  end
end
is_valid() click to toggle source

Detect if the supplied string is a valid ActionScript Interface file.

# File lib/shed/interface.rb, line 47
def is_valid
  @doc.scan(/^\s*public\s+(interface)\s+(\w+)\b/)
  return true if $1 == "interface"
  return false
end
parameterize(params) click to toggle source

Converts method arguments into an arry of parameters.

# File lib/shed/interface.rb, line 134
def parameterize(params)
  arr = []
  params.gsub!(/(\s|\n)/,'')
  params.scan(/(\b\w+\b\s*:\s*\b\w+\b(=\s*(['"].*['"]|\w+))?|(\.\.\.\w+))/).each do |match|
    arr << match[0]
  end
  arr
end
parse() click to toggle source

Parses the supplied string for all relevant information.

# File lib/shed/interface.rb, line 56
def parse
  find_name
  find_package
  find_methods
  find_getters
  find_setters
end