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
- .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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_method/base.rb', line 79 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 @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 |
.keyword_arguments ⇒ Object
28 29 30 |
# File 'lib/active_method/base.rb', line 28 def keyword_arguments @keyword_arguments ||= [] end |
.owner_name ⇒ Object
32 33 34 35 36 |
# File 'lib/active_method/base.rb', line 32 def owner_name return @owner_name unless @owner_name.nil? return if self == ActiveMethod::Base superclass.owner_name end |
Instance Method Details
#__set_owner(owner) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/active_method/base.rb', line 100 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
114 115 |
# File 'lib/active_method/base.rb', line 114 def call end |