Class: CV

Inherits:
Mustache
  • Object
show all
Defined in:
lib/yaml-cv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ CV

Returns a new instance of CV.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yaml-cv.rb', line 43

def initialize(file_path)
    @file_path = file_path
		@cv = YAML.load_file(file_path)

    if  @cv["contact"]
        @cv["contact"] = @cv["contact"].map { |c|
            c["icon_svg"] = icon(c["icon"])
            c
        }
    end
end

Instance Attribute Details

#is_pdfObject

Returns the value of attribute is_pdf.



41
42
43
# File 'lib/yaml-cv.rb', line 41

def is_pdf
  @is_pdf
end

Instance Method Details

#contactObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/yaml-cv.rb', line 127

def contact
    # split into a 2-column table
    contact_table = Array.new(3){Array.new(2)}

    i = 0
    while i < @cv["contact"].length()

        row = i % 3
        col = 1 - (i / 3)
        contact_table[row][col] = @cv["contact"][i]

        i = i + 1
    end
    contact_table
end

#cssObject



115
116
117
# File 'lib/yaml-cv.rb', line 115

def css
    load_asset("style.css")
end

#detailsObject



55
56
57
# File 'lib/yaml-cv.rb', line 55

def details
    @cv["details"]
end

#enable_pdf(enable = true) ⇒ Object



123
124
125
# File 'lib/yaml-cv.rb', line 123

def enable_pdf(enable = true)
    @is_pdf = true
end

#format_subsections(subsections) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/yaml-cv.rb', line 147

def format_subsections(subsections)
    if !subsections
        return
    end

    subsections.map { |e|
        if e["from"]
            e["from"] = format_period e["from"]
        end
        if e["to"]
            e["to"] = format_period e["to"]
        end

        if e["logo"]
            e["logo_img"] = read_image e["logo"]
        end

        e
    }
end

#full_nameObject



111
112
113
# File 'lib/yaml-cv.rb', line 111

def full_name
		details["last_name"] + " " + details["first_name"]
end

#has_profileObject



63
64
65
# File 'lib/yaml-cv.rb', line 63

def has_profile
    @cv.key?("profile")
end

#has_skillsObject



88
89
90
# File 'lib/yaml-cv.rb', line 88

def has_skills
    @cv.key?("skills")
end

#has_technicalObject



96
97
98
# File 'lib/yaml-cv.rb', line 96

def has_technical
    @cv.key?("technical")
end

#icon(name) ⇒ Object



143
144
145
# File 'lib/yaml-cv.rb', line 143

def icon(name)
    load_asset("icons/#{name.strip}.svg")
end

#is_windowsObject



210
211
212
# File 'lib/yaml-cv.rb', line 210

def is_windows
    RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
end

#pdf_cssObject



119
120
121
# File 'lib/yaml-cv.rb', line 119

def pdf_css
    load_asset("pdf.css")
end

#profileObject



59
60
61
# File 'lib/yaml-cv.rb', line 59

def profile
    @cv["profile"]
end

#read_image(img_path) ⇒ Object



168
169
170
171
172
173
# File 'lib/yaml-cv.rb', line 168

def read_image(img_path)
    file_path = File.join(File.dirname(@file_path), img_path)
    file = File.open(file_path, "rb")
    data = file.read
    Base64.strict_encode64(data)
end

#renderObject



175
176
177
178
# File 'lib/yaml-cv.rb', line 175

def render
    template = load_asset("cv.mustache")
    super(template)
end

#sectionsObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/yaml-cv.rb', line 100

def sections
    if !@cv["sections"]
        return
    end

    @cv["sections"].map { |s|
        s["items"] = format_subsections s["items"]
        s
    }
end

#skillsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/yaml-cv.rb', line 67

def skills
    # split into an n-column table
    nskills = @cv["skills"]["fields"].length()
    ncols = @cv["skills"]["columns"]
    nrows = (nskills.to_f / ncols).ceil

    skills_table = Array.new(nrows){Array.new(ncols)}

    i = 0
    while i < nskills

        col = i % ncols
        row = i / ncols

        skills_table[row][col] = @cv["skills"]["fields"][i]

        i = i + 1
    end
    skills_table
end

#technicalObject



92
93
94
# File 'lib/yaml-cv.rb', line 92

def technical
    @cv["technical"]
end

#write_html(file_path) ⇒ Object



180
181
182
183
# File 'lib/yaml-cv.rb', line 180

def write_html(file_path)
    html = render
    File.open(file_path, 'w') { |file| file.write(html) }
end

#write_pdf(file_path) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/yaml-cv.rb', line 185

def write_pdf(file_path)

    if is_windows
        temp_file_name = file_path + ".html"

        temp_file = File.open(temp_file_name, "w")
        temp_file << render
        temp_file.flush
        temp_file.close

        system("wkhtmltopdf.exe #{temp_file.path} #{file_path}")

        File.delete(temp_file_name)
    else
        temp_file = Tempfile.new(["cv", ".html"])
        temp_file << render
        temp_file.flush

        system("wkhtmltopdf #{temp_file.path} #{file_path}")

        temp_file.close
    end

end