Class: Serve::Application
- Inherits:
-
Object
- Object
- Serve::Application
- Defined in:
- lib/serve/application.rb
Defined Under Namespace
Classes: InvalidArgumentsError
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #help ⇒ Object
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #parse(args) ⇒ Object
- #run(args = ARGV) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
16 17 18 |
# File 'lib/serve/application.rb', line 16 def initialize self. = {} end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
10 11 12 |
# File 'lib/serve/application.rb', line 10 def @options end |
Class Method Details
.run(args = ARGV) ⇒ Object
12 13 14 |
# File 'lib/serve/application.rb', line 12 def self.run(args = ARGV) new.run(args) end |
Instance Method Details
#help ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/serve/application.rb', line 57 def help program = File.basename($0) [ "Usage:", " #{program} [port] [environment] [port]", " #{program} [address:port] [environment] [port]", " #{program} [options]", " ", "Description:", " Starts a WEBrick server on the specified address and port with its document ", " root set to the current working directory. (Optionally, you can specify the ", " directory as the last parameter.) By default the command uses 0.0.0.0 for ", " the address and 4000 for the port. This means that once the command has ", " been started you can access the documents in the current directory with any ", " Web browser at:", " ", " http://localhost:4000/", " ", " If the haml, redcloth, or bluecloth gems are installed the command can serve ", " Haml, Sass, Textile, and Markdown for documents with haml, sass, textile, ", " and markdown file extensions.", " ", " If the Rails command script/server exists in the current directory the ", " script will start that instead with the specified environment or the ", " development environment if none is specified. Rails apps are started by ", " default on port 3000.", " ", "Options:", " -h, --help Show this message and quit.", " -v, --version Show the program version number and quit." ].join("\n") end |
#parse(args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/serve/application.rb', line 41 def parse(args) args = normalize_args(args) [:help] = extract_boolean(args, '-h', '--help') [:version] = extract_boolean(args, '-v', '--version') [:environment] = extract_environment(args) [:root] = extract_root(args) [:address] = extract_address(args) [:port] = extract_port(args) raise InvalidArgumentsError if args.size > 0 end |
#run(args = ARGV) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/serve/application.rb', line 20 def run(args = ARGV) @options = parse(args) case when [:version] puts version when [:help] puts help else Dir.chdir([:root]) if rails_app? run_rails_app else run_server end end rescue InvalidArgumentsError puts "invalid arguments" puts puts help end |