Class: Caboodle::Kit

Inherits:
Sinatra::Base show all
Defined in:
lib/caboodle/kit.rb

Constant Summary collapse

Settings =
Hashie::Mash.new

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Sinatra::Base

#markdown

Class Attribute Details

Returns the value of attribute credit_link.



39
40
41
# File 'lib/caboodle/kit.rb', line 39

def credit_link
  @credit_link
end

Class Method Details

.add_layout(k, v) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/caboodle/kit.rb', line 165

def add_layout k, v
  unless Caboodle::Layout[k.to_sym].blank?
    Caboodle::Layout[k.to_sym] << "\n"
    Caboodle::Layout[k.to_sym] << v
  else
    Caboodle::Layout[k.to_sym] = v
  end
end

.add_sass(array_of_sass_files) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/caboodle/kit.rb', line 156

def add_sass array_of_sass_files
  if array_of_sass_files.class == Array
    array_of_sass_files.each { |a| Caboodle::SASS << a.to_s }
  else
    Caboodle::SASS << array_of_sass_files.to_s
  end
  Caboodle::SASS.uniq!
end

.add_to_layout(hash_of_items) ⇒ Object



196
197
198
199
200
# File 'lib/caboodle/kit.rb', line 196

def add_to_layout hash_of_items
  hash_of_items.each do |k,v|
    add_layout k, v
  end
end

.ask_user(r, optional = false) ⇒ Object



240
241
242
243
244
245
246
247
248
249
# File 'lib/caboodle/kit.rb', line 240

def ask_user r, optional=false       
  unless ENV["RACK_ENV"] == "production"
    puts " "
    opt = "Optional: " if optional
    puts "#{opt}Please set a value for #{r}:"
    v = STDIN.gets
    Caboodle::Site[r] = v
    Caboodle::Kit.dump_config
  end
end

.ask_user_for_all_missing_settingsObject



266
267
268
269
270
# File 'lib/caboodle/kit.rb', line 266

def ask_user_for_all_missing_settings
  Caboodle::Kits.each do |kit|
    kit.ask_user_for_missing_settings
  end
end

.ask_user_for_missing_settingsObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/caboodle/kit.rb', line 251

def ask_user_for_missing_settings
  required_settings.each do |r|
    if Caboodle::Site[r].blank?
      ask_user r
    end
    self.set r.to_s.to_sym, Caboodle::Site[r].to_s
  end
  optional_settings.each do |r|
    unless defined?(Caboodle::Site[r])
      ask_user r, true
    end
    self.set r.to_s.to_sym, Caboodle::Site[r].to_s
  end
end

.available_kitsObject



294
295
296
# File 'lib/caboodle/kit.rb', line 294

def available_kits
  Dir.new(File.join(File.dirname(__FILE__),"kits")).entries.delete_if{|a| a[0,1]=="."}
end

.config_files(array_of_files) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/caboodle/kit.rb', line 64

def config_files array_of_files
  configure do
    array_of_files.each do |file|
      config_path = File.expand_path(File.join(Caboodle::App.root,"config",file))
      if File.exists?(config_path)
        load_custom_config(config_path)
      else
        `cp "#{File.join(kit_root,"config",file)}" "#{File.join(Caboodle::App.root,"config",".")}"` rescue "Could not create the config yml file"
        puts "\nThis kit has a separate configuration file which you must edit:\n #{File.expand_path(File.join(Caboodle::App.root,"config",file))}\n"
      end
    end
  end
end

.configure_site(configuration_yaml_path) ⇒ Object



284
285
286
287
288
# File 'lib/caboodle/kit.rb', line 284

def configure_site configuration_yaml_path
  self.set :config_path, configuration_yaml_path
  Caboodle::Config.load_config(open(configuration_yaml_path).read)
  Caboodle::Config.setup
end

.credit(url, title = nil) ⇒ Object



208
209
210
211
212
213
# File 'lib/caboodle/kit.rb', line 208

def credit url, title=nil
  #todo there must be an easier way
  class_eval "def credit_link
  \"<a rel='me' href='#{url}'>#{title || self.name.split("::").last}</a>\"
  end"
end

.defaults(hash) ⇒ Object



202
203
204
205
206
# File 'lib/caboodle/kit.rb', line 202

def defaults hash
  if hash.class == Hash
    hash.each {|k,v| Site[k] = v }
  end
end

.description(string) ⇒ Object



89
90
91
92
# File 'lib/caboodle/kit.rb', line 89

def description string
  puts "\n"
  puts "#{name.to_s.split("::").last}: #{string}"
end

.dump_configObject



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/caboodle/kit.rb', line 272

def dump_config
  begin
    p = config_path
    d = Caboodle::Site.clone
    e = d.to_hash
    e.delete("required_settings")
    File.open(p, 'w') {|f| f.write(YAML::dump(e))}
  rescue
    puts "Cannot write to config file: #{p}"
  end
end

.files(array_of_files) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/caboodle/kit.rb', line 78

def files array_of_files
  configure do
    array_of_files.each do |file|
      target_path = File.expand_path(File.join(Caboodle::App.root,"config",file))
      unless File.exists?(target_path)              
        `cp "#{File.join(kit_root,"config",file)}" "#{File.join(Caboodle::App.root,"config",".")}"` rescue "Could not create the config yml file"
      end
    end
  end
end

