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.



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

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.



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

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Class Method Details

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



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

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

.allObject

TODO: return Pupu object, not string



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

def all
  files = Dir["#{self.root_path}/*"]
  dirs = files.select(&File.method(:directory?))
  dirs.map(&File.method(:basename))
end

.rootObject

Raises:

  • (Errno::ENOENT)


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

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, "#{self.root_path} doesn't exist, you have to create it first!" unless File.directory?(self.root_path)
  @root ||= MediaPath.new(self.root_path)
end

.root=(directory) ⇒ Object

TODO: reflect changes on root method

Raises:



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

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



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

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

Instance Method Details

#copy_initializersObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/pupu/pupu.rb', line 176

def copy_initializers
  js_initializer = initializer(:javascript)
  css_initializer = initializer(:stylesheet)
  
  if js_initializer && (not File.exist?("#{::Pupu.media_root}/javascripts/initializers/#{File.basename(js_initializer.to_s)}"))
    FileUtils.mkdir_p("#{::Pupu.media_root}/javascripts/initializers")
    FileUtils.mv js_initializer.to_s, "#{::Pupu.media_root}/javascripts/initializers/#{File.basename(js_initializer.to_s)}"
  end
  
  if css_initializer && (not File.exist?("#{::Pupu.media_root}/stylesheets/initializers/#{File.basename(css_initializer.to_s)}"))
    FileUtils.mkdir_p("#{::Pupu.media_root}/stylesheets/initializers")
    FileUtils.mv css_initializer.to_s, "#{::Pupu.media_root}/stylesheets/initializers/#{File.basename(css_initializer.to_s)}"
  end
end

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



195
196
197
198
199
200
# File 'lib/pupu/pupu.rb', line 195

def file(path, root = self.root)
  root = MediaPath.new(root) if root.is_a?(String)
  root.join(path)
rescue Errno::ENOENT
  raise AssetNotFound, "#{soft_file(path, root)}"
end

#image(basename) ⇒ Object



139
140
141
# File 'lib/pupu/pupu.rb', line 139

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

#initializer(type = :all) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/pupu/pupu.rb', line 151

def initializer(type = :all)
  case type
  when :all
    [self.initializer(:javascript), self.initializer(:stylesheet)]
  when :javascript
    begin
      file("#{@path}.js", "#{root}/initializers")
    rescue AssetNotFound
      file("#{@path}.js", "#{::Pupu.media_root}/javascripts/initializers")
    rescue
      nil
    end
  when :stylesheet
    begin
      file("#{@path}.css", "#{root}/initializers")
    rescue AssetNotFound
      file("#{@path}.css", "#{::Pupu.media_root}/stylesheets/initializers")
    rescue
      nil
    end
  else
    raise Exception, "#{type.to_s} is not know type of initializer"
  end
end

#javascript(basename) ⇒ Object



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

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

#metadataObject



121
122
123
124
125
126
127
128
129
# File 'lib/pupu/pupu.rb', line 121

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

#rootObject



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

def root
  # self.class.root.join(@path)
  File.join(self.class.root_path, @path)
end

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



191
192
193
# File 'lib/pupu/pupu.rb', line 191

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



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

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

#uninstallObject



143
144
145
146
147
148
149
# File 'lib/pupu/pupu.rb', line 143

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