Class: Jeanny::Sugar::BridgeToJeanny

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/jeanny/sugar.rb

Overview

Singleton класс который взаимодействует c Jeanny

Instance Method Summary collapse

Constructor Details

#initializeBridgeToJeanny

Returns a new instance of BridgeToJeanny.



13
14
15
# File 'lib/jeanny/sugar.rb', line 13

def initialize
    # refresh
end

Instance Method Details

#analyze(path, args = {}) ⇒ 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jeanny/sugar.rb', line 17

def analyze(path, args = {})

    refresh

    begin
        
        file_list = File.list path
        
        fail JeannyFileNotFound, "файлы для анализа не найдены (Jeanny::Engine.analyze)" if file_list.empty?
        
        file_meat = ''
        file_list.each do |file|
            file_meat = file_meat + File.open_file(file)
        end
        
        if @engine.analyze file_meat
            @canbe[:save], @canbe[:process] = true, true
            @canbe[:analyze] = false
        end
        
    rescue StandardError => e
        $stderr.puts "Ошибка: ".red + e.message
        exit 1
    end
    
    @compare_file = (args[:compare_with] or '')
    
    # Завершаем метод если сравнивать ни с чем не надо
    return true if @compare_file.empty?
    
    # Если файл не найден, спрашиваем у юзернейма, как быть дальше,
    # продолжать без сравнения или прекратить работу.
    unless File.exists? @compare_file
        puts "Файл с сохраненными классами не найден. Продолжаем без сравнения.".yellow
        return true
    end
    
    saved_classes = []
    
    begin
        saved_classes = YAML.load_file @compare_file
        saved_classes.map! do |x|
            x.to_a.flatten
        end
    rescue Exception => e
        $stderr.puts e.message
        exit 1                    
    end
    
    # Сравниваем
    new_classes = @engine.compare_with saved_classes
    
    unless new_classes.nil? or new_classes.empty?
        puts 'Новые классы: '
        new_classes.each do |class_name|
            puts "  #{class_name.ljust(40, '.')}#{@engine.classes[class_name].green}"
        end
    end

    true
    
end

#group(type, args = {}, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/jeanny/sugar.rb', line 96

def group type, args = {}, &block
   
    begin
        
        fail "We can`t process here..." unless @canbe[:process]
        fail "Блоки process не должны быть рекурсивными" if @process_block_start
        fail "Не передан блок" unless block_given?
        
        @canbe[:process] = false
        @canbe[:replace] = true

        @process_block_start = true
        @process_block = []

        yield block
        
        @process_block_start = false

        @canbe[:replace] = false
        @canbe[:process] = true
        
    rescue Exception => e
        $stderr.puts "Ошибка: ".red + e.message
        exit 1
    end

    @process_block.each do |replace|
        File.list replace[:in] do |file, status|
            
            # next unless status.zero?
            unless status.zero?
                puts file.red
                next
            end
            
            exclude = false
            replace[:ex].each do |exclude_rule|
                if exclude_rule.kind_of? Regexp
                    exclude = file =~ exclude_rule
                else
                    exclude = file.include? exclude_rule
                end
                break if exclude
            end
            
            if exclude
                puts file.yellow
                next
            end
            
            begin
                
                puts file.green

                data = File.open_file file
                data = @engine.replace data, type

                File.save_file file, data, replace[:prefix]
                
            rescue Exception => e
                puts e.message + "\n#{$@}"
                exit 1
            end
            
        end
    end
    
end

#replace(args = { }) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/jeanny/sugar.rb', line 165

def replace args = { }
    
    fail "We can`t replace here..." unless @canbe[:replace]
    
    struct = { :in => [], :ex => [], :prefix => '' }

    struct[:in] = ([args[:in]] | [args[:include]]).flatten.delete_if { |item| item.nil? or item.empty? }
    struct[:ex] = ([args[:ex]] | [args[:exclude]]).flatten.delete_if { |item| item.nil? or item.empty? }

    struct[:prefix] = args[:prefix] unless args[:prefix].nil?

    @process_block << struct if struct[:in].length > 0
    
end

#save(file = '') ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jeanny/sugar.rb', line 80

def save file = ''
    
    fail RuntimeError, 'на данном этапе нельзя вызывать сохранение классов' unless @canbe[:save]

    file = file.empty? ? @compare_file : file

    open(file, 'w') do |f|
        f.puts @engine.classes.to_yaml
    end unless file.nil? or file.empty?

end

#save_to(file) ⇒ Object



92
93
94
# File 'lib/jeanny/sugar.rb', line 92

def save_to file
    save file
end