Class: Lazy::Checker::CheckedTag

Inherits:
Object
  • Object
show all
Defined in:
lib/lazy/check/checked_tag.rb

Defined Under Namespace

Classes: CheckCaseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ CheckedTag

Instanciation



12
13
14
15
16
17
# File 'lib/lazy/check/checked_tag.rb', line 12

def initialize(data)
  @data = data
  parse_tag
  check_data
  @error = nil
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/lazy/check/checked_tag.rb', line 7

def data
  @data
end

Instance Method Details

#attributesObject



304
# File 'lib/lazy/check/checked_tag.rb', line 304

def attributes  ; data[:attrs]      end

#check_attributes_of(found) ⇒ Object

– – Vérifie que found contienne bien les attributs – attendu par le check –



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/lazy/check/checked_tag.rb', line 223

def check_attributes_of(found)
  if must_have_attributes? 
    missing_attrs = found.attributes?(attributes)
    if missing_attrs.empty?
      return true # ne sert pas vraiment
    else
      # Il y a des attributs manquants
      _raise(5031, missing_attrs.inspect)
    end
  end
end

#check_containess_of(found) ⇒ Object



210
211
212
213
214
215
216
217
# File 'lib/lazy/check/checked_tag.rb', line 210

def check_containess_of(found)
  if must_have_text? && not(found.match?(text))
    _raise(5011, text.inspect)
  end
  if must_have_content? && not(found.contains?(contains))
    _raise(5010, data[:contains].inspect, found.errors.pretty_join)
  end
end

#check_emptiness_of(found) ⇒ Object

Note:

Il existe deux sorte d’emptiness :

  1. l’absence de texte (mais le node peut contenir d’autres nodes)

  2. la vrai emptiness quand le node ne contient vraiment rien

– – Check précis d’un node (Nokogiri::XML::Element) trouvé – correspond à la recherche –



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/lazy/check/checked_tag.rb', line 196

def check_emptiness_of(found)
  # puts "must_be_empty? #{must_be_empty?.inspect}".jaune
  # puts "found.empty? #{found.empty?.inspect}".bleu
  if must_be_empty? && not(found.empty?)
    _raise(5001, nil, found.content)
  elsif must_not_be_empty? && found.empty?
    _raise(5002)
  elsif must_have_no_text? && (found.has_text?)
    _raise(5003, nil, found.text)
  elsif must_have_text? && (found.has_no_text?)
    _raise(5004)
  end
end

#check_lengths_of(found) ⇒ Object

– – Vérifie la longueur du contenu si :max_length ou – :min_length ont été définis –



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/lazy/check/checked_tag.rb', line 239

def check_lengths_of(found)
  if must_have_lengths?
    len = found.length.freeze
    if min_length && len < min_length
      _raise(5032, min_length, len)
    end
    if max_length && len > max_length
      _raise(5033, max_length, len)
    end
  end
end

#child_has_css?(child_css, csss) ⇒ Boolean

Returns true si les child_css contiennent toutes les css.

Returns:

  • (Boolean)

    true si les child_css contiennent toutes les css



253
254
255
256
257
# File 'lib/lazy/check/checked_tag.rb', line 253

def child_has_css?(child_css, csss)
  csss.each do |css|
    return false if not(child_css.include?(css))
  end
end

#containsObject



301
# File 'lib/lazy/check/checked_tag.rb', line 301

def contains    ; data[:contains]   end

#countObject



298
# File 'lib/lazy/check/checked_tag.rb', line 298

def count       ; data[:count]      end

#cssObject



295
# File 'lib/lazy/check/checked_tag.rb', line 295

def css         ; @css              end

#direct_child_only?Boolean

– Predicate Methods –

Returns:

  • (Boolean)


262
263
264
# File 'lib/lazy/check/checked_tag.rb', line 262

def direct_child_only?
  :TRUE == @directchild ||= true_or_false((data[:direct_child_only]||data[:direct_child]) === true)    
end

#emptyObject



299
# File 'lib/lazy/check/checked_tag.rb', line 299

def empty       ; data[:empty]      end

#errorsObject

Returns les erreurs rencontrées.

