Module: Overridable::ModuleMixin::ClassMethods

Defined in:
lib/overridable.rb

Instance Method Summary collapse

Instance Method Details

#append_features(mod) ⇒ Object

:nodoc:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/overridable.rb', line 90

def append_features mod #:nodoc:
  # these must be done in `append_features`, not `included`
  mod.send :include, Overridable

  methods =
    if @override_options && !@override_options[:only].empty?
        @override_options[:only]
    else
      all_methods = 
        public_instance_methods +
        protected_instance_methods +
        private_instance_methods

      if @override_options && !@override_options[:except].empty?
        all_methods - @override_options[:except]
      else
        all_methods
      end
    end

  mod.overrides *methods
  super
end

#overrides(options = {}) ⇒ Object

Whitelist and blacklist for to-be-overrided methods. If this method with the same options is called multiple times within the same module, only the last call will work.

Examples:

overrides :except => [:foo, :bar]
overrides :except => :baz
overrides :only => [:foo, :bar]

Parameters:

  • options (Hash) (defaults to: {})

    the options describe methods are to be or not to be overrided.

Options Hash (options):

  • :only (Symbol, Array<Symbol>)

    only methods specified in this option will be overrided.

  • :except (Symbol, Array<Symbol>)

    methods specified in this option will not be overrided.

Raises:

  • (ArgumentError)


124
125
126
127
128
129
130
131
# File 'lib/overridable.rb', line 124

def overrides options = {}
  raise ArgumentError, "Only :only and :except options are accepted." unless
    options.keys.all? { |k| [:only, :except].include? k }
  @override_options ||= {:only => [], :except => []}
  @override_options[:only] = [options[:only]].flatten.compact if options[:only]
  @override_options[:except] = [options[:except]].flatten.compact if options[:except]
  @override_options[:only] = @override_options[:only] - @override_options[:except]
end