Module: Voke::ClassMethods

Defined in:
lib/voke.rb

Instance Method Summary collapse

Instance Method Details

#default(*args, &block) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/voke.rb', line 115

def default(*args, &block)
  value = block || args.pop
  argument = args.pop
  method = args.pop || :voke_next

  config = voke_config(method)
  config[:defaults][argument] = value
end

#description(method = :voke_next, string) ⇒ Object



110
111
112
113
# File 'lib/voke.rb', line 110

def description(method = :voke_next, string)
  config = voke_config(method)
  config[:description] = string
end

#method_added(method) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/voke.rb', line 124

def method_added(method)
  config = voke_config(method)

  return if config[:added]
  return if not public_method_defined?(method)

  config[:added] = true

  if next_config = @voke_config.delete(:voke_next)
    config[:description] ||= next_config[:description]
    config[:defaults] = next_config[:defaults].merge(config[:defaults])
  end

  orig_method = instance_method(method)

  define_method(method) do |*args, &block|
    if args.last.is_a?(Hash)
      options = args.pop

      config = self.class.voke_config(method)
      config[:defaults].each do |key, value|
        unless options[key]
          value = value.call(options) if value.is_a?(Proc) or value.is_a?(Method)
          options[key] = value
        end
      end

      args.push(options)
    end

    orig_method.bind(self).call(*args, &block)
  end
end

#voke_config(name) ⇒ Object



105
106
107
108
# File 'lib/voke.rb', line 105

def voke_config(name)
  @voke_config ||= Hash.new
  @voke_config[name] ||= { :defaults => Hash.new }
end