Returns:

  • les erreurs rencontrées



20
21
22
23
24
25
26
27
# File 'lib/lazy/check/checked_tag.rb', line 20

def errors
  err = []
  # err << "Erreur avec #{data.inspect}" # Pour obtenir précisément les données
  err << @error.strip unless @error.nil?
  err << @errors.join("\n") unless @errors.empty?
  err << @sub_errors.join("\n") unless @sub_errors.empty?
  return err.join("\n")
end

#formated_dataString

succès (et peut-être même à l’avenir un message de failure)

Returns:

  • (String)

    les données formatées pour un message de



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/lazy/check/checked_tag.rb', line 35

def formated_data
  ds = []
  ds << "#{MESSAGES[10]}: #{count}" if count
  ds << "#{MESSAGES[12]}: #{min_length}" if min_length
  ds << "#{MESSAGES[13]}: #{max_length}" if max_length
  ds << "#{MESSAGES[20]}" if direct_child_only?
  ds << "#{MESSAGES[15]}" if must_not_be_empty?
  ds << "#{MESSAGES[14]}" if must_be_empty?
  ds << "#{MESSAGES[16]} #{contains}" if must_have_content?
  ds << "#{MESSAGES[17]} #{text.inspect}" if must_have_text?
  ds << "#{MESSAGES[21]} #{attributes.inspect}" if must_have_attributes?
  #
  # On les répartit sur plusieurs lignes
  # 
  max_line = 70
  lines = []
  line  = []
  while seg = ds.shift
    curline = line.join(', ')
    if curline.length + seg.length > max_line
      lines << curline
      line = []
    end
    line << seg
  end
  lines << line.join(', ') unless line.empty?
  lines.join("\n")
end

#idObject



294
# File 'lib/lazy/check/checked_tag.rb', line 294

def id          ; @id               end

#is_in?(noko) ⇒ Boolean

main=

Nokogiri noko

Parameters:

  • noko (Nokogiri::XML::Element)

    L’élément qui doit contenir le checked-tag courant

Returns:

  • (Boolean)

    true si le checked-tag se trouve dans le XML::Element



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
172
173
174
175
176
177
178
179
# File 'lib/lazy/check/checked_tag.rb', line 73

def is_in?(noko)

  # puts "is_in? avec noko : #{noko.inspect}".bleu
  #
  # Array dans lequel seront placés tous les candidats, jusqu'aux
  # derniers
  # 
  founds = []

  #
  # Pour mettre les erreurs
  # 

  @errors = []

  #
  # Traitement différent en fonction du fait qu'il s'agisse d'un
  # document Nokogiri ou d'un XML::Element Nokogiri
  # 
  if noko.document?
    # 
    # <= L'élément envoyé est un document Nokogiri
    #    Nokogiri::HTML4/5::Document
    # 
    if direct_child_only?
      noko.children.first.children.each do |child|
        founds << child if tagname_id_class_ok?(child)
      end
    else       
      founds = noko.css("//#{data[:tag]}")
    end
  elsif direct_child_only?
    #
    # Si on ne doit pas traverser toutes les générations d'éléments
    # 
    noko.children.each do |child|
      founds << child if tagname_id_class_ok?(child)
    end
  else
    #
    # Si on peut traverser toutes les générations d'éléments
    # 
    #
    # <= L'élément envoyé est un node (un XML::Element)
    # => On doit chercher dans les enfants
    # 
    noko.traverse_children do |child|
      # puts "child: #{child.inspect}".bleu
      founds << child if tagname_id_class_ok?(child)
    end
  end

  #
  # Pour mettre toutes les sous-erreurs rencontrées
  # 
  @sub_errors = []

  # On boucle sur les éléments trouvés.
  # 
  # Noter que +founds+ peut être vide, mais peu importe, on 
  # passera simplement la boucle. C'est pour ne pas répéter le
  # code en [1]
  founds = founds.select do |found|
    begin
      check_emptiness_of(found)
      check_containess_of(found)
      check_attributes_of(found)
      check_lengths_of(found)
      # S'il n'a pas raisé jusque-là, c'est qu'il est bon
      true
    rescue CheckCaseError => e
      @sub_errors << e.message
      false
    end
  end

  # [1] Si aucun élément n'a passé le test
  if founds.empty?
    if count === 0
      return true
    else
      _raise(4999, count || 'un nombre indéfini')
    end
  end

  founds_count = founds.count
  # puts "Il en reste : #{founds_count}".bleu

  # 
  # Propriété :count
  # 
  if must_have_count? && ( founds_count != count )
    _raise(5000, count, founds_count, :count)
  end


  # 
  # Sinon, c'est un plein succès
  # 


