Class: Slugforge::TagManager

Inherits:
Object
  • Object
show all
Defined in:
lib/slugforge/models/tag_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ TagManager

Returns a new instance of TagManager.



3
4
5
# File 'lib/slugforge/models/tag_manager.rb', line 3

def initialize(opts)
  bucket(opts)
end

Instance Method Details

#bucket(opts = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/slugforge/models/tag_manager.rb', line 7

def bucket(opts={})
  if @bucket.nil? || (true == (opts[:refresh] || @bucket_dirty))
    @s3 = opts[:s3] || @s3
    @aws_bucket = opts[:bucket] || @aws_bucket
    @bucket = @s3.directories.get(@aws_bucket)
    @slugs_for_tag = {}
    @tags = {}
    @bucket_dirty = false
  end
  @bucket
end

#clone_tag(project_name, old_tag, new_tag) ⇒ Object



94
95
96
97
# File 'lib/slugforge/models/tag_manager.rb', line 94

def clone_tag(project_name, old_tag, new_tag)
  slugs = slugs_for_tag(project_name, old_tag)
  save_tag(project_name, new_tag, slugs)
end

#create_tag(project_name, tag, slug_name) ⇒ Object



99
100
101
102
103
104
# File 'lib/slugforge/models/tag_manager.rb', line 99

def create_tag(project_name, tag, slug_name)
  slugs = [slug_name]
  slugs += slugs_for_tag(project_name, tag)
  slugs = slugs.slice(0,10)
  save_tag(project_name, tag, slugs)
end

#delete_tag(project_name, tag) ⇒ Object



106
107
108
109
110
# File 'lib/slugforge/models/tag_manager.rb', line 106

def delete_tag(project_name, tag)
  return nil if bucket.files.nil?
  bucket.files.head(tag_file_name(project_name, tag)).destroy
  @bucket_dirty = true
end

#memoize_slugs_for_tags(project_name) ⇒ Object

This method should be called before iterating over a large set of slugs and calling #tags_for_slug on them. By doing this you are able to query all the data from AWS in advance using parallelized threads, rather than in serial.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/slugforge/models/tag_manager.rb', line 42

def memoize_slugs_for_tags(project_name)
  @slugs_for_tag[project_name] ||= {}
  tag_map = tags(project_name).parallel_map do |tag|
    next if @slugs_for_tag[project_name][tag]
    file = nil
    begin
      file = bucket.files.get(tag_file_name(project_name, tag))
    rescue Excon::Errors::Forbidden
      # ignore 403's
    end
    slugs = file.nil? ? [] :  file.body.split("\n")
    [tag, slugs]
  end
  tag_map.each do |tag, slugs|
    @slugs_for_tag[project_name][tag] = slugs
  end
end

#projectsObject



19
20
21
22
23
24
25
26
# File 'lib/slugforge/models/tag_manager.rb', line 19

def projects
  return [] if bucket.files.nil?
  result = {}
  bucket.files.each do |file|
    result[$~[1]] = true if (file.key =~ /^([^\/]+)\//)
  end
  result.keys
end

#rollback_slug_for_tag(project_name, tag) ⇒ Object



74
75
76
77
78
79
# File 'lib/slugforge/models/tag_manager.rb', line 74

def rollback_slug_for_tag(project_name, tag)
  slugs = slugs_for_tag(project_name, tag)
  slugs.shift
  save_tag(project_name, tag, slugs) unless slugs.empty?
  slugs.first
end

#save_tag(project_name, tag, slugs) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/slugforge/models/tag_manager.rb', line 112

def save_tag(project_name, tag, slugs)
  bucket.files.create(
    :key    => tag_file_name(project_name, tag),
    :body   => slugs.join("\n"),
    :public => false
  )
  @bucket_dirty = true
end

#slug_for_tag(project_name, tag) ⇒ Object



81
82
83
84
# File 'lib/slugforge/models/tag_manager.rb', line 81

def slug_for_tag(project_name, tag)
  slugs = slugs_for_tag(project_name, tag)
  slugs.first
end

#slugs_for_tag(project_name, tag) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/slugforge/models/tag_manager.rb', line 60

def slugs_for_tag(project_name, tag)
  @slugs_for_tag[project_name] ||= {}
  @slugs_for_tag[project_name][tag] ||= begin
    return [] if bucket.files.nil?
    file = nil
    begin
      file = bucket.files.get(tag_file_name(project_name, tag))
    rescue Excon::Errors::Forbidden
      # ignore 403's
    end
    file.nil? ? [] :  file.body.split("\n")
  end
end

#tag_file_name(project_name, tag) ⇒ Object



121
122
123
# File 'lib/slugforge/models/tag_manager.rb', line 121

def tag_file_name(project_name, tag)
  [project_name, 'tags', tag].join('/')
end

#tags(project_name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/slugforge/models/tag_manager.rb', line 28

def tags(project_name)
  @tags[project_name] ||= begin
    return [] if bucket.files.nil?
    result = {}
    bucket.files.each do |file|
      result[$~[1]] = true if file.key =~ /^#{project_name}\/tags\/(.+)/
    end
    result.keys
  end
end

#tags_for_slug(project_name, slug_name) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/slugforge/models/tag_manager.rb', line 86

def tags_for_slug(project_name, slug_name)
  tags = tags(project_name)

  tags.select do |tag|
    slug_for_tag(project_name, tag) == slug_name
  end
end