Module: Mint::Helpers

Defined in:
lib/mint/helpers.rb

Class Method Summary collapse

Class Method Details

.generate_temp_file!(file) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mint/helpers.rb', line 141

def self.generate_temp_file!(file)
  basename  = File.basename file
  extension = File.extname file
  content   = File.read file

  tempfile = Tempfile.new([basename, extension])
  tempfile << content
  tempfile.flush
  tempfile.close
  tempfile.path
end

.hashify(list1, list2) ⇒ Object



106
107
108
# File 'lib/mint/helpers.rb', line 106

def self.hashify(list1, list2)
  Hash[*list1.zip(list2).flatten]
end

.listify(list) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/mint/helpers.rb', line 74

def self.listify(list)
  if list.length > 2
    list[0..-2].join(", ") + " & " + list.last
  else
    list.join(" & ")
  end
end

.normalize_path(to_directory, from_directory) ⇒ Pathname

Returns the relative path to to_directory from from_directory. If to_directory and from_directory have no parents in common besides /, returns the absolute directory of to_directory. Assumes no symlinks.

Parameters:

  • to_directory (String, Pathname)

    the target directory

  • from_directory (String, Pathname)

    the starting directory

Returns:

  • (Pathname)

    the relative path to to_directory from from_directory, or an absolute path if they have no parents in common other than /



119
120
121
122
123
124
125
# File 'lib/mint/helpers.rb', line 119

def self.normalize_path(to_directory, from_directory)
  to_path, from_path = [to_directory, from_directory].map {|d| pathize d }
  to_root, from_root = [to_path, from_path].map {|p| p.each_filename.first }
  to_root == from_root ? 
    to_path.relative_path_from(from_path) :
    to_path
end

.pathize(str_or_path) ⇒ Pathname

Transforms a String or Pathname into a fully expanded Pathname.

Parameters:

  • str_or_path (String, Pathname)

    a path to be expanded

Returns:

  • (Pathname)

    an expanded representation of str_or_path



46
47
48
49
50
51
52
53
# File 'lib/mint/helpers.rb', line 46

def self.pathize(str_or_path)
  case str_or_path
  when String
    Pathname.new str_or_path
  when Pathname
    str_or_path
  end.expand_path
end

.slugize(obj) ⇒ String

Transforms a String into a URL-ready slug. Properly handles ampersands, non-alphanumeric characters, extra hyphens and spaces.

Parameters:

  • obj (String, #to_s)

    an object to be turned into a slug

Returns:

  • (String)

    a URL-ready slug



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

def self.slugize(obj)
  obj.to_s.downcase.
    gsub(/&/, "and").
    gsub(/[\s-]+/, "-").
    gsub(/[^a-z0-9-]/, "").
    gsub(/[-]+/, "-")
end

.standardize(metadata, opts = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mint/helpers.rb', line 82

def self.standardize(, opts={})
  table = opts[:table] || {}
  .reduce({}) do |hash, (key,value)|
    if table[key] && table[key].length == 2
      standard_key, standard_type = table[key]
      standard_value =
        case standard_type
        when :array
          [*value]
        when :string
          value
        else
          # If key/type were not in table
          value
        end

      hash[standard_key] = standard_value
    else
      hash[key] = value
    end
    hash
  end
end

.symbolize(obj) ⇒ Symbol

Transforms a potentially hyphenated String into a symbol name.

Parameters:

  • obj (String, #to_s)

    an object to be turned into a symbol name

Returns:

  • (Symbol)

    a symbol representation of obj



38
39
40
# File 'lib/mint/helpers.rb', line 38

def self.symbolize(obj)
  slugize(obj).gsub(/-/, "_").to_sym
end

.symbolize_keys(map, opts = {}) ⇒ Hash

Recursively transforms all keys in a Hash into Symbols.

Parameters:

  • map (Hash, #[])

    a potentially nested Hash containing symbolizable keys

Returns:

  • (Hash)

    a version of map where all keys are symbols



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mint/helpers.rb', line 59

def self.symbolize_keys(map, opts={})
  transform = lambda {|x| opts[:downcase] ? x.downcase : x }

  map.reduce(Hash.new) do |syms,(k,v)| 
    syms[transform[k].to_sym] = 
      case v
      when Hash
        self.symbolize_keys(v, opts)
      else
        v
      end
    syms
  end
end

.underscore(obj, opts = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mint/helpers.rb', line 8

def self.underscore(obj, opts={})
  namespaces = obj.to_s.split("::").map do |namespace|
    if opts[:ignore_prefix]
      namespace[0..1].downcase + namespace[2..-1]
    else
      namespace
    end
  end

  string = opts[:namespaces] ? namespaces.join("::") : namespaces.last
  string.underscore
end

.update_yaml!(file, opts = {}) ⇒ void

This method returns an undefined value.

Reads Yaml options from file. Updates values with new_opts. Writes merged data back to the same file, overwriting previous data.

Parameters:

  • new_opts (Hash, #[])

    a set of options to add to the Yaml file

  • file (Pathname, #exist)

    a file to read from and write to



133
134
135
136
137
138
139
# File 'lib/mint/helpers.rb', line 133

def self.update_yaml!(file, opts={})
  curr_opts = File.exist?(file) ? YAML.load_file(file) : {}

  File.open file, "w" do |f|
    YAML.dump(curr_opts.merge(opts), f)
  end
end