Module: ScribeDown::Res
- Defined in:
- lib/res/resources.rb
Class Method Summary collapse
- .create_file(path, contents = '') ⇒ Object
- .csv_contents(contents) ⇒ Object
- .erb_contents(contents, bind) ⇒ Object
- .markdown_contents(content) ⇒ Object
- .read_file(file_name, options = {}) ⇒ Object
- .read_res(res_name) ⇒ Object
- .root(file = nil) ⇒ Object
- .symbolize(hash) ⇒ Object
- .yaml_contents(contents) ⇒ Object
Class Method Details
.create_file(path, contents = '') ⇒ Object
83 84 85 86 87 |
# File 'lib/res/resources.rb', line 83 def self.create_file(path, contents='') File.open(path, 'w') do |file| file.write(contents) end end |
.csv_contents(contents) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/res/resources.rb', line 64 def self.csv_contents(contents) all = CSV.parse(contents) headers = all[0] return if headers == nil data = all[1..-1] data_classes = headers.map {|h| h.gsub(/\W+/, '-') } read_file('csv_template.html.erb', binding: binding) end |
.erb_contents(contents, bind) ⇒ Object
54 55 56 |
# File 'lib/res/resources.rb', line 54 def self.erb_contents(contents, bind) ERB.new(contents).result bind end |
.markdown_contents(content) ⇒ Object
50 51 52 |
# File 'lib/res/resources.rb', line 50 def self.markdown_contents(content) Kramdown::Document.new(content, :auto_ids => true).to_html end |
.read_file(file_name, options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/res/resources.rb', line 20 def self.read_file(file_name, ={}) binding = [:binding] format = [:format] only_in_fs = [:in_fs] if File.exist? file_name name = file_name elsif File.exist?(root('resources/' + file_name)) && !only_in_fs name = root('resources/' + file_name) else raise "File or resource does not exist: #{file_name}" end contents = File.open(name).read() if format == :plain return contents end if binding && (name.end_with?('.erb') || format == :erb) name = name.chomp('.erb') contents = erb_contents(contents, binding) end if name.end_with?('.md') || name.end_with?('.markdown') || format == :markdown contents = markdown_contents(contents) end if name.end_with?('.csv') || format == :csv contents = csv_contents(contents) end return contents end |
.read_res(res_name) ⇒ Object
16 17 18 |
# File 'lib/res/resources.rb', line 16 def self.read_res(res_name) File.open(self.root + '/resources/' + res_name).read() end |
.root(file = nil) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/res/resources.rb', line 8 def self.root(file=nil) path = File. '../..', File.dirname(__FILE__) if file path += '/' + file end return path end |
.symbolize(hash) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/res/resources.rb', line 74 def self.symbolize(hash) hash.default_proc = proc do |h, k| case k when String then sym = k.to_sym; h[sym] if h.key?(sym) when Symbol then str = k.to_s; h[str] if h.key?(str) end end end |
.yaml_contents(contents) ⇒ Object
58 59 60 61 62 |
# File 'lib/res/resources.rb', line 58 def self.yaml_contents(contents) res = YAML.load(contents) symbolize(res) return res end |