Module: ExtendIt::Class

Included in:
AdminIt::Context, AdminIt::Field, AdminIt::Filter, AdminIt::Resource
Defined in:
lib/extend_it/class.rb

Instance Method Summary collapse

Instance Method Details

#call_inherited(method_name, *args, base_first: false, &block) ⇒ Object



15
16
17
18
19
# File 'lib/extend_it/class.rb', line 15

def call_inherited(method_name, *args, base_first: false, &block)
  arr = parents.select { |parent| parent.methods.include?(method_name) }
  arr.reverse! if base_first == true
  arr.reduce([]) { |a, e| a << e.send(method_name, *args, &block) }
end

#class_attr_reader(*attrs) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/extend_it/class.rb', line 31

def class_attr_reader(*attrs)
  attrs.flatten.uniq.each do |attr_name|
    attr_name = attr_name.to_sym if attr_name.is_a?(String)
    next unless attr_name.is_a?(Symbol)
    next if instance_methods.include?(attr_name)
    var_name = "@#{attr_name}".to_sym
    if methods.include?(attr_name)
      define_method(attr_name) { self.class.send(attr_name) }
    elsif instance_variable_defined?(var_name)
      define_method attr_name do
        self.class.instance_variable_get(var_name)
      end
    end
  end
end

#inherited_class_reader(*names) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/extend_it/class.rb', line 21

def inherited_class_reader(*names)
  names.ensure_symbols.each do |name|
    var = "@#{name}"
    define_singleton_method(name) do
      p = parents.find { |parent| parent.instance_variable_defined?(var) }
      p.nil? ? nil : p.instance_variable_get(var)
    end
  end
end