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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, extends, file_contents) ⇒ CoffeeClass

Returns a new instance of CoffeeClass.



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

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.



15
16
17
# File 'lib/lanes/api/coffeescript_processor.rb', line 15

def contents
  @contents
end

#extendsObject (readonly)

Returns the value of attribute extends.



15
16
17
# File 'lib/lanes/api/coffeescript_processor.rb', line 15

def extends
  @extends
end

#file_contentsObject (readonly)

Returns the value of attribute file_contents.



15
16
17
# File 'lib/lanes/api/coffeescript_processor.rb', line 15

def file_contents
  @file_contents
end

#indentObject (readonly)

Returns the value of attribute indent.



15
16
17
# File 'lib/lanes/api/coffeescript_processor.rb', line 15

def indent
  @indent
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/lanes/api/coffeescript_processor.rb', line 15

def name
  @name
end

Instance Method Details

#ensure_property(property_name, definition) ⇒ Object



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

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



31
32
33
34
# File 'lib/lanes/api/coffeescript_processor.rb', line 31

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