Class: CssCompare::CSS::Component::Property

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

Overview

Represents a CSS property tied to a specific selector. It can have several values based on the conditions specified by the @media queries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#equals?

Constructor Details

#initialize(node, conditions) ⇒ Property

Note:

An optimization has been done here, as well. If there are several conditions, all should be paired with the same value but not the same object, since the value can be overridden later in the process of the stylesheet evaluation. A clone of the value is paired with each of the conditions.

Returns a new instance of Property.

Parameters:

  • node (Sass::Tree::PropNode)

    the property node

  • conditions (Array<String>)

    media query conditions



28
29
30
31
32
33
# File 'lib/css_compare/css/component/property.rb', line 28

def initialize(node, conditions)
  @name = node.resolved_name
  @values = {}
  value = Value.new(node)
  conditions.each { |c| set_value(value.clone, c) }
end

Instance Attribute Details

#nameString (readonly)

Returns name of the property.

Returns:

  • (String)

    name of the property



9
10
11
# File 'lib/css_compare/css/component/property.rb', line 9

def name
  @name
end

#valuesHash{String=>Value}

Key-value pair of property values.

The key represents the condition set by a media query and the value is the actual value of the property.

Returns:

  • (Hash{String=>Value})

    name of the property



17
18
19
# File 'lib/css_compare/css/component/property.rb', line 17

def values
  @values
end

Instance Method Details

#==(other) ⇒ Boolean

Checks whether two properties are equal.

Properties are equal only if both contain the same keys and the values under those keys are also equal.

Parameters:

  • other (Property)

    the property to compare this with.

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/css_compare/css/component/property.rb', line 44

def ==(other)
  return super(@values, other.values) unless font?
  equals?(@values, other.values)
end

#complex?Boolean

Whether the property is a complex one. One property, that can be separated into smaller chunks of elementary properties.

Example:

`border: 1px solid black` => `{
    border-width: 1px;
    border-style: solid;
    border-color: black;
}`

Returns:

  • (Boolean)

See Also:

  • #process


111
112
113
114
# File 'lib/css_compare/css/component/property.rb', line 111

def complex?
  COMPLEX_PROPERTIES.include?(@name)
  false
end

#deep_copyProperty

Creates a deep copy of this property.

Returns:



119
120
121
122
123
124
# File 'lib/css_compare/css/component/property.rb', line 119

def deep_copy
  copy = dup
  copy.values = {}
  @values.each { |k, v| copy.values[k] = v.clone }
  copy
end

#merge(property) ⇒ Void

Merges the property with another one.

Parameters:

  • property (Property)

    to be merged with ‘self`

Returns:

  • (Void)


53
54
55
# File 'lib/css_compare/css/component/property.rb', line 53

def merge(property)
  property.values.each { |cond, v| set_value(v, cond) }
end

#set_value(val, condition = 'all') ⇒ Void

Replaces the original value of the property under a certain media query condition.

If the value does not exist under the specified condition, it is added into the set of property values. Otherwise, it rewrites the current value if it’s not set as important. However, if the replacing value is also set as important, the current value will be replaced with the new one.

If the condition does not exist but there is an important global value assigned to the property, the replacing value will be used only, if it’s set to important, too. Otherwise, the global value should be cloned in there.

Parameters:

  • val (Value)

    that should replace the current value

  • condition (String) (defaults to: 'all')

    the circumstance, under which the property should take the new value.

Returns:

  • (Void)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/css_compare/css/component/property.rb', line 75

def set_value(val, condition = 'all')
  global_value = @values['all']
  val_to_replace = value(condition)
  # Check, whether the condition exists
  if val_to_replace
    @values[condition].value = val if val.important? || !val_to_replace.important?
  else
    @values[condition] = if global_value && global_value.important?
                           val.important? ? val : global_value.clone
                         else
                           val
                         end
  end
end

#to_jsonHash

Creates the JSON representation of this property.

Returns:

  • (Hash)


129
130
131
# File 'lib/css_compare/css/component/property.rb', line 129

def to_json
  @values.inject({}) { |result, (k, v)| result.update(k => v.to_s) }
end

#value(condition = 'all') ⇒ #to_s

Returns the property’s value taken under a certain circumstance

Returns:

  • (#to_s)


94
95
96
# File 'lib/css_compare/css/component/property.rb', line 94

def value(condition = 'all')
  @values[condition]
end