Class: Chimplate::Base

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

Class Method Summary collapse

Class Method Details

.diffObject



75
76
77
78
79
80
# File 'lib/chimplate/base.rb', line 75

def self.diff
  Dir.glob("*-*.html").each do |template_filename|
    diff_file(template_filename)
    puts "\n\n----------------------------------------------------------\n\n"
  end
end

.diff_file(template_filename) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/chimplate/base.rb', line 64

def self.diff_file(template_filename)
  file_info = file_info(template_filename)

  File.open template_filename, "rb" do |file|
    template = api.templateInfo "tid" => file_info[:tid]
    puts "Diff between local #{template_filename} and remote template.\n\n"

    puts Diffy::Diff.new(template["source"].chomp, file.read.chomp, :context => 2, :allow_empty_diff => true).to_s(:color)
  end
end

.pull(options = {}) ⇒ Object



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

def self.pull(options = {})
  api.templates["user"].each do |template_info|
    template = api.templateInfo "tid" => template_info["id"]
    filename = Dir.pwd + "/" + template_info["id"].to_s + "-" + sanitize_filename(template_info["name"]) + ".html"

    write = true
    if File.exist? filename
      if options[:force] || yield(filename)
        FileUtils.rm filename 
      else
        puts "Skipping #{filename}.\n"
        write = false
      end
    end

    if write
      File.open(filename, "w+") do |file|
        file.write(template["source"])
        puts "Saved template #{filename}.\n";
      end
    end
  end
end

.pushObject



41
42
43
44
45
# File 'lib/chimplate/base.rb', line 41

def self.push
  Dir.glob("*-*.html").each do |template_filename|
    push_file(template_filename)
  end
end

.push_file(template_filename) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chimplate/base.rb', line 47

def self.push_file(template_filename)
  file_data = file_info(template_filename)

  File.open template_filename, "rb" do |file|
    if file_data[:tid] == "new"
      new_tid = api.templateAdd :name => file_data[:template_name].gsub("_", " "), :html => file.read
      new_filename = new_tid.to_s + '-' + file_data[:template_name] + ".html"
      FileUtils.mv template_filename, new_filename

      puts "Saved new template #{new_filename}.\n";
    else
      api.templateUpdate "id" => file_data[:tid], "values" => { "html" => file.read }
      puts "Updated template #{template_filename}.\n";
    end
  end
end

.write_config(config, force = false) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/chimplate/base.rb', line 7

def self.write_config(config, force = false)
  FileUtils.rm destination if force
  
  return false if File.exist? destination
  
  File.open(destination, "w+") do |file|
    file.write(config.to_hash.to_yaml)
  end
end