Class: MPlayer::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_mplayer/commands.rb

Overview

:nodoc:all

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_name, opt_list) ⇒ Command

Returns a new instance of Command.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/easy_mplayer/commands.rb', line 31

def initialize(command_name, opt_list)
  @cmd   = command_name
  @min   = 0
  @max   = opt_list.length
  @names = opt_list
  @opts  = opt_list.map do |opt|
    @min += 1 if opt[0,1] != '['
    case opt
    when 'Integer', '[Integer]' then :int
    when 'Float',   '[Float]'   then :float
    when 'String',  '[String]'  then :string
    else raise "Unknown cmd option type: #{opt}"
    end
  end
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



29
30
31
# File 'lib/easy_mplayer/commands.rb', line 29

def cmd
  @cmd
end

#maxObject (readonly)

Returns the value of attribute max.



29
30
31
# File 'lib/easy_mplayer/commands.rb', line 29

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



29
30
31
# File 'lib/easy_mplayer/commands.rb', line 29

def min
  @min
end

#namesObject (readonly)

Returns the value of attribute names.



29
30
31
# File 'lib/easy_mplayer/commands.rb', line 29

def names
  @names
end

#optsObject (readonly)

Returns the value of attribute opts.



29
30
31
# File 'lib/easy_mplayer/commands.rb', line 29

def opts
  @opts
end

Class Method Details

.cmdlist_rawObject



4
5
6
# File 'lib/easy_mplayer/commands.rb', line 4

def cmdlist_raw
  @cmdlist_raw ||= `mplayer -input cmdlist`.split(/\n/)
end

.find(name) ⇒ Object



17
18
19
# File 'lib/easy_mplayer/commands.rb', line 17

def find(name)
  list[name.to_sym]
end

.listObject



8
9
10
11
12
13
14
15
# File 'lib/easy_mplayer/commands.rb', line 8

def list
  @list ||= returning Hash.new do |hsh|
    cmdlist_raw.map do |line|
      cmd, *opts = line.split(/\s+/)
      hsh[cmd.to_sym] = new(cmd, opts)
    end
  end
end

.validate!(args) ⇒ Object

Raises:



21
22
23
24
25
26
# File 'lib/easy_mplayer/commands.rb', line 21

def validate!(args)
  cmd = args.shift
  obj = find(cmd)
  raise BadCallName.new(cmd, args) unless obj
  obj.validate!(args)
end

Instance Method Details

#convert_arg_type(val, type) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/easy_mplayer/commands.rb', line 59

def convert_arg_type(val, type)
  begin
    case type
    when :int    then Integer(val)
    when :float  then Float(val)
    when :string then val.to_s
    end
  rescue
    nil
  end
end

#inspectObject



55
56
57
# File 'lib/easy_mplayer/commands.rb', line 55

def inspect
  "#<#{self.class} \"#{usage}\">"
end

#to_sObject



51
52
53
# File 'lib/easy_mplayer/commands.rb', line 51

def to_s
  usage
end

#usageObject



47
48
49
# File 'lib/easy_mplayer/commands.rb', line 47

def usage
  "#{cmd}(" + names.join(", ") + ")"
end

#validate!(args) ⇒ Object

Raises:



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/easy_mplayer/commands.rb', line 71

def validate!(args)
  len = args.length
  raise BadCallArgs.new(self, args, "not enough args") if len < min
  raise BadCallArgs.new(self, args, "too many args")   if len > max
  returning Array.new do |new_args|
    args.each_with_index do |x,i|
      new_args.push convert_arg_type(x, opts[i]) or
        raise BadCallArgs.new(self, args, "type mismatch")
    end
  end
end