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
38
39
40
41
42
43
44
# 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) and not($1 =~ /^g-hidden/)
        if match =~ /^\.([a-z]-.+)$/
            name = $1
            #unless @classes.include? name
            unless @classes.include? name or name =~ /^(g-hidden|b-keyboard|b-domik$)/
                @classes[name] = short_words.shift 
            end
        end
    end

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

end

#compare_with(saved_classes) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jeanny/engine.rb', line 48

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

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jeanny/engine.rb', line 72

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