Class: DashcodeConverter::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/dashcode-converter/controller.rb

Constant Summary collapse

FUNCTION_BODY_REGEX =
/function \w+\(([^)]*)\)\s*\{(.*)\}\s*$/m
ACTION_METHOD_CODE_DOC =
"  /**\n      <%=namespace%>.<%=name%>#<%=methodname%>(sender[, argument])\n      \n      - sender (coherent.View): The view that sent this action.\n      - argument (Any): An argument, usually set by the `argumentBinding`.\n      \n      This is an action method send by a view in your NIB.\n  **/\n"
DECL_TEMPLATE =
"    /*jsl:import coherent*/\n\n    <%=namespace%>.<%=name%>= Class.create(coherent.ViewController, {\n    \n    <%[email protected](\",\\n\").indent(INDENT)%>\n    \n    });\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, namespace = nil, scripts = nil) ⇒ Controller



30
31
32
33
34
35
# File 'lib/dashcode-converter/controller.rb', line 30

def initialize(name, namespace=nil, scripts=nil)
  @name= "#{name.capitalize}Controller"
  @namespace= namespace || name
  @methods= []
  @scripts= scripts
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/dashcode-converter/controller.rb', line 28

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



28
29
30
# File 'lib/dashcode-converter/controller.rb', line 28

def namespace
  @namespace
end

Instance Method Details

#add_action_method(methodname) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dashcode-converter/controller.rb', line 37

def add_action_method(methodname)
  doc= ERB.new(ACTION_METHOD_CODE_DOC.remove_indent).result binding

  match= FUNCTION_BODY_REGEX.match(@scripts[methodname].to_s)
  unless match
    @methods << "#{doc}\n#{methodname}: function(sender, argument)\n{\n}"
    return
  end
  
  args= match[1].split(/\s*,\*/)
  body= match[2]

  if args.include?('event') && body[/\bevent\b/]
    body= "\n    var event= coherent.EventLoop.currentEvent;\n#{body}"
  end
  # Fixup any crazy DC references
  body.gsub!(/\bDC\./, "coherent.")
  # Fixup datasources
  body.gsub!(/dashcode\.getDataSource\(['"](\w+)['"]\)/) { |match|
    "this.#{$1}"
  }
  @methods << "#{doc}\n#{methodname}: function(sender, argument)\n{#{body}}"
end

#declarationObject



61
62
63
64
# File 'lib/dashcode-converter/controller.rb', line 61

def declaration
  return @declaration if @declaration
  @declaration= ERB.new(DECL_TEMPLATE.remove_indent).result binding
end