Class: Gimli::File
- Inherits:
-
Object
- Object
- Gimli::File
- Defined in:
- lib/gimli/file.rb
Overview
Class used to load files and determine if they are valid
Constant Summary collapse
- FORMATS =
Accepted formats
[:markdown, :textile, :rdoc, :org, :creole, :rest, :asciidoc, :pod, :roff, :mediawiki]
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(filename) ⇒ File
constructor
Initializes the file object.
-
#load_format(format) ⇒ Symbol|nil
Converts the format to a symbol if it’s a valid format nil otherwise.
-
#valid? ⇒ Boolean
Is the file valid.
-
#valid_format?(format) ⇒ Boolean
Checks if the format is a valid one.
Constructor Details
#initialize(filename) ⇒ File
Initializes the file object. Only reads contents if it’s a valid file
13 14 15 16 17 18 19 |
# File 'lib/gimli/file.rb', line 13 def initialize(filename) @filename = filename extension = ::File.extname(@filename) @format = load_format(extension) @name = ::File.basename(@filename, extension) @data = ::File.open(@filename, 'rb') { |f| f.read } if valid? && ::File.exists?(@filename) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/gimli/file.rb', line 7 def data @data end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
7 8 9 |
# File 'lib/gimli/file.rb', line 7 def filename @filename end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
7 8 9 |
# File 'lib/gimli/file.rb', line 7 def format @format end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/gimli/file.rb', line 7 def name @name end |
Instance Method Details
#load_format(format) ⇒ Symbol|nil
Converts the format to a symbol if it’s a valid format nil otherwise
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gimli/file.rb', line 30 def load_format(format) case format when /(md|mkdn?|mdown|markdown)$/i :markdown when /(textile)$/i :textile when /(rdoc)$/i :rdoc when /(org)$/i :org when /(creole)$/i :creole when /(re?st(\.txt)?)$/i :rest when /(asciidoc)$/i :asciidoc when /(pod)$/i :pod when /(\d)$/i :roff when /(media)?wiki$/i :mediawiki else nil end end |
#valid? ⇒ Boolean
Is the file valid
23 24 25 |
# File 'lib/gimli/file.rb', line 23 def valid? valid_format? @format end |
#valid_format?(format) ⇒ Boolean
Checks if the format is a valid one
60 61 62 63 64 |
# File 'lib/gimli/file.rb', line 60 def valid_format?(format) return false if format.nil? FORMATS.include? format.to_sym end |