Class: Detroit::Custom

Inherits:
Tool
  • Object
show all
Defined in:
lib/detroit-custom.rb

Overview

Custom tool is used to create “quicky” services.

This is a useful alternative to writing a full-blown plugin when the need is simple.

Constant Summary collapse

SPECIAL_OPTIONS =

Plural alias for #group. alias_accessor :groups, :group

%w{
  tool class track active priority project 
  trial trace verbose force quiet
}

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Custom

Instantiate new custom plugin.

This works by interpreting the service configuration as a hash of stop names to ruby code.



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/detroit-custom.rb', line 34

def initialize(options)
  super(options)

  @scripts = {}

  options.each do |stop, script|
    # skip specific names used for configuration
    next if SPECIAL_OPTIONS.include? stop
    # remaining options are names of group stops
    @scripts[stop.to_sym] = script
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a, &b) ⇒ Object (private)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/detroit-custom.rb', line 65

def method_missing(s, *a, &b)
  if s.to_s.end_with?('=')
  #  stop = s.to_s.chomp('=')
  #  if !SPECIAL_OPTIONS.include?(stop)
  #   (class << self; self; end).module_eval %{
  #      def station_#{stop}
  #        #{a.first}
  #      end
  #    }
  #  end
  else
    if @context.respond_to?(s)
      @context.__send__(s,*a,&b)
    else
      super(s, *a, &b)
    end
  end
end

Instance Method Details

#assemble(stop, info = {}) ⇒ Object



53
54
55
# File 'lib/detroit-custom.rb', line 53

def assemble(stop, info={})
  instance_eval(@scripts[stop])
end

#assemble?(stop, info = {}) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/detroit-custom.rb', line 48

def assemble?(stop, info={})
  @scripts.key?(stop)
end

#respond_to_missing?(name, privy) ⇒ Boolean (private)

RUBY 1.9

Returns:

  • (Boolean)


91
92
93
94
95
96
# File 'lib/detroit-custom.rb', line 91

def respond_to_missing?(name, privy)
  #return true if name.to_s.start_with?('station_')
  return true if name.to_s.end_with?('=')
  return true if @context.respond_to?(name)
  false
end