Class: Module

Inherits:
Object show all
Defined in:
lib/r_kit/utility/module_extend.rb

Instance Method Summary collapse

Instance Method Details

#attr_reader(*names, default: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/r_kit/utility/module_extend.rb', line 8

def attr_reader *names, default: nil
  if default
    names.each do |name|
      define_method name do
        instance_variable_get("@#{ name }") ||
          instance_variable_set("@#{ name }", default.is_a?(Proc) ? default.call(self, name) : default)
      end
    end
  else
    basic_attr_reader *names
  end
end

#basic_attr_readerObject



7
# File 'lib/r_kit/utility/module_extend.rb', line 7

alias :basic_attr_reader :attr_reader

#singleton_attr_reader(*args, **options) ⇒ Object



40
41
42
# File 'lib/r_kit/utility/module_extend.rb', line 40

def singleton_attr_reader *args, **options
  singleton_class.send :attr_reader, *args, **options
end

#tap_attr_accessor(*names) ⇒ Object

TODO: these writter are called like “.name(value)” to set and return self or like “.name” to read TODO: to be used in ‘pagination’, these need an “after” callback (to set @limited_collection to nil) TODO: and to be used in ‘grid (base.rb, binding_accessor)’, these need an “to” delegation object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/r_kit/utility/module_extend.rb', line 26

def tap_attr_accessor *names
  names.each do |name|
    define_method name, ->(value = nil) do
      if value
        instance_variable_set "@#{ name }", value
        self
      else
        instance_variable_get "@#{ name }"
      end
    end
  end
end