Module: HamlLint::Utils

Defined in:
lib/haml_lint/utils.rb

Overview

A miscellaneous set of utility functions.

Class Method Summary collapse

Class Method Details

.camel_case(str) ⇒ Object

Converts a string containing underscores/hyphens/spaces into CamelCase.


20
21
22
# File 'lib/haml_lint/utils.rb', line 20

def camel_case(str)
  str.split(/_|-| /).map { |part| part.sub(/^\w/) { |c| c.upcase } }.join
end

.count_consecutive(items, offset, satisfies) ⇒ Object

Count the number of consecutive items satisfying the given Proc.

Parameters:

  • items (Array)
  • offset (Fixnum)

    index to start searching

  • satisfies (Proc)

    function to evaluate item with


52
53
54
55
56
# File 'lib/haml_lint/utils.rb', line 52

def count_consecutive(items, offset, satisfies)
  count = 1
  count += 1 while (offset + count < items.count) && satisfies[items[offset + count]]
  count
end

.extract_interpolated_values(filter_text) ⇒ Object

Yields interpolated values within a block of filter text.


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/haml_lint/utils.rb', line 7

def extract_interpolated_values(filter_text)
  Haml::Util.handle_interpolation(filter_text.dump) do |scan|
    escape_count = (scan[2].size - 1) / 2
    return unless escape_count.even?

    dumped_interpolated_str = Haml::Util.balance(scan, '{', '}', 1)[0][0...-1]

    # Hacky way to turn a dumped string back into a regular string
    yield eval('"' + dumped_interpolated_str + '"') # rubocop:disable Eval
  end
end

.find_consecutive(items, min_size, satisfies) ⇒ Object

Find all consecutive nodes satisfying the given Proc of a minimum size and yield each group.

Parameters:

  • items (Array)
  • min_size (Fixnum)

    minimum number of consecutive items before yielding

  • satisfies (Proc)

    function that takes an item and returns true/false


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/haml_lint/utils.rb', line 31

def find_consecutive(items, min_size, satisfies)
  current = -1

  while (current += 1) < items.count
    next unless satisfies[items[current]]

    count = count_consecutive(items, current, satisfies)
    next unless count >= min_size

    # Yield the chunk of consecutive items
    yield items[current...(current + count)]

    current += count # Skip this patch of consecutive items to find more
  end
end