Class: Madman::Document

Inherits:
Object
  • Object
show all
Includes:
Injector
Defined in:
lib/madman/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Injector

#inject

Constructor Details

#initialize(text, filename = nil) ⇒ Document

Returns a new instance of Document.



21
22
23
24
# File 'lib/madman/document.rb', line 21

def initialize(text, filename=nil)
  @text = text
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/madman/document.rb', line 8

def filename
  @filename
end

#textObject

Returns the value of attribute text.



9
10
11
# File 'lib/madman/document.rb', line 9

def text
  @text
end

#yaml_modeObject

Returns the value of attribute yaml_mode.



9
10
11
# File 'lib/madman/document.rb', line 9

def yaml_mode
  @yaml_mode
end

Class Method Details

.from_file(file) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/madman/document.rb', line 11

def self.from_file(file)
  if File.extname(file) == '.yml'
    result = new YAML.load_file(file), file
    result.yaml_mode = true
    result
  else
    new File.read(file), file
  end
end

Instance Method Details

#render(renderer = :default) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/madman/document.rb', line 26

def render(renderer=:default)
  if yaml_mode
    renderers[:yaml].render text, title: File.basename(filename, '.yml')
  else
    renderers[renderer].render text
  end
end

#rtl?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/madman/document.rb', line 34

def rtl?
  detector.direction(text[0..200]) == 'rtl'
end

#save(save_as = nil) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
# File 'lib/madman/document.rb', line 38

def save(save_as=nil)
  save_as ||= filename
  raise ArgumentError, "No filename provided" unless save_as
  File.write save_as, text
end