Class: CodeTerminator::Css

Inherits:
Object
  • Object
show all
Defined in:
lib/code_terminator/css.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Css

Returns a new instance of Css.



5
6
7
8
9
10
11
12
# File 'lib/code_terminator/css.rb', line 5

def initialize(args = {})
  @code = args[:code]
  @source = args[:source]
  @tags = Array.new

  args[:source_type] ||= "file"
  @source_type = args[:source_type]
end

Instance Method Details

#get_elements(source) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/code_terminator/css.rb', line 52

def get_elements(source)
   reader = read_file(source)
   parser = Crass.parse(reader)
   errors = parser.pop
   elements = Array.new
   selector = ""

   parser.each do |node|
     if !node[:selector].nil?
       selector = node[:selector][:value]
       elements << {:selector => selector}
     end
     if !node[:children].nil?
       node[:children].each do |children|
         if children.has_value?(:property)
           elements << {:selector => selector, :property => children[:name], :value => children[:value]}
         end
       end
     end
   end
   elements
end

#get_instructions(source) ⇒ Object

Get the instructions to recreate the html code. Return an array with strings .

Example:

>> CodeTerminator::Css.get_instructions(file.get_elements("exercises/test.css"))
=> [["Create the selector body", "In the selector body add the property 'background-color' with value 'yellow' "]

Arguments:

instructions: (Array)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/code_terminator/css.rb', line 166

def get_instructions(source)
  elements = get_elements(source)
  text = ""
  instructions = Array.new
  elements.each do |child|
    if child[:property].nil?
      text << "Create the selector " + child[:selector]
    else
      text << "In the selector " + child[:selector] + " add the property '"  + child[:property] + "'"  if !child[:property].nil?
      text << " with value '" + child[:value] + "' " if !child[:value].nil?
    end
    instructions.push(text)
    text = ""
  end
  instructions
end

#match(source, code) ⇒ Object

Example:

test.css
=> body { background-color: lightblue; }

>> CodeTerminator::Css.match("test.css","body {background-color: blue; }")
=> [{:element=>{:selector=>"body", :property=>"background-color", :value=>"yellow"}, :type=>111, :description=>"not the same property background-color: yellow in selector body"}]

Arguments:

source: (String)
code: (String)

Fix: Add