Class: ActiveMethod::Base
- Inherits:
-
Object
- Object
- ActiveMethod::Base
- Defined in:
- lib/active_method/base.rb
Defined Under Namespace
Classes: Argument
Class Method Summary collapse
- .[](owner) ⇒ Object (also: on)
- .arguments ⇒ Object
- .call(*args, &block) ⇒ Object
- .inherited(subclass) ⇒ Object
- .keyword_arguments ⇒ Object
- .owner_name ⇒ Object
Instance Method Summary collapse
- #__set_owner(owner) ⇒ Object
- #call ⇒ Object
-
#initialize(*args) ⇒ Base
constructor
A new instance of Base.
Constructor Details
#initialize(*args) ⇒ Base
Returns a new instance of Base.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/active_method/base.rb', line 100 def initialize(*args) arguments.each do |index, argument| begin instance_variable_set("@#{argument.name}", args.fetch(index - 1)) rescue IndexError instance_variable_set("@#{argument.name}", argument.default) end end opts = args[arguments.count] || {} keyword_arguments.each do |argument| if opts.key?(argument.name.to_sym) instance_variable_set("@#{argument.name}", opts[argument.name.to_sym]) elsif opts.key?(argument.name.to_s) instance_variable_set("@#{argument.name}", opts[argument.name.to_s]) else instance_variable_set("@#{argument.name}", argument.default) end end end |
Class Method Details
.[](owner) ⇒ Object Also known as: on
15 16 17 |
# File 'lib/active_method/base.rb', line 15 def [](owner) Executor.new(self, owner) end |
.arguments ⇒ Object
24 25 26 |
# File 'lib/active_method/base.rb', line 24 def arguments class_variable_get(:@@arguments) end |
.call(*args, &block) ⇒ Object
20 21 22 |
# File 'lib/active_method/base.rb', line 20 def call(*args, &block) new(*args).call(&block) end |
.inherited(subclass) ⇒ Object
36 37 38 39 40 |
# File 'lib/active_method/base.rb', line 36 def inherited(subclass) subclass.init_arguments subclass.init_keyword_arguments subclass.init_owner_name end |
.keyword_arguments ⇒ Object
28 29 30 |
# File 'lib/active_method/base.rb', line 28 def keyword_arguments class_variable_get(:@@keyword_arguments) end |
.owner_name ⇒ Object
32 33 34 |
# File 'lib/active_method/base.rb', line 32 def owner_name class_variable_get(:@@owner_name) end |
Instance Method Details
#__set_owner(owner) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/active_method/base.rb', line 121 def __set_owner(owner) @__method_owner = owner @__owner_name = self.class.owner_name if @__owner_name.nil? klass = owner.is_a?(Class) ? owner : owner.class @__owner_name = Util.snake_case(klass.name.split("::").last) end self.define_singleton_method @__owner_name do @__method_owner end end |
#call ⇒ Object
135 136 |
# File 'lib/active_method/base.rb', line 135 def call end |