Module: SknUtils::Exploring::Commander

Defined in:
lib/skn_utils/exploring/commander.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_methods(mod, options) ⇒ Object

Commander’s class method



56
57
58
59
60
61
62
63
64
# File 'lib/skn_utils/exploring/commander.rb', line 56

def self.define_methods(mod, options)
  method_defs = []
  options.each_pair do |method_names, accessor|
    Array(method_names).map do |message|
      method_defs.push yield(message, accessor)
    end
  end
  mod.class_eval method_defs.join("\n"), __FILE__, __LINE__
end

Instance Method Details

#command(options) ⇒ Object

Forward messages and return self, protecting the encapsulation of the object



33
34
35
36
37
38
39
40
41
42
# File 'lib/skn_utils/exploring/commander.rb', line 33

def command(options)
  Commander.define_methods(self, options) do |command, accessor|
    %{
      def #{command}(*args, &block)
        #{accessor}.__send__(:#{command}, *args, &block)
        self
      end
    }
  end
end

#query(options) ⇒ Object

Forward messages and return the result of the forwarded message



45
46
47
48
49
50
51
52
53
# File 'lib/skn_utils/exploring/commander.rb', line 45

def query(options)
  Commander.define_methods(self, options) do |query, accessor|
    %{
      def #{query}(*args, &block)
        #{accessor}.__send__(:#{query}, *args, &block)
      end
    }
  end
end