Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/curly/array.rb

Overview

Array helpers

Instance Method Summary collapse

Instance Method Details

#cleanArray

Remove extra spaces from each element of an array of strings

Returns:

  • (Array)

    cleaned array



11
12
13
# File 'lib/curly/array.rb', line 11

def clean
  map(&:clean)
end

#clean!Object

See Also:



18
19
20
# File 'lib/curly/array.rb', line 18

def clean!
  replace clean
end

#clean_outputArray

Clean up output, shrink single-item arrays, ensure array output

Returns:

  • (Array)

    cleaned up array



178
179
180
181
182
183
184
# File 'lib/curly/array.rb', line 178

def clean_output
  output = dup
  while output.is_a?(Array) && output.count == 1
    output = output[0]
  end
  output.ensure_array
end

Remove duplicate links from an array of link objects

Returns:

  • (Array)

    deduped array of link objects



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/curly/array.rb', line 46

def dedup_links
  used = []
  good = []
  each do |link|
    href = link[:href].sub(%r{/$}, '')
    next if used.include?(href)

    used.push(href)
    good.push(link)
  end

  good
end

#dedup_links!Object

Destructive version of #dedup_links

See Also:



65
66
67
# File 'lib/curly/array.rb', line 65

def dedup_links!
  replace dedup_links
end

#dot_query(path) ⇒ Array

Run a query on array elements

Parameters:

  • path (String)

    dot.syntax path to compare

Returns:

  • (Array)

    elements matching dot query



76
77
78
79
80
81
# File 'lib/curly/array.rb', line 76

def dot_query(path)
  res = map { |el| el.dot_query(path) }
  res.delete_if { |r| !r }
  res.delete_if(&:empty?)
  res
end

#ensure_arrayArray

Ensure that an object is an array

Returns:

  • (Array)

    object as Array



191
192
193
# File 'lib/curly/array.rb', line 191

def ensure_array
  return self
end

#get_value(path) ⇒ Array

Gets the value of every item in the array

Parameters:

  • path

    The query path (dot syntax)

Returns:

  • (Array)

    array of values



90
91
92
# File 'lib/curly/array.rb', line 90

def get_value(path)
  map { |el| el.get_value(path) }
end

#strip_tagsArray

Strip HTML tags from each element of an array of strings

Returns:

  • (Array)

    array of strings with HTML tags removed



28
29
30
# File 'lib/curly/array.rb', line 28

def strip_tags
  map(&:strip_tags)
end

#strip_tags!Object

Destructive version of #strip_tags

See Also:



37
38
39
# File 'lib/curly/array.rb', line 37

def strip_tags!
  replace strip_tags
end

#tag_match(tag_name, classes, id, attribute, operator, value, descendant: false) ⇒ Boolean

Test if a tag contains an attribute matching filter queries

Parameters:

  • tag_name (String)

    The tag name

  • classes (String)

    The classes to match

  • id (String)

    The id attribute to match

  • attribute (String)

    The attribute

  • operator (String)

    The operator, <>= *= $= ^=

  • value (String)

    The value to match

  • descendant (Boolean) (defaults to: false)

    Check descendant tags

Returns:

  • (Boolean)

    tag matches



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/curly/array.rb', line 119

def tag_match(tag_name, classes, id, attribute, operator, value, descendant: false)
  tag = self
  keep = true

  keep = false if tag_name && !tag['tag'] =~ /^#{tag_name}$/i

  if tag.key?('attrs') && tag['attrs']
    if keep && id
      tag_id = tag['attrs'].filter { |a| a['key'] == 'id' }.first['value']
      keep = tag_id && tag_id =~ /#{id}/i
    end

    if keep && classes
      cls = tag['attrs'].filter { |a| a['key'] == 'class' }.first
      if cls
        all = true
        classes.each { |c| all = cls['value'].include?(c) }
        keep = all
      else
        keep = false
      end
    end

    if keep && attribute
      attributes = tag['attrs'].filter { |a| a['key'] =~ /^#{attribute}$/i }
      any = false
      attributes.each do |a|
        break if any

        any = case operator
              when /^*/
                a['value'] =~ /#{value}/i
              when /^\^/
                a['value'] =~ /^#{value}/i
              when /^\$/
                a['value'] =~ /#{value}$/i
              else
                a['value'] =~ /^#{value}$/i
              end
      end
      keep = any
    end
  end

  return false if descendant && !keep

  if !descendant && tag.key?('tags')
    tags = tag['tags'].filter { |t| t.tag_match(tag_name, classes, id, attribute, operator, value) }
    tags.count.positive?
  else
    keep
  end
end

#to_htmlString

Convert every item in the array to HTML

Returns:

  • (String)

    Html representation of the object.



99
100
101
# File 'lib/curly/array.rb', line 99

def to_html
  map(&:to_html)
end