Module: FrozenFilters

Defined in:
lib/frozen-filters.rb

Instance Method Summary collapse

Instance Method Details

#array_head(input, p) ⇒ Object

Returns the first N elements of an array. e.g. ‘[“first”,“second”,“third”] | array_head: 2 }` =~ `[“first”,“second”]`. If the number of parameters is negative it returns an empty array. The the input isn’t an array it returns the untouched input.



59
60
61
# File 'lib/frozen-filters.rb', line 59

def array_head(input, p)
  input.kind_of?(Array) ? input.take([0, p.to_i].max) : input
end

#array_tail(input, p) ⇒ Object

Returns the last N elements of an array. e.g. ‘[“first”,“second”,“third”] | array_tail: 2 }` =~ `[“second”,“third”]`. If the number of parameters is negative it returns an empty array. The the input isn’t an array it returns the untouched input.



67
68
69
# File 'lib/frozen-filters.rb', line 67

def array_tail(input, p)
  input.kind_of?(Array) ? input.drop([0, input.length - p.to_i].max) : input
end

#array_to_taglist(input, p) ⇒ Object

Transforms an array into an enclose html tag list separated by newline. e.g. ‘[“first”,“second” | array_to_taglist: “li” }` =~ “`html <li>first</li> <li>second</li> “` The the input isn’t an array it returns the untouched input.



78
79
80
81
82
83
84
85
86
# File 'lib/frozen-filters.rb', line 78

def array_to_taglist(input, p)
  if input.kind_of?(Array) && p.kind_of?(String)
    startTag = "<" + p + ">"
    endTag = "</" + p + ">"
    input.length != 0 ? startTag + input.join(endTag + "\n" + startTag) + endTag : ""
  else
    input
  end
end

#extract_basename(input) ⇒ Object

Returns the basename of an url. e.g. ‘index.html`.



24
25
26
# File 'lib/frozen-filters.rb', line 24

def extract_basename(input)
  input.to_s.gsub(/^.*\/([^\/\?]+).*$/, '\1')
end

#extract_dirname(input) ⇒ Object

Returns the dirname of an url. e.g. ‘/first/second`.



29
30
31
32
# File 'lib/frozen-filters.rb', line 29

def extract_dirname(input)
  result = extract_path(input).gsub(/\/[^\/]+$/, '')
  result != "" ? result : "/"
end

#extract_path(input) ⇒ Object

Returns the path of an url. e.g. ‘/first/second/index.html`.



35
36
37
38
39
40
41
42
# File 'lib/frozen-filters.rb', line 35

def extract_path(input)
  input_s = input.to_s
  if /^(\w+):/.match(input_s)
    input_s.gsub(/^\w+:[^\/]*\/\/[^\/]+(\/[^\?]+)(?:\?.*)?$/, '\1')
  else
    input_s.gsub(/(?:\?.*)$/, '')
  end
end

#extract_protocol(input) ⇒ Object

Returns the protocol. e.g. ‘http`.



45
46
47
48
# File 'lib/frozen-filters.rb', line 45

def extract_protocol(input)
  matches = input.to_s.match(/^(\w+):/)
  matches ? matches[1] : ""
end

#extract_qs(input) ⇒ Object

Returns the query string part. e.g. ‘param1=value1&param2=value2`.



51
52
53
# File 'lib/frozen-filters.rb', line 51

def extract_qs(input)
  input.to_s.gsub(/^[^\?]*\??/, '\1')
end

#remove_ext(input) ⇒ Object

Removes the extension part of an url. e.g. ‘www.example.com/first/second/index?param1=value1&param2=value2`.



14
15
16
# File 'lib/frozen-filters.rb', line 14

def remove_ext(input)
  input.to_s.gsub(/\.[^\.\?]+(\?[^\?]*)?$/, '\1')
end

#remove_qs(input) ⇒ Object

Removes the query string part of an url. e.g. ‘www.example.com/first/second/index.html`.



19
20
21
# File 'lib/frozen-filters.rb', line 19

def remove_qs(input)
  input.to_s.gsub(/\?[^\?]+$/, '')
end