rescue CheckCaseError => e
  @error = e.message
  return false
else
  return true
end

#max_lengthObject



302
# File 'lib/lazy/check/checked_tag.rb', line 302

def max_length  ; data[:max_length] end

#messageObject



29
30
31
# File 'lib/lazy/check/checked_tag.rb', line 29

def message
  MESSAGES[4999] % {tag: tag, f_data: formated_data}
end

#min_lengthObject



303
# File 'lib/lazy/check/checked_tag.rb', line 303

def min_length  ; data[:min_length] end

#must_be_empty?Boolean

Returns:

  • (Boolean)


271
272
273
# File 'lib/lazy/check/checked_tag.rb', line 271

def must_be_empty?
  :TRUE == @mbempty ||= true_or_false(empty === true)
end

#must_have_attributes?Boolean

Returns:

  • (Boolean)


283
284
285
# File 'lib/lazy/check/checked_tag.rb', line 283

def must_have_attributes?
  :TRUE == @mhattrs ||= true_or_false(not(attributes.nil?))
end

#must_have_content?Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/lazy/check/checked_tag.rb', line 277

def must_have_content?
  :TRUE == @mhcontent ||= true_or_false(not(contains.nil?))
end

#must_have_count?Boolean

Returns:

  • (Boolean)


280
281
282
# File 'lib/lazy/check/checked_tag.rb', line 280

def must_have_count?
  :TRUE == @mhcount ||= true_or_false(not(count.nil?))
end

#must_have_lengths?Boolean

Returns:

  • (Boolean)


286
287
288
# File 'lib/lazy/check/checked_tag.rb', line 286

def must_have_lengths?
  :TRUE == @mhlens ||= true_or_false(not(min_length.nil? && max_length.nil?))
end

#must_have_no_text?Boolean

Returns:

  • (Boolean)


268
269
270
# File 'lib/lazy/check/checked_tag.rb', line 268

def must_have_no_text?
  :TRUE == @mhnotxt ||= true_or_false(notext === true)
end

#must_have_text?Boolean

Returns:

  • (Boolean)


265
266
267
# File 'lib/lazy/check/checked_tag.rb', line 265

def must_have_text?
  :TRUE == @mhtxt ||= true_or_false(notext === false || not(text.nil?))
end

#must_not_be_empty?Boolean

Returns:

  • (Boolean)


274
275
276
# File 'lib/lazy/check/checked_tag.rb', line 274

def must_not_be_empty?
  :TRUE == @mnbempty ||= true_or_false(empty === false)
end

#nameObject

– Data Methods –



292
# File 'lib/lazy/check/checked_tag.rb', line 292

def name        ; data[:name]       end

#notextObject



300
# File 'lib/lazy/check/checked_tag.rb', line 300

def notext      ; data[:notext]     end

#tagObject



296
# File 'lib/lazy/check/checked_tag.rb', line 296

def tag         ; data[:tag]        end

#tag_nameObject



293
# File 'lib/lazy/check/checked_tag.rb', line 293

def tag_name    ; @tag_name         end

#tagname_id_class_ok?(child) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
184
185
186
# File 'lib/lazy/check/checked_tag.rb', line 181

def tagname_id_class_ok?(child)
  name_ok = child.node_name == tag_name
  id_ok   = id.nil? ? true : child.id == id
  css_ok  = css.nil? ? true : child_has_css?(child.classes, css)
  return name_ok && id_ok && css_ok
end

#textObject



297
# File 'lib/lazy/check/checked_tag.rb', line 297

def text        ; data[:text]       end