Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/curly/array.rb
Overview
Array helpers
Instance Method Summary collapse
-
#clean ⇒ Array
Remove extra spaces from each element of an array of strings.
- #clean! ⇒ Object
-
#clean_output ⇒ Array
Clean up output, shrink single-item arrays, ensure array output.
-
#dedup_links ⇒ Array
Remove duplicate links from an array of link objects.
-
#dedup_links! ⇒ Object
Destructive version of #dedup_links.
-
#dot_query(path) ⇒ Array
Run a query on array elements.
-
#ensure_array ⇒ Array
Ensure that an object is an array.
-
#get_value(path) ⇒ Array
Gets the value of every item in the array.
-
#strip_tags ⇒ Array
Strip HTML tags from each element of an array of strings.
-
#strip_tags! ⇒ Object
Destructive version of #strip_tags.
-
#tag_match(tag_name, classes, id, attribute, operator, value, descendant: false) ⇒ Boolean
Test if a tag contains an attribute matching filter queries.
-
#to_html ⇒ String
Convert every item in the array to HTML.
Instance Method Details
#clean ⇒ Array
Remove extra spaces from each element of an array of strings
11 12 13 |
# File 'lib/curly/array.rb', line 11 def clean map(&:clean) end |
#clean! ⇒ Object
18 19 20 |
# File 'lib/curly/array.rb', line 18 def clean! replace clean end |
#clean_output ⇒ Array
Clean up output, shrink single-item arrays, ensure array output
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 |
#dedup_links ⇒ Array
Remove duplicate links from an 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
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
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_array ⇒ Array
Ensure that an object is an 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
90 91 92 |
# File 'lib/curly/array.rb', line 90 def get_value(path) map { |el| el.get_value(path) } end |
#strip_tags ⇒ Array
Strip HTML tags from each element of an array of strings
28 29 30 |
# File 'lib/curly/array.rb', line 28 def map(&:strip_tags) end |
#strip_tags! ⇒ Object
Destructive version of #strip_tags
37 38 39 |
# File 'lib/curly/array.rb', line 37 def replace end |
#tag_match(tag_name, classes, id, attribute, operator, value, descendant: false) ⇒ Boolean
Test if a tag contains an attribute matching filter queries
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') = tag['tags'].filter { |t| t.tag_match(tag_name, classes, id, attribute, operator, value) } .count.positive? else keep end end |
#to_html ⇒ String
Convert every item in the array to HTML
99 100 101 |
# File 'lib/curly/array.rb', line 99 def to_html map(&:to_html) end |