Class: Codestrap::Patch::File

Inherits:
File
  • Object
show all
Defined in:
lib/codestrap/patch.rb

Overview

Inherited and patched File class

Used as an alternative to monkey patching

Instance Method Summary collapse

Instance Method Details

#has_mode_line?true|false

Has mode line

Returns:

  • (true|false)


117
118
119
# File 'lib/codestrap/patch.rb', line 117

def has_mode_line?
  !!self.mode_line
end

#mode_line(limit = 10) ⇒ true\nil

Set and get mode line status

Parameters:

  • limit (Integer) (defaults to: 10)

    Number of lines to scan from the top

Returns:

  • (true\nil)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/codestrap/patch.rb', line 80

def mode_line(limit = 10)
  @mode_line ||= begin
                   # Check if file read
    curpos   = self.pos
    self.pos = 0
    lines    = self.readlines()[0 .. (limit - 1)]
    lines.each do |line|
      line =~ /(?:^|\b)(strap|stub):(erb|\S+?)(?:\b|$)/
      if $1
        @mode_line = line
        @template  = $2.to_sym if $2
        @template  = :erb if $2.length == 0
        self.options
      end
      break
    end
    self.pos = curpos
    @mode_line
  end
end

#optionsObject

Parse mode line options



103
104
105
106
107
108
109
110
111
112
# File 'lib/codestrap/patch.rb', line 103

def options
  @options ||= begin
    @mode_line.scan(/(\S+)\s*=\s*(\S+)/).each do |arr|
      key           = arr[0]
      value         = arr[1]
      @options[key] = value
    end
    @options
  end
end

#templateSymbol|nil

Returns Template types :erb.

Returns:

  • (Symbol|nil)

    Template types :erb



67
68
69
70
71
72
# File 'lib/codestrap/patch.rb', line 67

def template
  @template || begin
    self.mode_line
    @template
  end
end