Module: Acter

Defined in:
lib/acter.rb,
lib/acter/help.rb,
lib/acter/error.rb,
lib/acter/action.rb,
lib/acter/result.rb,
lib/acter/request.rb,
lib/acter/version.rb,
lib/acter/response.rb

Defined Under Namespace

Classes: Action, Error, Help, HelpWanted, InvalidAction, InvalidCommand, InvalidSchema, InvalidSubject, MissingParameters, NoSchema, Request, Response, Result

Constant Summary collapse

VERSION =
"0.1.2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.help_wantedObject Also known as: help_wanted?

Returns the value of attribute help_wanted.



58
59
60
# File 'lib/acter.rb', line 58

def help_wanted
  @help_wanted
end

.options_textObject

Returns the value of attribute options_text.



58
59
60
# File 'lib/acter.rb', line 58

def options_text
  @options_text
end

Class Method Details

.handle_invalid_command(exn) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acter.rb', line 27

def handle_invalid_command(exn)
  puts exn
  puts
  help = Help.new(exn.schema)
  case exn
  when HelpWanted, MissingParameters
    puts help.help_for_action(exn.action, exn.subject)
  when InvalidAction
    puts help.help_for_subject(exn.subject)
  else
    puts help.general_help
  end
end

.load_schema_data(path = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/acter.rb', line 10

def load_schema_data(path = nil)
  path ||= Pathname.glob("schema.{json,yml}").first or raise NoSchema
  if path.is_a?(String)
    uri = URI(path)
    source = uri.scheme ? uri : Pathname.new(path)
  elsif path.respond_to?(:read) && path.respond_to?(:to_s)
    source = path
  else
    raise ArgumentError, "Argument to load_schema must be a String or a Pathname-like object"
  end
  if source.to_s =~ /\.ya?ml$/
    YAML.load(source.read)
  else
    MultiJson.load(source.read)
  end
end

.program_nameObject



54
55
56
# File 'lib/acter.rb', line 54

def program_name
  @program_name ||= File.basename($0, ".rb")
end

.run(args, schema_path = nil, render_options = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/acter.rb', line 41

def run(args, schema_path = nil, render_options = nil)
  schema_data = load_schema_data(schema_path)
  action = Action.new(args, schema_data)
  result = action.send_request
  puts result.render(render_options)
  result.success?
rescue InvalidCommand => e
  handle_invalid_command(e)
rescue NoSchema
  raise unless args.empty? || args == %w"help"
  handle_invalid_command(InvalidCommand.new(nil))
end