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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vimpack/models/script.rb', line 62

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 =~ /\//) ? :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



82
83
84
# File 'lib/vimpack/models/script.rb', line 82

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

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



56
57
58
59
60
# File 'lib/vimpack/models/script.rb', line 56

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



126
127
128
# File 'lib/vimpack/models/script.rb', line 126

def bundle_path
  Repo.bundle_path.join(name)
end

#commitsObject



40
41
42
43
44
# File 'lib/vimpack/models/script.rb', line 40

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

#install!(link_to_bundle = true) ⇒ Object

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vimpack/models/script.rb', line 86

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



117
118
119
120
121
122
123
124
# File 'lib/vimpack/models/script.rb', line 117

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)


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

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

#installed?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/vimpack/models/script.rb', line 109

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

#set_attributes_from_githubObject



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

def set_attributes_from_github
  url = "#{repo_owner}/#{name}"
  @repo = self.class.repo(url)
  [ :url, :description ].each { |attr| instance_variable_set("@#{attr}".to_sym, @repo[attr]) }
  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



46
47
48
49
50
51
52
53
54
# File 'lib/vimpack/models/script.rb', line 46

def set_version_from_github
  last_commit = commits.last
  if type == 'github'
    @version = last_commit.id
  else
    @version = last_commit.message[0..10].gsub(/Version /, '')
  end
  @version_date = last_commit.authored_date
end

#uninstall!Object

Raises:



101
102
103
104
105
106
107
# File 'lib/vimpack/models/script.rb', line 101

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