Class: Merrol::SourceModel

Inherits:
Gtk::SourceBuffer
  • Object
show all
Defined in:
lib/merrol/models/source_model.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ SourceModel

Returns a new instance of SourceModel.



4
5
6
7
8
9
# File 'lib/merrol/models/source_model.rb', line 4

def initialize path = nil
  super(nil)
  @path = path
  load
  language_from_filetype
end

Instance Method Details

#language_from_filetypeObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/merrol/models/source_model.rb', line 23

def language_from_filetype
  yaml = YAML.load_config('language')
  yaml.each do |name, hash|
    hash['pattern'].split(' ').each do |pattern|
      if pattern[0..0] == '.' && @path[0..0] != '.'
        name_or_ext = File.extname(@path)
      else
        name_or_ext = File.basename(@path)
      end

      bang_line = "#!/usr/bin/env #{hash['bang']}"
      file_exists = hash['exists'] ? File.exist?(hash['exists']) : true
      if (pattern == name_or_ext || self.text[0..(bang_line.length - 1)] == bang_line) && file_exists
        self.language = Gtk::SourceLanguageManager.new.get_language name
        return
      end
    end
  end
end

#loadObject



16
17
18
19
20
21
# File 'lib/merrol/models/source_model.rb', line 16

def load
  if @path && File.exist?(@path)
    begin_not_undoable_action { self.text = File.read(@path) }
    self.modified = false
  end
end

#saveObject



11
12
13
14
# File 'lib/merrol/models/source_model.rb', line 11

def save
  File.open(@path, 'w') {|f| f.write(self.text) }
  self.modified = false
end