.inherited(subclass) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/caboodle/kit.rb', line 41

def inherited subclass
  set :kit_root, File.expand_path(File.dirname(caller[0].split(/:in/).last))
  c = caller[0].split(":")
  f = File.dirname(File.expand_path("#{c[0]}"))
  views = File.join(f, "views")
  pub = File.join(f, "public")
  subclass.set :views, views if File.exists?(views)
  subclass.set :public, pub if File.exists?(pub)
  super
end

.is_a_caboodle_kitObject



52
53
54
# File 'lib/caboodle/kit.rb', line 52

def is_a_caboodle_kit
  true
end

.javascripts(array_of_js_files) ⇒ Object Also known as: javascript



176
177
178
179
180
181
182
183
# File 'lib/caboodle/kit.rb', line 176

def javascripts array_of_js_files
  if array_of_js_files.class == Array
    array_of_js_files.each { |a| Caboodle::Javascripts << a.to_s }
  else
    Caboodle::Javascripts << array_of_js_files.to_s
  end
  Caboodle::Javascripts.uniq!
end

.kit_nameObject



290
291
292
# File 'lib/caboodle/kit.rb', line 290

def kit_name
  self.ancestors.first.to_s.split("::").last
end

.load_custom_config(p) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/caboodle/kit.rb', line 56

def load_custom_config p
  loaded = YAML.load_file(p)
  Hashie::Mash.new(loaded).each{ |k,v| 
    v.strip! if v.class == String
    Settings[k.to_s] = v 
  }
end


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/caboodle/kit.rb', line 98

def menu display, path=nil, &block
  Settings.menu_items ||= [] 
  
  #todo proper slugify
  slug = display.downcase.gsub(" ","-").gsub("'","")
  path = "/#{slug}" unless path
  path = "/" if Site.home_kit == self.to_s.gsub("Caboodle::","") && !Settings.menu_items.include?("/")
  Caboodle::MenuItems << {:display=>display, :link=>path, :kit=>self}
  
  if block
    self.get path, &block
  else
    eval "self.get '#{path}' do
    @title = '#{display}'
    haml :#{slug.gsub("-","_")}
    end"
  end
  
  eval "before do
    @title = '#{display}'
  end"
  
  Settings.menu_items << path
end

.nameObject



94
95
96
# File 'lib/caboodle/kit.rb', line 94

def name
  self.to_s.split("::").last
end

.optional(keys) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/caboodle/kit.rb', line 135

def optional keys
  if keys.class == Array
    keys.each do |k| 
      self.optional_settings << k
      self.set k.to_s.to_sym, Caboodle::Site[k].to_s
    end
  else
    self.optional_settings << keys
  end
  self.optional_settings      
end

.optional_settingsObject



220
221
222
223
# File 'lib/caboodle/kit.rb', line 220

def optional_settings
  OptionalSettings[kit_name] ||= [] 
  OptionalSettings[kit_name]
end

.register_kit(ask = true) ⇒ Object



225
226
227
228
229
230
231
# File 'lib/caboodle/kit.rb', line 225

def register_kit ask=true
  ask_user_for_missing_settings if ask
  Site.kits << name
  Site.kits.uniq!
  Caboodle::Kits << self
  Caboodle::Kits
end

.required(keys) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/caboodle/kit.rb', line 123

def required keys
  if keys.class == Array
    keys.each do |k| 
      self.required_settings << k
      self.set k.to_s.to_sym, Caboodle::Site[k].to_s
    end
  else
    self.required_settings << keys
  end
  self.required_settings
end

.required_settingsObject



215
216
217
218
# File 'lib/caboodle/kit.rb', line 215

def required_settings
  RequiredSettings[kit_name] ||= [] 
  RequiredSettings[kit_name]
end

.rss(array_of_feeds) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/caboodle/kit.rb', line 187

def rss array_of_feeds
  if array_of_feeds.class == Array
    array_of_feeds.each { |a| Caboodle::RSS << a.to_s }
  else
    Caboodle::RSS << array_of_feeds.to_s
  end
  Caboodle::RSS.uniq!
end

.startObject



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/caboodle/kit.rb', line 298

def start
  errors = []
  self.required_settings.each do |s|
    if Site[s].blank?
      errors << "    :#{s} has not been set"
    end
  end

  if errors.empty?
    Caboodle::App.use self 
  else
    STDERR.puts " "
    STDERR.puts "Warning - #{kit_name} is disabled because:"
    STDERR.puts errors.join("\n") 
    Caboodle::Errors << Hashie::Mash.new(:title=>"#{kit_name} is disable", :reason=>errors.join(";"))
  end
end

.stylesheets(array_of_css_files) ⇒ Object Also known as: stylesheet



147
148
149
150
151
152
153
154
# File 'lib/caboodle/kit.rb', line 147

def stylesheets array_of_css_files
  if array_of_css_files.class == Array
    array_of_css_files.each { |a| Caboodle::Stylesheets << a.to_s }
  else
    Caboodle::Stylesheets << array_of_css_files.to_s
  end
  Caboodle::Stylesheets.uniq!
end

.unregister_kitObject



233
234
235
236
237
238
# File 'lib/caboodle/kit.rb', line 233

def unregister_kit
  Caboodle::Kits.delete(self)
  Caboodle::Site.kits.delete(self.to_s)
  Caboodle::Kit.dump_config
  Caboodle::Kits
end