Class: Module

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

Instance Method Summary collapse

Instance Method Details

#const_replace(name, value) ⇒ Object



67
68
69
70
# File 'lib/r_kit/utility/module_extend.rb', line 67

def const_replace name, value
  remove_const name
  const_set name, value
end

#namespaceObject



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

def namespace
  (deconstantize.presence || 'Object').constantize
end

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



54
55
56
# File 'lib/r_kit/utility/module_extend.rb', line 54

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

#tap_attr_accessor(*names, typecast: nil) ⇒ 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



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/r_kit/utility/module_extend.rb', line 39

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