Class: Overapp::TemplateFile

Inherits:
Object
  • Object
show all
Includes:
FromHash
Defined in:
lib/overapp/template_file.rb

Direct Known Subclasses

BasicFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#full_bodyObject

Returns the value of attribute full_body.



4
5
6
# File 'lib/overapp/template_file.rb', line 4

def full_body
  @full_body
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/overapp/template_file.rb', line 4

def path
  @path
end

Instance Method Details

#apply_body_to(base_body) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/overapp/template_file.rb', line 65

def apply_body_to(base_body)
  note_params.each do |params|
    body = params[:body]
    base_body = if params[:action].blank?
      body
    elsif params[:action] == 'append'
      base_body + body
    elsif params[:action] == 'insert' && params[:after]
      base_body.gsub(params[:after],"#{params[:after]}#{body}").tap do |subbed|
        if subbed == base_body
          raise "no change, couldn't find #{params[:after]} to insert #{body} in \n#{base_body}"
        end
      end
    elsif params[:action] == 'insert' && params[:before]
      base_body.gsub(params[:before],"#{body}#{params[:before]}").tap do |subbed|
        if subbed == base_body
          raise "no change, couldn't find #{params[:before]} in \n#{base_body}"
        end
      end
    elsif params[:action] == 'replace' && params[:base]
      base_body.gsub(params[:base],body).tap do |subbed|
        if subbed == base_body
          raise "no change, couldn't find #{params[:base]} to replace with #{body} in \n#{base_body}"
        end
      end
    elsif params[:action] == 'delete'
      :delete
    else
      raise "bad #{params.inspect}"
    end
  end
  base_body
end

#bodyObject



104
105
106
# File 'lib/overapp/template_file.rb', line 104

def body
  full_body
end

#combined(base) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/overapp/template_file.rb', line 108

def combined(base)
  b = apply_body_to(base.full_body)
  if b == :delete
    nil
  else
    self.class.new(:path => path, :full_body => b)
  end
end

#has_note?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/overapp/template_file.rb', line 100

def has_note?
  split_parts.any? { |x| x[:note].present? }
end

#note_paramsObject



58
59
60
61
62
# File 'lib/overapp/template_file.rb', line 58

def note_params
  split_parts.map do |one|
    note_params_single(one)
  end
end

#note_params_single(one) ⇒ Object



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
# File 'lib/overapp/template_file.rb', line 32

def note_params_single(one)
  res = {}
  note = one[:note]
  res[:body] = one[:body]

  if note
    lines = note.split("\n").select { |x| x.present? }
    if lines.size == 1 && !(lines.first =~ /action:/)
      res[:action] = lines.first.strip
    else
      lines.each do |line|
        parts = line.split(":").select { |x| x.present? }
        if parts.size > 2
          parts = [parts[0],parts[1..-1].join(":")]
        end
        parts = parts.map { |x| x.strip }
        raise "bad #{path} #{parts.inspect}" unless parts.size == 2
        res[parts[0].to_sym] = parts[1]
      end
    end
  else
    # do nothing
  end
  res
end

#split_note_and_bodyObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/overapp/template_file.rb', line 6

def split_note_and_body
  res = []
  remaining_body = full_body
  while remaining_body
    if remaining_body =~ /^<over(?:lay|app)>(.+)<\/over(?:lay|app)>(.*)(<over(?:lay|app)>.+)/m
      note = $1
      rest = $2
      remaining_body = $3
      res << {:note => note, :body => rest}
    elsif remaining_body =~ /^<over(?:lay|app)>(.+)<\/over(?:lay|app)>(.*)/m
      note = $1
      rest = $2
      remaining_body = nil
      res << {:note => note, :body => rest}
    else
      res << {:note => nil, :body => remaining_body}
      remaining_body = nil
    end
  end
  res
rescue => exp
  puts "Error in split_note_and_body #{path}"
  raise exp
end

#write_to!(dir) ⇒ Object



117
118
119
120
121
122
# File 'lib/overapp/template_file.rb', line 117

def write_to!(dir)
  raise "bad path" if path.blank?
  d = File.dirname("#{dir}/#{path}")
  `mkdir -p #{d}`
  File.create "#{dir}/#{path}",body
end