Class: CssCompare::CSS::Component::PageSelector

Inherits:
Base
  • Object
show all
Defined in:
lib/css_compare/css/component/page_selector.rb

Overview

Represents @page directive’s page selector (‘:right`, `LandscapeTable`, `CompanyLetterHead:first`) combined with the page body, that can contain a list of declarations and a list of page margin boxes.

The declarations not assigned to any margin symbol will be automatically grouped under the global margin symbol.

Constant Summary collapse

GLOBAL_MARGIN_SYMBOL =

The global margin symbol.

'@all'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#equals?

Constructor Details

#initialize(value, children, conditions) ⇒ PageSelector

Returns a new instance of PageSelector.

Parameters:

  • value (String)

    the page selector

  • children (Array<Sass::Tree::Node>)

    page body

  • conditions (Array<String>)

    applying media query conditions



32
33
34
35
36
# File 'lib/css_compare/css/component/page_selector.rb', line 32

def initialize(value, children, conditions)
  @value = value
  @margin_boxes = {}
  process_margin_boxes(children, conditions)
end

Instance Attribute Details

#margin_boxesHash{String => MarginBox}

The margin box directives containing the declarations.

Returns:



24
25
26
# File 'lib/css_compare/css/component/page_selector.rb', line 24

def margin_boxes
  @margin_boxes
end

#valueString

The value of this page selector

Returns:

  • (String)


18
19
20
# File 'lib/css_compare/css/component/page_selector.rb', line 18

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Checks, whether two page selectors are equal.

Two page selectors are equal only if both have declared the same margin boxes and they are also equal.

Parameters:

  • other (PageSelector)

    the page selector to compare this with.

Returns:

  • (Boolean)


47
48
49
# File 'lib/css_compare/css/component/page_selector.rb', line 47

def ==(other)
  super(@margin_boxes, other.margin_boxes)
end

#deep_copy(value = @value) ⇒ PageSelector

Creates a deep copy of this page selector.

Parameters:

  • value (String) (defaults to: @value)

    the new value of the selector’s copy.

Returns:



66
67
68
69
70
71
72
73
# File 'lib/css_compare/css/component/page_selector.rb', line 66

def deep_copy(value = @value)
  copy = dup
  copy.value = value
  copy.margin_boxes = @margin_boxes.inject({}) do |result, (k, v)|
    result.update(k => v.deep_copy)
  end
  copy
end

#merge(other) ⇒ Void

Merges this page selector with another one.

Parameters:

Returns:

  • (Void)


55
56
57
58
59
# File 'lib/css_compare/css/component/page_selector.rb', line 55

def merge(other)
  other.margin_boxes.each do |_, prop|
    add_margin_box(prop, true)
  end
end

#to_jsonHash

Creates the JSON representation of this page selector.

Returns:

  • (Hash)


78
79
80
81
82
83
84
# File 'lib/css_compare/css/component/page_selector.rb', line 78

def to_json
  json = { :selector => @value, :margin_boxes => [] }
  @margin_boxes.inject(json[:margin_boxes]) do |result, (_k, v)|
    result << v.to_json
  end
  json
end