Module: Aperitiiif::Utils

Defined in:
lib/aperitiiif/utils.rb

Overview

TO DO COMMENT

Class Method Summary collapse

Class Method Details

.basename_no_ext(str) ⇒ Object



9
# File 'lib/aperitiiif/utils.rb', line 9

def self.basename_no_ext(str)   = rm_ext File.basename(str)

.csv_records(file, defaults = {}) ⇒ Object



38
39
40
41
# File 'lib/aperitiiif/utils.rb', line 38

def self.csv_records(file, defaults = {})
  hashes = CSV.read(file, headers: true).map(&:to_hash)
  hashes.map { |hash| Record.new hash.compact, defaults }
end

.formatted_datetime(time = Time.now) ⇒ Object



25
26
27
28
29
# File 'lib/aperitiiif/utils.rb', line 25

def self.formatted_datetime(time = Time.now)
  fmt_date  = "#{time.month}/#{time.day}/#{time.year}"
  fmt_time  = "#{time.hour}:#{time.min.to_s.rjust(2, '0')}#{time.zone}"
  "#{fmt_date} at #{fmt_time}"
end

.item_assets_from_path(path) ⇒ Object



31
32
33
34
35
36
# File 'lib/aperitiiif/utils.rb', line 31

def self.item_assets_from_path(path)
  return [path] if Utils.valid_source?(path)
  return [] unless File.directory? path

  Dir.glob("#{path}/*").select { |sub| Utils.valid_source? sub }
end

.mime(path) ⇒ Object



14
# File 'lib/aperitiiif/utils.rb', line 14

def self.mime(path) = MimeMagic.by_magic(File.open(path)).to_s

.mkfile_p(path, contents) ⇒ Object



43
44
45
46
# File 'lib/aperitiiif/utils.rb', line 43

def self.mkfile_p(path, contents)
  FileUtils.mkdir_p File.dirname(path)
  File.open(path, 'w') { |file| file.write contents }
end

.parent_dir(str) ⇒ Object



12
# File 'lib/aperitiiif/utils.rb', line 12

def self.parent_dir(str)        = File.dirname(str).split('/').last

.parent_id(str, dir) ⇒ Object



11
# File 'lib/aperitiiif/utils.rb', line 11

def self.parent_id(str, dir)    = prune_prefix_junk(rm_ext(str.sub(dir, '')))

.prune_prefix_junk(str) ⇒ Object



13
# File 'lib/aperitiiif/utils.rb', line 13

def self.prune_prefix_junk(str) = str.sub(/^(_|\W+)/, '')

.rm_ext(str) ⇒ Object



10
# File 'lib/aperitiiif/utils.rb', line 10

def self.rm_ext(str)            = str.sub File.extname(str), ''

.slugify(str) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/aperitiiif/utils.rb', line 17

def self.slugify(str)
  str.downcase!
  str.strip!
  str.gsub! ' ', '-'
  str.gsub!(/[^\w-]/, '')
  prune_prefix_junk str
end

.valid_source?(path) ⇒ Boolean

Returns:

  • (Boolean)


15
# File 'lib/aperitiiif/utils.rb', line 15

def self.valid_source?(path) = File.file?(path) && path.end_with?(*ALLOWED_SRC_FORMATS)