Class: NA::Action

Inherits:
Hash
  • Object
show all
Defined in:
lib/na/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, project, parent, action) ⇒ Action

Returns a new instance of Action.



7
8
9
10
11
12
13
14
# File 'lib/na/action.rb', line 7

def initialize(file, project, parent, action)
  super()

  @file = file
  @project = project
  @parent = parent
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



5
6
7
# File 'lib/na/action.rb', line 5

def action
  @action
end

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/na/action.rb', line 5

def file
  @file
end

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/na/action.rb', line 5

def parent
  @parent
end

#projectObject (readonly)

Returns the value of attribute project.



5
6
7
# File 'lib/na/action.rb', line 5

def project
  @project
end

Instance Method Details

#inspectObject



20
21
22
23
24
25
26
27
# File 'lib/na/action.rb', line 20

def inspect
  <<~EOINSPECT
  @file: #{@file}
  @project: #{@project}
  @parent: #{@parent.join('>')}
  @action: #{@action}
  EOINSPECT
end

#pretty(extension: 'taskpaper', template: {}) ⇒ Object



29
30
31
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/na/action.rb', line 29

def pretty(extension: 'taskpaper', template: {})
  default_template = {
    file: '{xbk}',
    parent: '{c}',
    parent_divider: '{xw}/',
    action: '{bg}',
    project: '{xbk}',
    tags: '{m}',
    value_parens: '{m}',
    values: '{y}',
    output: '%filename%parents| %action'
  }
  template = default_template.merge(template)

  if @parent != ['Inbox']
    parents = @parent.map do |par|
      NA::Color.template("#{template[:parent]}#{par}")
    end.join(NA::Color.template(template[:parent_divider]))
    parents = "{dc}[{x}#{parents}{dc}]{x} "
  else
    parents = ''
  end

  project = NA::Color.template("#{template[:project]}#{@project}{x} ")

  file = @file.sub(%r{^\./}, '').sub(/#{ENV['HOME']}/, '~')
  file = file.sub(/\.#{extension}$/, '')
  file = file.sub(/#{File.basename(@file, ".#{extension}")}$/, "{dw}#{File.basename(@file, ".#{extension}")}{x}")
  file_tpl = "#{template[:file]}#{file} {x}"
  filename = NA::Color.template(file_tpl)

  action = NA::Color.template("#{template[:action]}#{@action}{x}")
  action = action.highlight_tags(color: template[:tags],
                                 parens: template[:value_parens],
                                 value: template[:values],
                                 last_color: template[:action])

  NA::Color.template(template[:output].gsub(/%filename/, filename)
                      .gsub(/%project/, project)
                      .gsub(/%parents?/, parents)
                      .gsub(/%action/, action))
end

#to_sObject



16
17
18
# File 'lib/na/action.rb', line 16

def to_s
  "(#{@file}) #{@project}:#{@parent.join('>')} | #{@action}"
end