Class: Pickler::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/pickler/feature.rb

Constant Summary collapse

URL_REGEX =
%r{\bhttps?://www\.pivotaltracker\.com/\S*/(\d+)\b}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pickler, identifier) ⇒ Feature

Returns a new instance of Feature.



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

def initialize(pickler, identifier)
  @pickler = pickler
  case identifier
  when nil, /^\s+$/
    raise Error, "No feature given"

  when Pickler::Tracker::Story
    @story = identifier
    @id = @story.id

  when Integer
    @id = identifier

  when /^#{URL_REGEX}$/, /^(\d+)$/
    @id = $1.to_i

  when /\.feature$/
    if File.exist?(identifier)
      @filename = identifier
    end

  else
    if File.exist?(path = pickler.features_path("#{identifier}.feature"))
      @filename = path
    end

  end or raise Error, "Unrecognizable feature #{identifier}"
end

Instance Attribute Details

#picklerObject (readonly)

Returns the value of attribute pickler.



4
5
6
# File 'lib/pickler/feature.rb', line 4

def pickler
  @pickler
end

Instance Method Details

#existing_filenameObject



45
46
47
48
49
50
# File 'lib/pickler/feature.rb', line 45

def existing_filename
  unless defined?(@filename)
    @filename = find_feature_filename
  end
  @filename
end

#feature_titleObject



52
53
54
55
# File 'lib/pickler/feature.rb', line 52

def feature_title
  contents = self.to_s
  contents[/Feature: (.*)$/, 1]
end

#filenameObject



61
62
63
64
65
66
67
# File 'lib/pickler/feature.rb', line 61

def filename
  unless filename = existing_filename
    name = feature_title ? feature_title.gsub(/ /,'_').underscore : id
    filename = pickler.features_path("#{name}.feature")
  end
  filename
end

#find_feature_filenameObject



39
40
41
42
43
# File 'lib/pickler/feature.rb', line 39

def find_feature_filename
  Dir[pickler.features_path("**","*.feature")].detect do |f|
    File.read(f)[/(?:#\s*|@[[:punct:]]?)#{URL_REGEX}/,1].to_i == @id
  end
end

#finishObject



106
107
108
109
110
111
112
113
114
# File 'lib/pickler/feature.rb', line 106

def finish
  if filename
    story.finish
    story.to_s = local_body
    story.save
  else
    story.finish!
  end
end

#idObject



116
117
118
119
120
121
122
123
# File 'lib/pickler/feature.rb', line 116

def id
  unless defined?(@id)
    @id = if id = local_body.to_s[/(?:#\s*|@[[:punct:]]?)#{URL_REGEX}/,1]
            id.to_i
          end
  end
  @id
end

#local_bodyObject



35
36
37
# File 'lib/pickler/feature.rb', line 35

def local_body
  @local_body ||= File.read(filename) if existing_filename
end

#pullObject



69
70
71
72
73
# File 'lib/pickler/feature.rb', line 69

def pull
  story = story() # force the read into local_body before File.open below blows it away
  File.open(filename, 'w') {|f| f.puts self.to_s}
  @filename = filename
end

#pushObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pickler/feature.rb', line 86

def push
  body = local_body
  if story
    return if story.to_s(pickler.format) == body.to_s
    story.to_s = body
    story.save!
  else
    unless pushable?
      raise Error, "To create a new story, tag it @http://www.pivotaltracker.com/story/new"
    end
    story = pickler.new_story
    story.to_s = body
    @story = story.save!
    unless body.sub!(%r{\bhttps?://www\.pivotaltracker\.com/story/new\b}, story.url)
      body.sub!(/\A(?:#.*\n)?/,"# #{story.url}\n")
    end
    File.open(filename,'w') {|f| f.write body}
  end
end

#pushable?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/pickler/feature.rb', line 82

def pushable?
  id || local_body =~ %r{\A(?:#\s*|@[[:punct:]]?(?:https?://www\.pivotaltracker\.com/story/new)?[[:punct:]]?(?:\s+@\S+)*\s*)\n[[:upper:]][[:lower:]]+:} ? true : false
end

#start(default = nil) ⇒ Object



75
76
77
78
79
80
# File 'lib/pickler/feature.rb', line 75

def start(default = nil)
  story.transition!("started") if story.startable?
  if filename || default
    pull(default)
  end
end

#storyObject



125
126
127
# File 'lib/pickler/feature.rb', line 125

def story
  @story ||= @pickler.project.story(id) if id
end

#to_sObject



57
58
59
# File 'lib/pickler/feature.rb', line 57

def to_s
  local_body || story.to_s(pickler.format)
end