Class: Module

Inherits:
Object show all
Defined in:
lib/kiss/ext/core.rb

Instance Method Summary collapse

Instance Method Details

#_attr_accessor(*syms) ⇒ Object

protected attribute (ivar) accessor



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kiss/ext/core.rb', line 67

def _attr_accessor(*syms)
  class_eval(
    syms.flatten.map do |sym|
      sym.is_a?(Hash) ? '' : %Q(
        @_#{sym} = nil unless defined? @_#{sym}
        def #{sym}; @_#{sym}; end
        def #{sym}=(v); @_#{sym} = v; end
      )
    end.join, __FILE__, __LINE__
  )
end

#_attr_reader(*syms) ⇒ Object

protected attribute (ivar) reader



55
56
57
58
59
60
61
62
63
64
# File 'lib/kiss/ext/core.rb', line 55

def _attr_reader(*syms)
  class_eval(
    syms.flatten.map do |sym|
      sym.is_a?(Hash) ? '' : %Q(
        @_#{sym} = nil unless defined? @_#{sym}
        def #{sym}; @_#{sym}; end
      )
    end.join, __FILE__, __LINE__
  )
end

#cattr_accessor(*syms) ⇒ Object

adapted from Rails, re-written for speed (only one class_eval call)



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kiss/ext/core.rb', line 40

def cattr_accessor(*syms)
  class_eval(
    syms.flatten.map do |sym|
      sym.is_a?(Hash) ? '' : %Q(
        @@#{sym} = nil unless defined? @@#{sym}
        def self.#{sym}; @@#{sym}; end
        def self.#{sym}=(v); @@#{sym} = v; end
        def #{sym}; @@#{sym}; end
        def #{sym}=(v); @@#{sym} = v; end
      )
    end.join, __FILE__, __LINE__
  )
end

#cattr_reader(*syms) ⇒ Object

adapted from Rails, re-written for speed (only one class_eval call)



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kiss/ext/core.rb', line 27

def cattr_reader(*syms)
  class_eval(
    syms.flatten.map do |sym|
      sym.is_a?(Hash) ? '' : %Q(
        @@#{sym} = nil unless defined? @@#{sym}
        def self.#{sym}; @@#{sym}; end
        def #{sym}; @@#{sym}; end
      )
    end.join, __FILE__, __LINE__
  )
end

#dsl_accessor(*symbols) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kiss/ext/core.rb', line 79

def dsl_accessor(*symbols)
  class_eval(
    symbols.map { |sym|
      %{
        def #{sym}(*val)
          if val.empty?
            @_#{sym}
          else
            @_#{sym} = val.size == 1 ? val[0] : val
          end
        end
        def #{sym}=(val)
          @_#{sym} = val
        end
      }
    }.join)
end