Class: Backlog::DateFile

Inherits:
Object
  • Object
show all
Defined in:
lib/backlog/datefile.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date_string = nil) ⇒ DateFile

Returns a new instance of DateFile.



9
10
11
12
13
14
15
16
17
18
# File 'lib/backlog/datefile.rb', line 9

def initialize(date_string = nil)
  
  @string = date_string
  if date_string == nil
    now
  else
    parse_date(date_string)
  end
  
end

Instance Attribute Details

#dateObject

Returns the value of attribute date.



7
8
9
# File 'lib/backlog/datefile.rb', line 7

def date
  @date
end

#validObject

Returns the value of attribute valid.



6
7
8
# File 'lib/backlog/datefile.rb', line 6

def valid
  @valid
end

Class Method Details

.date_from_path(path) ⇒ Object



20
21
22
23
24
# File 'lib/backlog/datefile.rb', line 20

def self.date_from_path(path)
  
  date_string = File.basename(path).sub!(".md", "").gsub!("-", " ")
  return Date.parse(Chronic.parse(date_string).to_s)
end

.new_from_argv(argv) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/backlog/datefile.rb', line 26

def self.new_from_argv(argv)

  if argv.length == 0
    return DateFile.new
  end

  # string
  date_string = ""
  date = nil

  # loop through all the string combinations possible
  argv.each do |piece|
  
    date_string = [date_string.split(" ") + [piece]].join " "
    piece_date = DateFile.new date_string

    if piece_date.valid
      date = piece_date
    end
  end

  if date and date.valid
    return date
  else
    return nil
  end
end

Instance Method Details

#argvObject



60
61
62
# File 'lib/backlog/datefile.rb', line 60

def argv
  return @argv
end

#existsObject



97
98
99
100
101
102
# File 'lib/backlog/datefile.rb', line 97

def exists

  # return whether or not the date exists
  return File.exist? path

end

#filenameObject



54
55
56
57
58
# File 'lib/backlog/datefile.rb', line 54

def filename
  
  code = @date.strftime("%a-%b-%d")
  return "#{code.downcase}.md"
end

#pathObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/backlog/datefile.rb', line 64

def path
  # determine path
  # if a past date - check to see if the date exists
  # check current/archive
  # generate the correct path as needed
  current_path = File.join Config.current_dir, filename
  archive_path = File.join Config.archive_dir, filename

  # logic to return the correct path
  if @date == Date.today
    return current_path

  elsif @date < Date.today
    
    if File.exist? current_path
      return current_path
    elsif File.exist? archive_path
        return archive_path
    else
        return current_path
    end
  else 
    return current_path
  end
end

#relative_pathObject



90
91
92
93
94
95
# File 'lib/backlog/datefile.rb', line 90

def relative_path

  # remove the base directory from the path
  return path.sub "#{Config.base_dir}/", ""

end