Class: Artifacts::Executable

Inherits:
Object
  • Object
show all
Defined in:
lib/artifacts/executable.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Executable

Returns a new instance of Executable.



19
20
21
22
23
24
25
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
53
54
55
56
57
58
59
60
61
62
63
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/artifacts/executable.rb', line 19

def initialize( args )
  if args.first == 'init'
    args.shift
    path = args.shift
    Artifacts.new( path )
    puts "Artifacts repository created at: #{path}"
    exit 0
  end
  begin
    Artifacts.new
  rescue Artifacts::UnableToLocatePath
    puts "Unable to locate artifacts data file!"
    raise
  end

  @closed = false
  @open = true
  @verbose = false
  @dependants = false

  unless args.select{|n|n=~/^(?:-c|--closed)$/}.empty?
    @closed = true
    @open = false
    args.delete( '-c' )
    args.delete( '--closed' )
  end

  unless args.select{|n|n=~/^(?:-a|--all)$/}.empty?
    @closed = true
    @open = true
    @dependants = true
    args.delete( '-a' )
    args.delete( '--all' )
  end

  unless args.select{|n|n=~/^(?:-v|--verbose)$/}.empty?
    @verbose = true
    args.delete( '-v' )
    args.delete( '--verbose' )
  end

  unless args.select{|n|n=~/^(?:-d|--dependants)$/}.empty?
    @dependants = true
    args.delete( '-d' )
    args.delete( '--dependants' )
  end

  unless args.select{|n|n=~/^(?:-l|--limit)$/}.empty?
    idx = args.index( '-l' )
    idx ||= args.index( '--limit' )
    args.delete_at( idx )
    @limit = args.delete_at( idx ).to_i
  end

  @limit = ( @verbose ? 3 : 10 ) if @limit.to_i < 1

  @artifacts = []
  @tags = []
  any_artifacts_given = false
  while match = args.first && args.first.match( /^(?:(\d+)|#(#{Tag::VALID_NAME}))$/ )
    any_artifacts_given = true
    if match[1]
      @artifacts << Artifact[ args.shift.to_i ]
    elsif match[2]
      args.shift
      @tags << match[2]
      tag = Tag.select{|n|n.name == match[2]}.first
      @artifacts << tag.artifacts if tag
    end
  end

  @artifacts.flatten!
  @artifacts.uniq!

  @tags.flatten!
  @tags.uniq!

  @artifacts = Artifact.all unless any_artifacts_given

  @artifacts = @artifacts.select do |artifact|
    ( ( @open && !artifact.closed ) ||
      ( @closed && artifact.closed ) ) &&
    ( @dependants || artifact.open_depends_on.size == 0 )
  end

  @artifacts.sort!

  command = args.shift
  command ||= :list

  send( command.to_sym, *args )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



112
113
114
# File 'lib/artifacts/executable.rb', line 112

def method_missing( method, *args )
  puts "Unknown command: #{method.to_s.gsub('__',' ')}"
end

Class Method Details

.stub(method, default) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/artifacts/executable.rb', line 11

def self.stub( method, default )
  define_method method do |*args|
    command = args.shift
    command ||= default
    send( "#{method}__#{command}".to_sym, *args )
  end
end

Instance Method Details

#closeObject



147
148
149
150
151
152
# File 'lib/artifacts/executable.rb', line 147

def close
  @artifacts.each do |artifact|
    artifact.closed = Time.now
    puts "Closed: #{artifact}"
  end
end

#comment(*args) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'lib/artifacts/executable.rb', line 215

def comment( *args )
  text = args.join(' ')
  author = Artifacts.username
  @artifacts.each do |artifact|
    Comment.create( artifact, author, text )
  end
  puts "Comments added to:"
  list
end

#create(*args) ⇒ Object



140
141
142
143
144
145
# File 'lib/artifacts/executable.rb', line 140

def create( *args )
  artifact = Artifact.create( Artifacts.username, args.join(' ') )
  puts "Created artifact #{artifact.id}."
  @artifacts = [artifact]
  tag( *@tags )
end

#depends(*args) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/artifacts/executable.rb', line 168

def depends( *args )
  args.shift if args.first == 'on' # optional syntax sugar
  depends_on_list = args.map{|id|Artifact[id.to_i]}
  @artifacts.each do |artifact|
    depends_on_list.each do |depends_on|
      ArtifactArtifact.create( artifact, depends_on )
      puts "Artifact [#{artifact.id}] now depends on artifact [#{depends_on.id}]"
    end
  end
end

#help__help(*args) ⇒ Object



120
121
122
# File 'lib/artifacts/executable.rb', line 120

def help__help( *args )
  help_command :help, 'Display this message. For more information on a specific command use `help <command>` eg: `help tag`'
end

#help__list(*args) ⇒ Object



124
125
126
127
128
129
# File 'lib/artifacts/executable.rb', line 124

def help__list( *args )
  help__help
  help_command :tag,  'Tagging'
  help_command :create,  'Create a new artifact'
  help_command :list, 'List artifacts'
end

#help_command(command, description) ⇒ Object



116
117
118
# File 'lib/artifacts/executable.rb', line 116

def help_command( command, description )
  puts( "%-10s : %s"%[command,description] )
end

#listObject



131
132
133
134
135
136
137
138
# File 'lib/artifacts/executable.rb', line 131

def list
  @limit = @artifacts.size if @limit > @artifacts.size
  @limit.times do
    artifact = @artifacts.shift
    puts artifact.send( @verbose ? :inspect : :to_s )
  end
  puts "... #{@artifacts.size} more ..." unless @artifacts.empty?
end

#priority(priority) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/artifacts/executable.rb', line 154

def priority( priority )
  priority = priority.to_i
  @artifacts.each do |artifact|
    artifact.priority = priority
    puts "Priority changed to #{priority} for: #{artifact}"
  end
end

#pruneObject



200
201
202
203
204
205
206
207
# File 'lib/artifacts/executable.rb', line 200

def prune
  Tag.all.sort{|a,b|a.name <=> b.name}.each do |tag|
    if tag.artifacts.size == 0
      puts "Pruning tag: #{tag.name}"
      tag.delete
    end
  end
end

#tag(*args) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/artifacts/executable.rb', line 179

def tag( *args )
  args.each do |tag_name|
    tag = Tag.select{|n|n.name == tag_name}.first
    tag ||= Tag.create( tag_name )
    @artifacts.each do |artifact|
      puts "Adding tag \"#{tag.name}\" to #{artifact}"
      artifact.add_tag( tag )
    end
  end
end

#tag__create(tag) ⇒ Object



225
226
227
# File 'lib/artifacts/executable.rb', line 225

def tag__create( tag )
  Tag.create( tag )
end

#tags(*args) ⇒ Object



209
210
211
212
213
# File 'lib/artifacts/executable.rb', line 209

def tags( *args )
  @artifacts.map{|n|n.tags}.flatten.uniq.sort.each do |tag|
    puts "#{tag}: #{@artifacts.select{|n|n.tags.index(tag)}.map{|n|n.id.to_s}.join(',')}"
  end
end

#untag(*args) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/artifacts/executable.rb', line 190

def untag( *args )
  args.each do |tag_name|
    tag = Tag.select{|n|n.name == tag_name}.first
    @artifacts.each do |artifact|
      puts "Removing tag \"#{tag.name}\" from #{artifact}"
      artifact.remove_tag( tag )
    end
  end
end

#versionObject



162
163
164
# File 'lib/artifacts/executable.rb', line 162

def version
  puts "\nArtifacts: #{Artifacts::VERSION}"
end