Class: CssCompare::CSS::Component::Keyframes

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

Overview

Represents an @keyframes declaration

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#equals?

Constructor Details

#initialize(node, conditions) ⇒ Keyframes

Returns a new instance of Keyframes.



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

def initialize(node, conditions)
  @name = node.value[1]
  process_conditions(conditions, process_rules(node.children))
end

Instance Attribute Details

#nameString (readonly)

The name of the keyframes.

Returns:

  • (String)

    the name



11
12
13
# File 'lib/css_compare/css/component/keyframes.rb', line 11

def name
  @name
end

#rulesHash{String => Hash{String => KeyframeSelector}} (readonly)

The rules of the keyframe grouped by Rules’ example structure:

{
  'all' => {
    '0%'   => {KeyframeSelector},
    '100%' => {KeyframeSelector}
  },
  '(max-width: 600px)' => {
    '0%'   => {KeyframeSelector},
    '50%'  => {KeyframeSelector},
    '100%' => {KeyframeSelector}
  }
}

Returns:

  • (Hash{String => Hash{String => KeyframeSelector}})


30
31
32
# File 'lib/css_compare/css/component/keyframes.rb', line 30

def rules
  @rules
end

Instance Method Details

#==(other) ⇒ Object

Checks, whether two @keyframes are equal.

Two @keyframes are only equal, if they both have equal keyframe selectors under each and every condition. If a condition or frame is missing from one or another, the @keyframes are not equal.

Parameters:

  • other (Keyframes)

    the @keyframe to compare this with.

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/css_compare/css/component/keyframes.rb', line 47

def ==(other)
  conditions = @rules.keys + other.rules.keys
  conditions.uniq!
  conditions.all? do |condition|
    return false unless @rules[condition] && other.rules[condition]
    super(@rules[condition], other.rules[condition])
  end
end

#deep_copy(name = @name) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/css_compare/css/component/keyframes.rb', line 72

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

#merge(keyframes) ⇒ Void

Merges this selector with another one.

The new declaration of the keyframe under the same condition rewrites the previous one. No deep_copy needs to be made and the value can be passed by reference.

Parameters:

  • keyframes (Keyframes)

    the keyframes to extend this one.

Returns:

  • (Void)


66
67
68
69
70
# File 'lib/css_compare/css/component/keyframes.rb', line 66

def merge(keyframes)
  keyframes.rules.each do |condition, selector|
    @rules[condition] = selector
  end
end

#process_conditions(conditions, keyframe_rules) ⇒ Hash{String => Hash}

Assigns the processed rules to the passed conditions By reference. No deep copy needed, as the KeyframeSelectors won’t be altered or merged with another KeyframeSelector, since this feature is missing at @keyframe directives.

Returns:

  • (Hash{String => Hash})

See Also:

  • `@rules`


101
102
103
104
105
# File 'lib/css_compare/css/component/keyframes.rb', line 101

def process_conditions(conditions, keyframe_rules)
  @rules = conditions.inject({}) do |kf, condition|
    kf.update(condition => keyframe_rules)
  end
end

#process_rules(rule_nodes) ⇒ Hash{String => KeyframeSelector}

Processes the keyframe rules and creates their internal representation.

Returns:

  • (Hash{String => KeyframeSelector})


111
112
113
114
115
116
117
118
119
# File 'lib/css_compare/css/component/keyframes.rb', line 111

def process_rules(rule_nodes)
  rule_nodes.each_with_object({}) do |node, rules|
    if node.is_a?(Sass::Tree::KeyframeRuleNode)
      rule = Component::KeyframesSelector.new(node)
      rules.update(rule.value => rule)
    end
    rules
  end
end

#to_jsonHash

Creates the JSON representation of this keyframes.

Returns:

  • (Hash)


83
84
85
86
87
88
89
90
91
92
# File 'lib/css_compare/css/component/keyframes.rb', line 83

def to_json
  json = { :name => @name.to_sym, :rules => {} }
  @rules.each_with_object(json[:rules]) do |(cond, rules), frames|
    rules.each_with_object(frames[cond.to_sym] = {}) do |(value, rule), result|
      result.update(value.to_sym => rule.to_json)
    end
    frames
  end
  json
end