Class: Object

Inherits:
BasicObject
Defined in:
lib/lolita/ruby_ext/accessors.rb

Instance Method Summary collapse

Instance Method Details

#lolita_accessor(*methods) ⇒ Object

Works similar as attr_accessor only reader method is changed to allow to set value (used for Lolita blocks in different classes).

Example

class Klass
  lolita_accessor :my_method
end
k=Klass.new
k.my_method("it's me")
puts k.my_method #=> it's me


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lolita/ruby_ext/accessors.rb', line 11

def lolita_accessor *methods
  if [Class,Module].include?(self.class)
    methods.each do |method|
      class_eval <<-ACCESSORS,__FILE__,__LINE__+1
      def #{method}(value=nil)
        @#{method}=value if value
        @#{method}
      end

      def #{method}=(value)
        @#{method}=value
      end
      ACCESSORS
    end
  end
end