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

#filenameObject



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

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

#finishObject



90
91
92
93
94
95
96
97
98
# File 'lib/pickler/feature.rb', line 90

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

#idObject



100
101
102
103
104
105
106
107
# File 'lib/pickler/feature.rb', line 100

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
  File.read(filename) if filename
end

#pull(default = nil) ⇒ Object



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

def pull(default = nil)
  story = story() # force the read into local_body before File.open below blows it away
  filename = filename() || pickler.features_path("#{story.suggested_basename(default)}.feature")
  File.open(filename,'w') {|f| f.puts story.to_s(pickler.format)}
  @filename = filename
end

#pushObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pickler/feature.rb', line 70

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)


66
67
68
# File 'lib/pickler/feature.rb', line 66

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



59
60
61
62
63
64
# File 'lib/pickler/feature.rb', line 59

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

#storyObject



109
110
111
# File 'lib/pickler/feature.rb', line 109

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

#to_sObject



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

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