Class: Module
- Inherits:
-
Object
- Object
- Module
- Defined in:
- lib/core_ext.rb
Instance Method Summary collapse
-
#follows_the_rules! ⇒ Object
This declares that the current class has a rulebook with rules that it wants it’s instances to follow.
Instance Method Details
#follows_the_rules! ⇒ Object
This declares that the current class has a rulebook with rules that it wants it’s instances to follow.
I picked this name because the probability of someone needing the method ‘follows_the_rules!’ (with a bang) is slim… hopefully.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/core_ext.rb', line 7 def follows_the_rules! extend(MetaTools) include(MetaTools) (:def_class_rule) do |what_to_capture, &class_block| class_eval { @rulebook ||= Rulebook.new }.add(what_to_capture, &class_block) # unless respond_to?(:method_missing) (:method_missing) do |meth, *args, &block| matching_rules = @rulebook[meth] unless matching_rules.empty? rule = matching_rules.first rule_block = rule.block rule_captures = rule[meth].captures || [] #rule_arity = rule_block.arity == -1 ? 0 : rule_block.arity # The above removes the possibility of optional arguments # Define the method unless .respond_to?(meth) (meth) do |*rule_args| rule_args = (rule_captures + rule_args)#.take(rule_arity) instance_exec(*rule_args, &rule_block) end end send(meth, *args, &block) else super(meth, *args, &block) end # matching_rules.empty? end # meta_def(:method_missing) # end # respond_to?(:method_missing) end # def_class_rule (:def_rule) do |what_to_capture, &class_block| (@rulebook ||= Rulebook.new).add(what_to_capture, &class_block) # unless respond_to?(:method_missing) define_method(:method_missing) do |meth, *args, &block| matching_rules = self.class.instance_variable_get(:@rulebook)[meth] unless matching_rules.empty? rule = matching_rules.first rule_block = rule.block rule_captures = rule[meth].captures || [] # rule_arity = rule_block.arity == -1 ? 0 : rule_block.arity # The above removes the possibility of optional arguments # Define the method unless .respond_to?(meth) (meth) do |*rule_args| rule_args = (rule_captures + rule_args)#.take(rule_arity) instance_exec(*rule_args, &rule_block) end end send(meth, *args, &block) else super(meth, *args, &block) end # matching_rules.empty? end # meta_def(:method_missing) # end # respond_to?(:method_missing) end # def_rule end |