Class: ZtreningM::Ztrening

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

Instance Method Summary collapse

Constructor Details

#initialize(*opts) ⇒ Ztrening

Returns a new instance of Ztrening.



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
# File 'lib/ztrening.rb', line 104

def initialize(*opts)
  @list_fname  = 'ztlista.txt'
  @dwl_link    = "z-trening.com|/training.php\?all_tasks\=1"
  @link_base   = 'http://z-trening.com/tasks.php?show_task=_ID_'
  @ztdir_fname = '.ztconf' # bolje ztdata; file sa conf vars
  @todo_fname  = '_PROJDIR_/todo.txt'

  @lista ||= []
  @opts = opts.moj_keys

  execerrmsg = "'exec' error! Aborting ..."

  if !@opts[:web].nil?
    web
    execout = exec
    (puts execerrmsg; exit) if (execout.nil? || execout.empty?)
    @lista = parse(execout)
  else
    if !File.exists? @list_fname
      puts "Datoteka #{@list_fname} ne postoji, skidam s weba ..."
      web
    end
    execout = exec
    (puts execerrmsg; exit) if execout.nil? || execout.empty?
    @lista = parse(execout)
  end
  @lista.sort!
end

Instance Method Details

#create_zaddir(dir) ⇒ Object

(args) - koristi hash i symbole



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/ztrening.rb', line 207

def create_zaddir(dir) #(args) - koristi hash i symbole
  # TODO: args: string dirname='', bool templates, ...
  if File.directory? dir
    File.open("#{File.dirname dir}/@ztdir_fname", "w"){ |f|
      f.write(<<EOS)
todo_file = todo.txt
status_file = status.txt
EOS
    }
    FileUtils.touch "#{File.dirname dir}/todo.txt"
    FileUtils.touch "#{File.dirname dir}/status.txt"
    puts "Napravljen ztdir u dir-u #{dir}"
  else
    puts "Dir #{dir} ne postoji, ne mogu napraviti zaddir!"
    return false
  end
end

#find(str) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/ztrening.rb', line 133

def find(str)
  puts "Lista je prazna!" if @lista.nil? or @lista.empty?
  ll=[]
  @lista.each { |e|
    ll << e if e[1].match(/#{str}/)
  }
  @lista = ll
end

#find_by_id(id) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/ztrening.rb', line 150

def find_by_id(id)
  puts "Lista je prazna! (find_by_id)" if @lista.nil? or @lista.empty?
  # puts "find_by_id lista cnt begin: #{@lista.count}"
  ll=[]
  @lista.each { |e| ll << e if e[0].match /#{id}/ }
  @lista = ll
  # puts "find_by_id lista cnt return: #{@lista.count}"
end

#find_by_name(str) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/ztrening.rb', line 142

def find_by_name(str)
  puts "Lista je prazna! (find_uniq)" if @lista.nil? or @lista.empty?
  ll=[]
  @lista.each { |e| ll << e if e[1] == str }
  @lista = ll
  # puts "find_by_name lista cnt: #{@lista.count}"
end

#findzaddirObject

get_zaddir



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ztrening.rb', line 195

def findzaddir
  # nadji DirName s datotekom '.ztprojdir', prema gore
  # false ako nema

  for n in (0..32)
    p = ('../'*n)
    return File.expand_path(p) if File.file?("#{p}#{@ztdir_fname}")
    return false if File.expand_path p == "/"
  end

end

#in_zaddir?Boolean

Returns:

  • (Boolean)


225
226
227
# File 'lib/ztrening.rb', line 225

def in_zaddir?
  (@ztdir_fname && File.exists?("./#{@ztdir_fname}")) || false
end

#listaObject



171
172
173
# File 'lib/ztrening.rb', line 171

def lista
  @lista
end

#lista_as_html(*args) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ztrening.rb', line 180

def lista_as_html(*args)
  opts = args.moj_keys
  puts '<ul>' if opts[:li]
  puts '<b>Zadatci:</b>' if opts[:naslov]
  @lista.each { |l|
    print '<li>' if opts[:li]
    print "<a href='#{link(l[0])}'>#{l[1]}</a>"
    puts if !opts[:li]
    print "</li>\n" if opts[:li]
  }
  puts '</ul>' if opts[:li]
end

#lista_txtObject



175
176
177
178
# File 'lib/ztrening.rb', line 175

def lista_txt
  puts 'Zadatci:'
  @lista.each { |l| puts "Zadatak: #{l[1]}" }
end

#open(arg = -1)) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ztrening.rb', line 159

def open(arg=-1)
  puts "Lista je prazna! (open)" if @lista.nil? || @lista.empty?
  if @lista.count == 1
    system "open #{link @lista[0][0]}"
  elsif arg!=-1
    # puts "arg u open(): #{arg}, #{arg.class}"
    system "open #{link(@lista[arg][0])}"
  else
    puts "Lista ne sadrzi 1 zadatak!"
  end
end

#set(arg) ⇒ Object



229
230
231
232
233
234
235
236
237
# File 'lib/ztrening.rb', line 229

def set(arg)
  if arg.start_with? '500000'
    id = arg
  else
    find_by_name arg
    id = @lista[0][0]
  end
  # TODO: set()
end