Class: Lanes::API::CoffeeScriptProcessor::CoffeeClass

Inherits:
Object
  • Object
show all
Defined in:
lib/lanes/api/coffeescript_processor.rb

Direct Known Subclasses

ReactCoffeeClass

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, extends, file_contents) ⇒ CoffeeClass

Returns a new instance of CoffeeClass.



18
19
20
21
22
23
# File 'lib/lanes/api/coffeescript_processor.rb', line 18

def initialize(name, extends, file_contents)
    @name=name; @extends=extends; @file_contents=file_contents
    file_contents.gsub!(/class\s+#{name}\s+.*?\n/,"class #{name}\n")
    @contents = @file_contents[/(class #{name}\n.*?)(\n\w|\Z)/m,1]
    @indent = @contents[/\n(\s+)(\w+):/,1] || '    '
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



17
18
19
# File 'lib/lanes/api/coffeescript_processor.rb', line 17

def contents
  @contents
end

#extendsObject (readonly)

Returns the value of attribute extends.



17
18
19
# File 'lib/lanes/api/coffeescript_processor.rb', line 17

def extends
  @extends
end

#file_contentsObject (readonly)

Returns the value of attribute file_contents.



17
18
19
# File 'lib/lanes/api/coffeescript_processor.rb', line 17

def file_contents
  @file_contents
end

#indentObject (readonly)

Returns the value of attribute indent.



17
18
19
# File 'lib/lanes/api/coffeescript_processor.rb', line 17

def indent
  @indent
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/lanes/api/coffeescript_processor.rb', line 17

def name
  @name
end

Instance Method Details

#ensure_property(property_name, definition) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/lanes/api/coffeescript_processor.rb', line 25

def ensure_property(property_name, definition)
    if contents !~ /^\s+#{property_name}\s*:/
        # figure out how much to indent, sigh.
        file_contents.gsub!(/class #{name}\n/,
                            "\\0#{indent}#{property_name}: #{definition}\n")
    end
end

#saveObject



38
39
40
41
# File 'lib/lanes/api/coffeescript_processor.rb', line 38

def save
    file_contents.gsub!(/^(\s*class #{name}\n.*?)(\n\w|\Z)/m,
                        "\\1\n#{extends}.extend(#{name})\n\\2")
end

#setup_propertiesObject



33
34
35
36
# File 'lib/lanes/api/coffeescript_processor.rb', line 33

def setup_properties
    ensure_property("constructor", "-> super")
    ensure_property("FILE", "FILE")
end