Class: ActiveMethod::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_method/base.rb

Defined Under Namespace

Classes: Argument

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/active_method/base.rb', line 83

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

.argumentsObject



24
25
26
# File 'lib/active_method/base.rb', line 24

def arguments
  class_variable_get(:@@arguments)
end

.call(*args) ⇒ Object



20
21
22
# File 'lib/active_method/base.rb', line 20

def call(*args)
  new(*args).call
end

.inherited(subclass) ⇒ Object



32
33
34
35
# File 'lib/active_method/base.rb', line 32

def inherited(subclass)
  subclass.init_arguments
  subclass.init_keyword_arguments
end

.keyword_argumentsObject



28
29
30
# File 'lib/active_method/base.rb', line 28

def keyword_arguments
  class_variable_get(:@@keyword_arguments)
end

Instance Method Details

#__set_owner(owner) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/active_method/base.rb', line 104

def __set_owner(owner)
  @__method_owner = owner

  klass = owner.is_a?(Class) ? owner : owner.class
  instance_name = Util.snake_case(klass.name.split("::").last)
  self.define_singleton_method instance_name do
    @__method_owner
  end
end

#callObject



114
115
# File 'lib/active_method/base.rb', line 114

def call
end