Class: Vimpack::Models::Script

Inherits:
Object
  • Object
show all
Includes:
Utils::Github, Utils::Vimscripts
Defined in:
lib/vimpack/models/script.rb

Defined Under Namespace

Classes: AlreadyInstalled, NotInstalled, ScriptNotFound

Constant Summary collapse

SCRIPT_TYPES =
[ 'utility', 'color scheme', 'syntax', 'ftplugin',
'indent', 'game', 'plugin', 'patch', 'github' ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Github

included

Methods included from Utils::Vimscripts

included

Constructor Details

#initialize(attrs = {}) ⇒ Script

Returns a new instance of Script.



22
23
24
25
# File 'lib/vimpack/models/script.rb', line 22

def initialize(attrs={})
  set_attributes_from_input(attrs)
  set_attributes_from_github
end

Instance Attribute Details

#authorObject (readonly)

From Vimscripts.org



13
14
15
# File 'lib/vimpack/models/script.rb', line 13

def author
  @author
end

#author_emailObject (readonly)

From Vimscripts.org



13
14
15
# File 'lib/vimpack/models/script.rb', line 13

def author_email
  @author_email
end

#descriptionObject (readonly)

From Github



15
16
17
# File 'lib/vimpack/models/script.rb', line 15

def description
  @description
end

#nameObject (readonly)

From Vimscripts.org



13
14
15
# File 'lib/vimpack/models/script.rb', line 13

def name
  @name
end

#repo_ownerObject (readonly)

From awesomeness



17
18
19
# File 'lib/vimpack/models/script.rb', line 17

def repo_owner
  @repo_owner
end

#typeObject (readonly)

From Vimscripts.org



13
14
15
# File 'lib/vimpack/models/script.rb', line 13

def type
  @type
end

#urlObject (readonly)

From Github



15
16
17
# File 'lib/vimpack/models/script.rb', line 15

def url
  @url
end

#versionObject (readonly)

From Vimscripts.org



13
14
15
# File 'lib/vimpack/models/script.rb', line 13

def version
  @version
end

#version_dateObject (readonly)

From Vimscripts.org



13
14
15
# File 'lib/vimpack/models/script.rb', line 13

def version_date
  @version_date
end

Class Method Details

.get(name) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vimpack/models/script.rb', line 66

def self.get(name)
  # If the name has a slash in it, then it's URLy and it's a straight github repo
  _type = name.include?('/') ? :github : :vimscript
  case _type
  when :github
    repo_key = name.split("/")[-2..-1].join("/").split(".").first # wut demeter?
    repo = repository(repo_key)
    script_hash =
      { :name => repo.name, :type => 'github',
        :description => repo.description, :script_version => '',
        :author => repo.owner., :author_email => '',
        :repo_owner => repo.owner.
      }
  when :vimscript
    script_hash = get_vimscript(name)
    raise ScriptNotFound.new(name) if script_hash.nil?
  end
  Script.new(script_hash)
end

.info(name) ⇒ Object



86
87
88
# File 'lib/vimpack/models/script.rb', line 86

def self.info(name)
  get(name)
end

.search(q, types = [], limit = 10, offset = 0) ⇒ Object



60
61
62
63
64
# File 'lib/vimpack/models/script.rb', line 60

def self.search(q, types = [], limit = 10, offset = 0)
  search_vimscripts(q, types).map do |vimscript|
    Script.new(vimscript)
  end
end

Instance Method Details

#bundle_pathObject



130
131
132
# File 'lib/vimpack/models/script.rb', line 130

def bundle_path
  Repo.bundle_path.join(name)
end

#commitsObject



44
45
46
47
48
# File 'lib/vimpack/models/script.rb', line 44

def commits
  @commits ||= self.class.commits(github_short_url).sort do |a, b|
    a.authored_date <=> b.authored_date
  end
end

#github_short_urlObject



33
34
35
# File 'lib/vimpack/models/script.rb', line 33

def github_short_url
  @github_short_url ||= "#{repo_owner}/#{name}"
end

#install!(link_to_bundle = true) ⇒ Object

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vimpack/models/script.rb', line 90

def install!(link_to_bundle=true)
  Repo.raise_unless_initialized!
  raise AlreadyInstalled.new(name) if installed?
  case type
  when 'github'
    Repo.add_submodule(url, type.gsub(' ', '_'), repo_owner, name)
  else
    Repo.add_submodule(url, type.gsub(' ', '_'), name)
  end
  if link_to_bundle
    Repo.create_link(install_path, bundle_path)
  end
  true
end

#install_pathObject



121
122
123
124
125
126
127
128
# File 'lib/vimpack/models/script.rb', line 121

def install_path
  case type
  when 'github'
    Repo.script_path.join(type.gsub(' ', '_'), repo_owner, name)
  else
    Repo.script_path.join(type.gsub(' ', '_'), name)
  end
end

#installable?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/vimpack/models/script.rb', line 117

def installable?
  Repo.initialized? && !installed?
end

#installed?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/vimpack/models/script.rb', line 113

def installed?
  Repo.initialized? && Repo.directory_exists?(install_path)
end

#set_attributes_from_githubObject



37
38
39
40
41
42
# File 'lib/vimpack/models/script.rb', line 37

def set_attributes_from_github
  @repo = self.class.repo(github_short_url)
  instance_variable_set("@description".to_sym, @repo[:description])
  instance_variable_set("@url".to_sym, @repo[:clone_url])
  set_version_from_github
end

#set_attributes_from_input(attrs) ⇒ Object



27
28
29
30
31
# File 'lib/vimpack/models/script.rb', line 27

def set_attributes_from_input(attrs)
  attrs.each_pair do |attr, value|
    instance_variable_set("@#{attr}".to_sym, value)
  end
end

#set_version_from_githubObject



50
51
52
53
54
55
56
57
58
# File 'lib/vimpack/models/script.rb', line 50

def set_version_from_github
  last_commit = commits.first
  if type == 'github'
    @version = last_commit.sha
  else
    @version = last_commit.commit.message.split(':')[0][0..10].gsub(/Version /, '')
  end
  @version_date = last_commit.commit.author.date
end

#uninstall!Object

Raises:



105
106
107
108
109
110
111
# File 'lib/vimpack/models/script.rb', line 105

def uninstall!
  Repo.raise_unless_initialized!
  raise NotInstalled.new(name) unless installed?
  Repo.remove_submodule(type.gsub(' ', '_'), name)
  Repo.remove_link(bundle_path) if Repo.symlink_exists?(bundle_path)
  true
end