Module: Trax::Core::AbstractMethods::ClassMethods

Defined in:
lib/trax/core/abstract_methods.rb

Instance Method Summary collapse

Instance Method Details

#abstract_class_attribute(*method_names) ⇒ Object Also known as: abstract_class_attributes



25
26
27
28
# File 'lib/trax/core/abstract_methods.rb', line 25

def abstract_class_attribute(*method_names)
  _abstract_class_attributes.push(*method_names)
  class_attribute(*method_names)
end

#abstract_class_method(*method_names) ⇒ Object Also known as: abstract_class_methods



20
21
22
# File 'lib/trax/core/abstract_methods.rb', line 20

def abstract_class_method(*method_names)
  _abstract_class_methods.push(*method_names)
end

#abstract_method(*method_names) ⇒ Object Also known as: abstract_methods



15
16
17
# File 'lib/trax/core/abstract_methods.rb', line 15

def abstract_method(*method_names)
  _abstract_methods.push(*method_names)
end

#inherited(subklass) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/trax/core/abstract_methods.rb', line 31

def inherited(subklass)
  klass = super(subklass)

  trace = ::TracePoint.new(:end) do |tracepoint|
    if tracepoint.self == subklass #modules also trace end we only care about the class end
      trace.disable

      if _abstract_methods.any?
        missing_instance_methods = ( Array(_abstract_methods) - subklass.instance_methods(false) )

        raise NotImplementedError, "#{subklass} must implement the following instance method(s) \n" \
                                   << "#{missing_instance_methods}" if missing_instance_methods.any?
      end

      if _abstract_class_methods.any?
        missing_class_methods = ( Array(_abstract_class_methods) - subklass.singleton_methods(false) )

        raise NotImplementedError, "#{subklass} must implement the following class method(s) \n" \
                                    << "#{missing_class_methods}" if missing_class_methods.any?
      end

      if _abstract_class_attributes.any?
        missing_class_attributes = _abstract_class_attributes.select{|_property| subklass.__send__(_property).blank? }

        raise NotImplementedError, "#{subklass} must set the following class attributes(s) \n" \
                                    << "#{missing_class_attributes}" if missing_class_attributes.any?
      end
    end
  end

  trace.enable

  klass
end