Class: Habaki::Declarations

Inherits:
NodeArray
  • Object
show all
Includes:
Shorthand
Defined in:
lib/habaki/declarations.rb

Overview

Array of Declaration

Constant Summary

Constants included from Shorthand

Shorthand::BORDER_PROPERTIES, Shorthand::DIMENSIONS, Shorthand::PRECOMPUTED_SHORTHAND_PROPS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shorthand

#compute_dimensions_shorthand, #create_background_shorthand!, #create_border_shorthand!, #create_dimensions_shorthand!, #create_font_shorthand!, #create_list_style_shorthand!, #create_shorthand!, #create_shorthand_properties!, #expand_background_shorthand!, #expand_border_shorthand!, #expand_dimensions_shorthand!, #expand_font_shorthand!, #expand_list_style_shorthand!, #expand_shorthand!, #expand_shorthand_properties!, shorthand_properties

Methods inherited from NodeArray

#string_join, #to_s

Constructor Details

#initialize(*args) ⇒ Declarations

Returns a new instance of Declarations.



263
264
265
266
# File 'lib/habaki/declarations.rb', line 263

def initialize(*args)
  super(*args)
  @hash = {}
end

Class Method Details

.parse(data) ⇒ Declarations

Parse inline declarations

Parameters:

Returns:



271
272
273
274
275
# File 'lib/habaki/declarations.rb', line 271

def self.parse(data)
  decls = self.new
  decls.parse!(data)
  decls
end

Instance Method Details

#[](prop) ⇒ Declaration?

at position or shortcut for find_by_property

Parameters:

  • prop (Integer, String)

    index or property name

Returns:



342
343
344
345
346
347
348
349
350
351
# File 'lib/habaki/declarations.rb', line 342

def [](prop)
  case prop
  when Integer
    at(prop)
  when ::String
    find_by_property(prop)
  else
    raise TypeError, "invalid type #{prop.class}"
  end
end

#add_by_property(property, value = [], important = false) ⇒ Declaration

Add declaration

Parameters:

  • property (String)
  • value (Value, Values, Array<Value>) (defaults to: [])
  • important (Boolean) (defaults to: false)

Returns:



315
316
317
318
319
320
# File 'lib/habaki/declarations.rb', line 315

def add_by_property(property, value = [], important = false)
  decl = Habaki::Declaration.new(property, important)
  decl.values = Values.new([value].flatten)
  push_declaration decl
  decl
end

#find_by_property(property) ⇒ Declaration

Find declaration with property

Parameters:

Returns:



298
299
300
# File 'lib/habaki/declarations.rb', line 298

def find_by_property(property)
  @hash[property]
end

#has_property?(property) ⇒ Boolean

Does declaration with property present ?

Returns:

  • (Boolean)


291
292
293
# File 'lib/habaki/declarations.rb', line 291

def has_property?(property)
  @hash.has_key?(property)
end

#parse!(data) ⇒ void

This method returns an undefined value.

Parse inline declarations and append to current declarations

Parameters:



280
281
282
283
284
285
286
287
# File 'lib/habaki/declarations.rb', line 280

def parse!(data)
  return unless data

  out = Katana.parse_inline(data)
  if out.declarations
    read_from_katana(out.declarations)
  end
end

#read_from_katana(decls) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:



364
365
366
367
368
# File 'lib/habaki/declarations.rb', line 364

def read_from_katana(decls)
  decls.each do |decl|
    push_declaration Declaration.read_from_katana(decl)
  end
end

#remove_by_property(property) ⇒ void

This method returns an undefined value.

Remove declaration with property

Parameters:



305
306
307
308
# File 'lib/habaki/declarations.rb', line 305

def remove_by_property(property)
  @hash.delete(property)
  reject! { |decl| decl.property == property }
end

#replace_important(decl) ⇒ Declaration

Add declaration or replace if more important

Parameters:

Returns:



325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/habaki/declarations.rb', line 325

def replace_important(decl)
  previous_decl = find_by_property(decl.property)
  if previous_decl
    if decl.important || !previous_decl.important
      #remove_by_property(decl.property)
      delete(previous_decl)
      push_declaration decl
    end
  else
    push_declaration decl
  end
  decl
end

#string(format = Formatter::Base.new) ⇒ String

Parameters:

Returns:



355
356
357
358
359
# File 'lib/habaki/declarations.rb', line 355

def string(format = Formatter::Base.new)
  map do |decl|
    decl.string(format) + ";"
  end.join(format.declarations_join)
end