Class: Jeanny::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/jeanny/engine.rb

Overview

Класс который выполнят всю основную работу. Парсит и заменяет классы, сохраняет и сравнивает их.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEngine

Returns a new instance of Engine.



12
13
14
# File 'lib/jeanny/engine.rb', line 12

def initialize
    @classes = Dictionary.new
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



10
11
12
# File 'lib/jeanny/engine.rb', line 10

def classes
  @classes
end

Instance Method Details

#analyze(file_meat) ⇒ Object

Метод ищет имена классов, в переданном ему тексте



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jeanny/engine.rb', line 17

def analyze file_meat
    
    fail TypeError, "передан неверный аргумент (Jeanny::Engine.analyze)" if file_meat.empty?

    # Удаляем все экспрешены и удаляем все что в простых и фигурных скобках
    file_meat = file_meat.remove_expressions.gsub(/\{.*?\}/m , '{}').gsub(/\(.*?\)/, '()')

    short_words = generate_short_words

    # Находим имена классов
    file_meat.gsub(/\.([^\.,\{\} :#\[\]\*\n\s\/]+)/) do |match|
        # Если найденная строка соответствует маске и класс еще не был добавлен — добавляем его
        @classes[$1] = short_words.shift if match =~ /^\.([a-z]-.+)$/ and not(@classes.include? $1 ) 
    end

    fail JeannyClassesNotFound, "похоже, что в анализируемом файле нет классов подходящих по условию" if @classes.empty?
    
    # @classes.sort!
    @classes

end

#compare_with(saved_classes) ⇒ Object

Метод сравниваеи найденные классы с переданными в аргументе saved_classes и возвращает имена элементво которых нет в saved_classes



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jeanny/engine.rb', line 41

def compare_with saved_classes

    return if saved_classes.nil? or saved_classes.empty?
    
    saved_classes = Dictionary.new saved_classes
    
    # находим новые классы
    new_classes = ((saved_classes.keys | @classes.keys) - saved_classes.keys)

    @classes = saved_classes

    # генерируем короткие имена и удаляем из них уже используемые
    short_words = generate_short_words - saved_classes.values
    new_classes.each do |class_name|
        @classes[class_name] = short_words.shift
    end
    
    # @classes.sort!

    new_classes

end

#replace(data, type) ⇒ Object

Метод для замены классов



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jeanny/engine.rb', line 65

def replace data, type
    
    fail "Тип блока не понятный" unless [:js, :css, :html, :tt2, :plain].include? type
    fail "nil Ololo" if data.nil?
    
    code = case type
        when :js then JSCode
        when :css then CSSCode
        when :tt2 then TT2Template
        when :html then HTMLCode
        when :plain then PlainCode
    end
    
    @classes.sort!
    
    code.new(data).replace @classes
    
end