Class: ActiveShepherd::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/active_shepherd/method.rb

Direct Known Subclasses

ApplyMethod, QueryMethod

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Method

Returns a new instance of Method.



16
17
18
19
20
21
22
# File 'lib/active_shepherd/method.rb', line 16

def initialize(*args)
  @aggregate    = args.shift
  @associations = {}
  @attributes   = {}

  setup *args
end

Instance Attribute Details

#aggregateObject (readonly)

Returns the value of attribute aggregate.



14
15
16
# File 'lib/active_shepherd/method.rb', line 14

def aggregate
  @aggregate
end

#associationsObject (readonly)

Returns the value of attribute associations.



3
4
5
# File 'lib/active_shepherd/method.rb', line 3

def associations
  @associations
end

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/active_shepherd/method.rb', line 3

def attributes
  @attributes
end

Class Method Details

.inherited(base) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/active_shepherd/method.rb', line 5

def self.inherited(base)
  # If you're looking for magic, you've come to the right place
  return unless base.name.match /Methods::/
  apply_or_query, state_or_changes = base.name.demodulize.underscore.split('_', 2)
  method_name = "#{apply_or_query}_#{state_or_changes}"
  action_proc = ->(*args) { new(*args).send(method_name) }
  base.singleton_class.send :define_method, method_name, &action_proc
end

Instance Method Details

#recurse(model, foreign_key) ⇒ Object



24
25
26
# File 'lib/active_shepherd/method.rb', line 24

def recurse(model, foreign_key)
  Aggregate.new model, foreign_key
end

#traverse!Object



28
29
30
31
32
33
34
# File 'lib/active_shepherd/method.rb', line 28

def traverse!
  Traversal.new(
    self,
    attributes: attributes,
    associations: associations,
  ).traverse
end