Class: JsonResume::Formatter

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

Direct Known Subclasses

FormatterHtml, FormatterLatex, FormatterMd

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Formatter

Returns a new instance of Formatter.



13
14
15
16
17
18
19
20
21
22
# File 'lib/json_resume/formatter.rb', line 13

def initialize(hash)
  @hash = hash

  #recursively defined proc
  @hash_proc = Proc.new do |k,v|
    v = k if v.nil? #hack to make it common for hash and array
    v.delete_if(&@hash_proc) if [Hash,Array].any? { |x| v.instance_of? x }
    v.empty?
  end
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



11
12
13
# File 'lib/json_resume/formatter.rb', line 11

def hash
  @hash
end

Instance Method Details

#add_last_marker_on_field(field_name) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/json_resume/formatter.rb', line 55

def add_last_marker_on_field field_name
  return if @hash['bio_data'][field_name].nil?
  @hash['bio_data'][field_name]['items'].each do |item|
    next if item['technology_used'].nil?
    item['technology_used']['tools'].map!{|x| {'name' => x} }
    item['technology_used']['tools'][-1]['last'] = true
  end
end

#add_last_marker_on_skillsObject



38
39
40
41
42
43
44
# File 'lib/json_resume/formatter.rb', line 38

def add_last_marker_on_skills
  return if @hash['bio_data']['skills'].nil?
  @hash['bio_data']['skills']['details'].each do |item|
    item['items'].map!{|x| {'name'=>x} }
    item['items'][-1]['last'] = true
  end
end

#add_last_marker_on_starsObject



30
31
32
33
34
35
36
# File 'lib/json_resume/formatter.rb', line 30

def add_last_marker_on_stars
  return if @hash['bio_data']['stars'].nil?
  @hash["bio_data"]["stars"] = {
    "items" => @hash["bio_data"]["stars"].map{ |i| { "name" => i } }
  }
  @hash["bio_data"]["stars"]["items"][-1]["last"] = true
end

#add_last_marker_on_toolsObject



46
47
48
49
50
51
52
53
# File 'lib/json_resume/formatter.rb', line 46

def add_last_marker_on_tools
  return if @hash['bio_data']['other_projects'].nil?
  @hash['bio_data']['other_projects']['items'].each do |item|
    next if item['technology_used'].nil?
    item['technology_used']['tools'].map!{|x| {'name' => x} }
    item['technology_used']['tools'][-1]['last'] = true
  end
end

#add_linkedin_github_urlObject



24
25
26
27
28
# File 'lib/json_resume/formatter.rb', line 24

def add_linkedin_github_url
  @hash["raw_website"] = @hash["bio_data"]["website"].sub(/^https?:\/\//,'') if @hash["bio_data"] && @hash["bio_data"]["website"]
  @hash["linkedin_url"] = "http://linkedin.com/in/" + @hash["linkedin_id"] if @hash["linkedin_id"]
  @hash["github_url"] = "http://github.com/" + @hash["github_id"] if @hash["github_id"]
end

#add_padding(course) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/json_resume/formatter.rb', line 92

def add_padding(course)
  unless @hash["bio_data"].nil? || @hash["bio_data"][course].nil?
    course_hash = @hash["bio_data"][course]
    course_hash << {} if course_hash.size % 2 == 1
    @hash["bio_data"][course] = {
      "rows" => course_hash.each_slice(2).to_a.map{ |i| { "columns" => i } }
    }
  end
end

#cleanseObject



64
65
66
67
# File 'lib/json_resume/formatter.rb', line 64

def cleanse
  @hash.delete_if &@hash_proc
  self
end

#formatObject



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
# File 'lib/json_resume/formatter.rb', line 102

def format
  return if @hash["bio_data"].nil?

  cleanse

  format_to_output_type

  add_last_marker_on_stars

  add_last_marker_on_skills

  add_last_marker_on_field 'experience'
  add_last_marker_on_field 'other_projects'

  purge_gpa

  add_linkedin_github_url

  #make odd listed courses to even
  ["grad_courses", "undergrad_courses"].each do |course|
    add_padding(course)
  end

  return self
end

#format_string(str) ⇒ Object

Raises:

  • (NotImplementedError)


79
80
81
# File 'lib/json_resume/formatter.rb', line 79

def format_string str
  raise NotImplementedError.new("format_string not impl in formatter")
end

#format_to_output_typeObject



69
70
71
72
73
74
75
76
77
# File 'lib/json_resume/formatter.rb', line 69

def format_to_output_type
  format_proc = Proc.new do |k,v|
    v = k if v.nil?
    v.each{|x| format_proc.call(x)} if [Hash,Array].any? {|x| v.instance_of? x}
    format_string v if v.instance_of? String
  end
  @hash.each{|x| format_proc.call(x)}
  self
end

#is_false?(item) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/json_resume/formatter.rb', line 83

def is_false? item
  item == false || item == 'false'
end

#purge_gpaObject



87
88
89
90
# File 'lib/json_resume/formatter.rb', line 87

def purge_gpa
  return if @hash['bio_data']['education'].nil?
  @hash["bio_data"]["education"].delete("show_gpa") if is_false?(@hash["bio_data"]["education"]["show_gpa"]) || @hash["bio_data"]["education"]["schools"].all? {|sch| sch["gpa"].nil? || sch["gpa"].empty?}
end