Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/filament.rb

Instance Method Summary collapse

Instance Method Details

#attr_accessor2(*symbols) ⇒ Object

creates a reader and a writer for the given attributes. both reader and writer are the same method. if the method is called without arguments, the current value is returned if it is called with a single arguemnt, the value is set.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/filament.rb', line 26

def attr_accessor2(*symbols)
#    attr_writer(*symbols)
  symbols.each do |sym|
    attr_sym = "@#{sym.to_s}".to_sym
    define_method(sym) do |*val|
      if val.empty?
        return instance_variable_get(attr_sym)
      else
        val = val.first if val.size == 1
        return instance_variable_set(attr_sym, val)
      end
    end
  end
end

#intercept_method(method, temporary_result) ⇒ Object

temporarily intercepts a given method, evaluating the given block



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/filament.rb', line 43

def intercept_method(method, temporary_result)
  original_method = "#{Time.now.to_i}_#{method}"
  alias_method original_method, method

  # Not enough room for a ternary ;_;
  t = if temporary_result.respond_to?(:to_proc)
    temporary_result.to_proc
  else
    Proc.new { temporary_result }
  end

  define_method(method, t)
  yield
  alias_method method, original_method
end