Class: SocialSnippet::Repository::Drivers::BaseRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/social_snippet/repository/drivers/base_repository.rb

Overview

Repository base class usage: class GitRepository < BaseRepository

Direct Known Subclasses

GitRepository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_path, new_ref = nil) ⇒ BaseRepository

Constructor


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 23

def initialize(new_path, new_ref = nil)
  @path = new_path
  @cache_path = nil
  @ref = new_ref

  if ( not ref.nil? ) && ( not refs.empty? ) && ( not refs.include?(ref) )
    raise Errors::NotExistRef
  end

  @url = nil
end

Instance Attribute Details

#cache_pathObject (readonly)

Returns the value of attribute cache_path


11
12
13
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 11

def cache_path
  @cache_path
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies


16
17
18
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 16

def dependencies
  @dependencies
end

#descObject (readonly)

Returns the value of attribute desc


13
14
15
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 13

def desc
  @desc
end

#mainObject (readonly)

Returns the value of attribute main


14
15
16
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 14

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name


12
13
14
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 12

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path


10
11
12
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 10

def path
  @path
end

#refObject (readonly)

Returns the value of attribute ref


15
16
17
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 15

def ref
  @ref
end

#urlObject (readonly)

Returns the value of attribute url


18
19
20
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 18

def url
  @url
end

Instance Method Details

#checkout(new_ref) ⇒ Object

Checkout to ref


168
169
170
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 168

def checkout(new_ref)
  raise "not implement checkout"
end

#commit_idString

Returns current ref's commit id


141
142
143
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 141

def commit_id
  raise "not implement commit_id"
end

#create_cache(base_cache_path) ⇒ String

Create cache of repository


54
55
56
57
58
59
60
61
62
63
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 54

def create_cache(base_cache_path)
  @cache_path = "#{base_cache_path}/#{name}/#{short_commit_id}"

  return if ::Dir.exists?(cache_path)

  ::FileUtils.mkdir_p "#{base_cache_path}/#{name}"
  ::FileUtils.cp_r path, cache_path

  return cache_path
end

#current_refObject


120
121
122
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 120

def current_ref
  raise "not implete current_ref"
end

#glob(glob_path) ⇒ Object

Returns the directory or file names within repository


148
149
150
151
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 148

def glob(glob_path)
  root_path = real_path("/")
  ::Dir.glob("#{root_path}/#{glob_path}")
end

#has_versions?Boolean

Check repository has version ref


175
176
177
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 175

def has_versions?
  versions.empty? === false
end

#is_cached?Boolean

Check repository is cached


182
183
184
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 182

def is_cached?
  @cache_path.nil? === false
end

#latest_version(pattern = "") ⇒ String

Returns latest version


107
108
109
110
111
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 107

def latest_version(pattern = "")
  pattern = "" if pattern.nil?
  matched_versions = versions.select {|ref| ::SocialSnippet::Version.is_matched_version_pattern(pattern, ref)}
  return ::VersionSorter.rsort(matched_versions).first
end

#load_cache(base_cache_path) ⇒ Object


40
41
42
43
44
45
46
47
48
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 40

def load_cache(base_cache_path)
  cache_path_cand = "#{base_cache_path}/#{name}/#{short_commit_id}"

  if ::Dir.exists?(cache_path_cand)
    return @cache_path = cache_path_cand
  end

  return @cache_path = nil
end

#load_snippet_jsonObject

Load snippet.json file


66
67
68
69
70
71
72
73
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 66

def load_snippet_json
  text = ::File.read(::File.join(path, "snippet.json"))
  snippet_json = ::JSON.parse(text)
  @name = snippet_json["name"]
  @desc = snippet_json["desc"]
  @main = snippet_json["main"] || ""
  @dependencies = snippet_json["dependencies"] || {}
end

#real_path(target_path) ⇒ Object

Returns the real path of given path


156
157
158
159
160
161
162
163
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 156

def real_path(target_path)
  # TODO: normalize path
  if is_cached?
    "#{cache_path}/#{main}/#{target_path}"
  else
    "#{path}/#{main}/#{target_path}"
  end
end

#refsArray<String>

Returns all refs


127
128
129
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 127

def refs
  raise "not implement refs"
end

#set_url(new_url) ⇒ Object

Set repo's URL


36
37
38
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 36

def set_url(new_url)
  @url = new_url
end

#short_commit_idString

Returns short current ref's commit id


134
135
136
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 134

def short_commit_id
  commit_id[0..7]
end

#to_snippet_jsonObject

Returns json text from repo instance


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 78

def to_snippet_json
  required = [
    :name,
  ]

  optional = [
    :desc,
    :main,
    :dependencies,
  ]

  data = {}
  required.each do |key|
    data[key] = send(key)
  end
  optional.each do |key|
    val = send(key)
    if val.nil? === false && val != ""
      data[key] = send(key)
    end
  end

  return data.to_json
end

#update(options = {}) ⇒ Object


186
187
188
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 186

def update(options = {})
  throw "not implement update"
end

#versionsArray<String>

Returns all versions


116
117
118
# File 'lib/social_snippet/repository/drivers/base_repository.rb', line 116

def versions
  refs.select {|ref| ::SocialSnippet::Version.is_version(ref) }
end