Class: XcodeSnippets::Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/xcode_snippets/bundle.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Bundle

Returns a new instance of Bundle.



7
8
9
10
11
# File 'lib/xcode_snippets/bundle.rb', line 7

def initialize(path)
  @path = path
  @snippets = []
  load_snippets
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/xcode_snippets/bundle.rb', line 5

def path
  @path
end

#snippetsObject (readonly)

Returns the value of attribute snippets.



5
6
7
# File 'lib/xcode_snippets/bundle.rb', line 5

def snippets
  @snippets
end

Class Method Details

.bundle_named(name, install_directory) ⇒ Object



14
15
16
# File 'lib/xcode_snippets/bundle.rb', line 14

def bundle_named(name, install_directory)
  new(File.join(install_directory, "#{name}.snippetbundle"))
end

.default(directory) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/xcode_snippets/bundle.rb', line 18

def default(directory)
  bundle = bundle_named("Default", directory)
  
  if bundle.exists?
    bundle
  else
    bundle.copy_to(directory)
  end
end

Instance Method Details

#add_copy_of_snippet(snippet) ⇒ Object



57
58
59
60
# File 'lib/xcode_snippets/bundle.rb', line 57

def add_copy_of_snippet(snippet)
  @snippets << snippet.copy_to_bundle(self)
  @snippets.last
end

#add_copy_of_snippet_from_file(snippet_path) ⇒ Object



48
49
50
# File 'lib/xcode_snippets/bundle.rb', line 48

def add_copy_of_snippet_from_file(snippet_path)
  add_copy_of_snippet Snippet.new(snippet_path)
end

#add_snippet(snippet) ⇒ Object



52
53
54
55
# File 'lib/xcode_snippets/bundle.rb', line 52

def add_snippet(snippet)
  @snippets << snippet
  @snippets.last
end

#copy_to(directory) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/xcode_snippets/bundle.rb', line 29

def copy_to(directory)
  installation_path = File.join(directory, name)
  FileUtils.mkdir_p(installation_path)
  
  self.class.new(installation_path).tap do |copied_bundle|
    snippets.each do |snippet|
      copied_bundle.add_copy_of_snippet(snippet)
    end
  end
end

#deleteObject



62
63
64
# File 'lib/xcode_snippets/bundle.rb', line 62

def delete
  FileUtils.rm_rf(path)
end

#exists?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/xcode_snippets/bundle.rb', line 44

def exists?
  File.exist?(path)
end

#nameObject



40
41
42
# File 'lib/xcode_snippets/bundle.rb', line 40

def name
  File.basename(path)
end

#snippet_named(name) ⇒ Object



66
67
68
69
70
# File 'lib/xcode_snippets/bundle.rb', line 66

def snippet_named(name)
  name += ".codesnippet" if name !~ /\.codesnippet$/
  snippet = Snippet.new(File.join(path, name), self)
  snippet.exists? ? snippet : nil
end