Class: Ame::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/ame-1.0/method.rb

Overview

A method in its defined state.

Defined Under Namespace

Classes: Undefined

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ruby_name, klass, description, options, arguments) ⇒ Method

Returns a new instance of Method.



22
23
24
25
# File 'lib/ame-1.0/method.rb', line 22

def initialize(ruby_name, klass, description, options, arguments)
  @ruby_name, @class, @description, @options, @arguments =
    ruby_name, klass, description, options, arguments
end

Instance Attribute Details

#argumentsArguments (readonly)

Returns The arguments of the receiver.

Returns:

  • (Arguments)

    The arguments of the receiver



63
64
65
# File 'lib/ame-1.0/method.rb', line 63

def arguments
  @arguments
end

#descriptionString (readonly)

Returns The description of the receiver.

Returns:

  • (String)

    The description of the receiver



57
58
59
# File 'lib/ame-1.0/method.rb', line 57

def description
  @description
end

#optionsOptions (readonly)

Returns The options of the receiver.

Returns:

  • (Options)

    The options of the receiver



60
61
62
# File 'lib/ame-1.0/method.rb', line 60

def options
  @options
end

Class Method Details

.name(ruby_name) ⇒ String

Returns The command-line version of RUBY_NAME, replacing any ‘_’ with ‘-’.

Parameters:

  • ruby_name (Symbol)

Returns:

  • (String)

    The command-line version of RUBY_NAME, replacing any ‘_’ with ‘-’



17
18
19
# File 'lib/ame-1.0/method.rb', line 17

def name(ruby_name)
  ruby_name.to_s.gsub('_', '-')
end

.ruby_name(name) ⇒ Symbol

Returns The Ruby version of NAME, possibly the name of the method given on the command-line, replacing any ‘-’ with ‘_’.

Parameters:

  • name (String)

Returns:

  • (Symbol)

    The Ruby version of NAME, possibly the name of the method given on the command-line, replacing any ‘-’ with ‘_’.



10
11
12
# File 'lib/ame-1.0/method.rb', line 10

def ruby_name(name)
  name.gsub('-', '_').to_sym
end

Instance Method Details

#call(instance, arguments = nil, options = nil) ⇒ self

Call the receiver’s method on INSTANCE with ARGUMENTS and OPTIONS, retrieving any default values for them if they’re nil.

Parameters:

  • instance (Class)
  • arguments (Array<Object>, nil) (defaults to: nil)
  • options (Hash<String, Object>) (defaults to: nil)

Returns:

  • (self)

Raises:



49
50
51
52
53
54
# File 'lib/ame-1.0/method.rb', line 49

def call(instance, arguments = nil, options = nil)
  options, _ = @options.process([]) unless options
  arguments ||= @arguments.process(options, [])
  instance.send @ruby_name, *(arguments + (options.empty? ? [] : [options]))
  self
end

#nameString

Returns The command-line name of the receiver.

Returns:

  • (String)

    The command-line name of the receiver



66
67
68
# File 'lib/ame-1.0/method.rb', line 66

def name
  @name ||= self.class.name(@ruby_name)
end

#process(instance, arguments) ⇒ self

Process ARGUMENTS as a set of Options and Arguments, then #call the receiver’s method on INSTANCE with them.

Parameters:

  • instance (Class)
  • arguments (Array<String>)

Returns:

  • (self)

Raises:



35
36
37
38
# File 'lib/ame-1.0/method.rb', line 35

def process(instance, arguments)
  options, remainder = @options.process(arguments)
  call(instance, @arguments.process(options, remainder), options)
end

#qualified_nameString

Returns The full command-line name of the receiver, including the class that this method belongs to’s Class.fullname.

Returns:

  • (String)

    The full command-line name of the receiver, including the class that this method belongs to’s Class.fullname



72
73
74
# File 'lib/ame-1.0/method.rb', line 72

def qualified_name
  [@class.fullname, name].reject(&:empty?).join(' ')
end