Class: Pupu::Pupu

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params = Hash.new) ⇒ Pupu

Returns a new instance of Pupu.



109
110
111
112
113
# File 'lib/pupu/pupu.rb', line 109

def initialize(name, params = Hash.new)
  @name   = name.to_sym
  @path   = name.to_s
  @params = params
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



108
109
110
# File 'lib/pupu/pupu.rb', line 108

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



108
109
110
# File 'lib/pupu/pupu.rb', line 108

def params
  @params
end

Class Method Details

.[](plugin, params = Hash.new) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/pupu/pupu.rb', line 98

def [](plugin, params = Hash.new)
  plugin = plugin.to_s
  if self.all.include?(plugin)
    self.new(plugin, params)
  else
    raise PluginNotFoundError
  end
end

.allObject

TODO: return Pupu object, not string



65
66
67
68
69
# File 'lib/pupu/pupu.rb', line 65

def all
  Dir["#{self.root}/*"].select do |item|
    File.directory?(item)
  end.map { |entry| File.basename(entry).to_s }
end

.rootObject

Raises:

  • (Errno::ENOENT)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pupu/pupu.rb', line 76

def root
  # TODO: it should be configurable
  # root = ::Pupu.root.sub(%r[#{Regexp::quote(::Pupu.root)}], '').chomp("/")
  # root = "./" if root.empty?
  # case path
  #  when :absolute then File.join(root, ::Pupu.media_root, "pupu")
  #  when :relative then File.join(::Pupu.media_root, "pupu")
  #  else
  #    # exception
  #  end
  raise "Pupu.media_root has to be initialized" if ::Pupu.media_root.nil?
  raise Errno::ENOENT, "#{::Pupu.media_root}/pupu doesn't exist, you have to create it first!" unless File.directory?File.join(::Pupu.media_root, "pupu")
  @root ||= MediaPath.new(File.join(::Pupu.media_root, "pupu"))
end

.root=(directory) ⇒ Object

TODO: reflect changes on root method

Raises:



92
93
94
95
96
# File 'lib/pupu/pupu.rb', line 92

def root=(directory)
  @root = MediaPath.new(directory)
  raise PupuRootNotFound unless File.exist?(@root.to_s)
  return @root
end

.root_pathObject

same as root, but doesn’t raise any exception



72
73
74
# File 'lib/pupu/pupu.rb', line 72

def root_path
  File.join(::Pupu.media_root, "pupu")
end

Instance Method Details

#file(path, root = self.root) ⇒ Object



163
164
165
166
# File 'lib/pupu/pupu.rb', line 163

def file(path, root = self.root)
  root = MediaPath.new(root) if root.is_a?(String)
  root.join(path)
end

#image(basename) ⇒ Object



136
137
138
# File 'lib/pupu/pupu.rb', line 136

def image(basename)
  file("javascripts/#{image}")
end

#initializer(type = :all) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/pupu/pupu.rb', line 148

def initializer(type = :all)
  case type
  when :all
    [self.initializer(:javascript), self.initializer(:stylesheet)]
  when :javascript
    file("#{@path}.js", "#{::Pupu.media_root}/javascripts/initializers") rescue nil # TODO: fix media
  when :stylesheet
    file("#{@path}.css", "#{::Pupu.media_root}/stylesheets/initializers") rescue nil # TODO: fix media
  end
end

#javascript(basename) ⇒ Object



128
129
130
# File 'lib/pupu/pupu.rb', line 128

def javascript(basename)
  file("javascripts/#{basename}.js")
end

#metadataObject



119
120
121
122
123
124
125
126
# File 'lib/pupu/pupu.rb', line 119

def 
  return @metadata if @metadata
  path = self.file("metadata.yml").path
  hash = YAML::load_file(path)
  @metadata = OpenStruct.new(hash)
rescue Errno::ENOENT # we might remove pupu directory, so metadata are missing, but we can get them from cache
  @metadata
end

#rootObject



115
116
117
# File 'lib/pupu/pupu.rb', line 115

def root
  self.class.root.join(@path)
end

#soft_file(path, root = self.root) ⇒ Object



159
160
161
# File 'lib/pupu/pupu.rb', line 159

def soft_file(path, root = self.root)
  File.join(root.to_s, path.to_s) # for files which doesn't exist so far
end

#stylesheet(basename) ⇒ Object



132
133
134
# File 'lib/pupu/pupu.rb', line 132

def stylesheet(basename)
  file("stylesheets/#{basename}.css")
end

#uninstallObject



140
141
142
143
144
145
146
# File 'lib/pupu/pupu.rb', line 140

def uninstall
  FileUtils.rm_r self.root.to_s
  # TODO
  # self.metadata.dependants.each do |dependant|
  #   dependant.uninstall
  # end
end