Module: Boson::Commands::WebCore

Extended by:
WebCore
Included in:
WebCore
Defined in:
lib/boson/commands/web_core.rb

Defined Under Namespace

Classes: Get

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.def_which_requires(meth, *libs, &block) ⇒ Object

Requires libraries only once before defining method with given block



24
25
26
27
28
29
# File 'lib/boson/commands/web_core.rb', line 24

def self.def_which_requires(meth, *libs, &block)
  define_method(meth) do |*args|
    libs.each {|e| require e }
    define_method(meth, block).call(*args)
  end
end

Instance Method Details

#browser(*urls) ⇒ Object

non-mac users should override this with the launchy gem



65
66
67
# File 'lib/boson/commands/web_core.rb', line 65

def browser(*urls)
  system('open', *urls)
end

#configObject

:nodoc:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/boson/commands/web_core.rb', line 4

def config #:nodoc:
  commands = {
    'get'=>{ :desc=>"Gets the body of a url", :args=>[['url'],['options', {}]]},
    'post'=>{ :desc=>'Posts to a url', :args=>[['url'],['options', {}]]},
    'build_url'=>{ :desc=>"Builds a url, escaping the given params", :args=>[['url'],['params']]},
    'browser'=>{ :desc=>"Opens urls in a browser on a Mac"},
    'install'=>{ :desc=>"Installs a library by url. Library should then be loaded with load_library.",
      :args=>[['url'],['options', {}]],
      :options=> { :name=>{:type=>:string, :desc=>"Library name to save to"},
        :force=>{:type=>:boolean, :desc=>'Overwrites an existing library'},
        :default=>{:type=>:boolean, :desc=>'Adds library as a default library to main config file'},
        :module_wrap=>{:type=>:boolean, :desc=>"Wraps a module around install using library name"},
        :method_wrap=>{:type=>:boolean, :desc=>"Wraps a method and module around installed library using library name"}}
    }
  }

  {:library_file=>File.expand_path(__FILE__), :commands=>commands, :namespace=>false}
end

#install(url, options = {}) ⇒ Object

:nodoc:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/boson/commands/web_core.rb', line 49

def install(url, options={}) #:nodoc:
  options[:name] ||= strip_name_from_url(url)
  return puts("Please give a library name for this url.") if options[:name].empty?
  filename = File.join ::Boson.repo.commands_dir, "#{options[:name]}.rb"
  return puts("Library name #{options[:name]} already exists. Try a different name.") if File.exists?(filename) && !options[:force]

  file_string = get(url) or raise "Unable to fetch url"
  file_string = "# Originally from #{url}\n"+file_string
  file_string = wrap_install(file_string, options) if options[:method_wrap] || options[:module_wrap]

  File.open(filename, 'w') {|f| f.write file_string }
  Boson.repo.update_config {|c| (c[:defaults] ||= []) << options[:name] } if options[:default]
  puts "Saved to #{filename}."
end