Module: Hudkins::Mixin

Included in:
Job
Defined in:
lib/hudkins/mixin.rb

Overview

extend Hudkins::Mixin to include DSL style methods

Instance Method Summary collapse

Instance Method Details

#attr_accessor_from_config(*args) ⇒ Object

combines attr_reader,writer,post_from_config



97
98
99
100
101
# File 'lib/hudkins/mixin.rb', line 97

def attr_accessor_from_config *args
  attr_reader_from_config *args
  attr_writer_from_config *args
  attr_post_from_config *args
end

#attr_post_from_config(method_name, search_path, type = :default) ⇒ Object

Description

see attr_reader_from_config

(optionally) update config node and immediately post back the config then return the updated value

‘!’ is added to end of method_name to signify post call



85
86
87
88
89
90
91
92
93
# File 'lib/hudkins/mixin.rb', line 85

def attr_post_from_config method_name, search_path, type = :default
  attr_reader_from_config method_name, search_path, type unless self.respond_to? method_name
  define_method "#{method_name}!".to_sym do |arg|
    # complains if arg isn't given..
    send("#{method_name}=", arg) if arg
    post_config!
    self.send(method_name)
  end
end

#attr_reader_from_config(method_name, search_path, type = :default) ⇒ Object

Description

Similar to attr_reader but takes additional parameters

:bool type addes a method_name? alias

Examples:

class MyObj
  attr_reader_from_config :disabled, "//project//disabled", :bool
end

MyObj.new.disabled? # => true

Parameters:

method_name

attr_reader method name

search_path

Nokogiri.at search path

type

:fixnum # => converts config content to integer

:bool # => creates a boolean reader method. useful

when the config returns string "true" but True class is
desirable

Class # => ie. a value of ‘Integer’ will result in

typecasting via Integer(value)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hudkins/mixin.rb', line 36

def attr_reader_from_config method_name, search_path, type = :default
  # could we do something like inspect el.children.size? for arrays?
  define_method method_name do
    el = config.at(search_path)
    if el
      case type
      when :bool then
        /true/i === el.content
      when :fixnum, :int then
        el.content.to_i
      when :array then
        warn ":array not implemented yet"
        #value = []
        #el.children
      when Class then
        # Integer(value)
        Kernel.send(type.name, el.content)
      else
        el.content
      end
    else
      warn "`#{search_path}' was not found in config."
    end
  end
  # use alias_method instead of explicitely defining the method so I can
  # call the method_name without the ? in internal methods.
  alias_method "#{method_name}?".to_sym, method_name if :bool === type
end

#attr_writer_from_config(method_name, search_path, type = :default) ⇒ Object

Description

see attr_reader_from_config

update config node



70
71
72
73
74
75
# File 'lib/hudkins/mixin.rb', line 70

def attr_writer_from_config method_name, search_path, type = :default
  define_method "#{method_name}=".to_sym do |arg|
    config.at(search_path).content = arg
    arg
  end
end