Class: ModelScope::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/modelscope/callback.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, rails_callback:, name:, defining_class:) ⇒ Callback

Returns a new instance of Callback.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/modelscope/callback.rb', line 9

def initialize(model:, rails_callback:, name:, defining_class:)
  @model = model
  @callback = rails_callback
  @name = name
  @defining_class = defining_class

  analyzer = Analyzers::CallbackAnalyzer.new(@callback, model, defining_class)

  @kind = @callback.kind
  @method_name = @callback.filter
  @conditional = analyzer.conditional?
  @origin = analyzer.origin
  @inherited = analyzer.inherited?
  @association_generated = analyzer.association_generated?
  @attribute_generated = analyzer.attribute_generated?
  # fingerprint allows us to de-duplicate callbacks/validations;
  # in most cases, it's just an object_id, but for named validations/callbacks,
  # it's a combination of the name, kind and the method_name.
  # The "0" and "1" prefixes define how to handle duplicates (1 means last write wins, 0 means first write wins)
  @fingerprint = (@method_name.is_a?(Symbol) && @origin != :rails) ? ["1", @name, @kind, @method_name].join("-") : "0-#{@callback.object_id}"
end

Instance Attribute Details

#association_generatedObject (readonly)

Returns the value of attribute association_generated.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def association_generated
  @association_generated
end

#attribute_generatedObject (readonly)

Returns the value of attribute attribute_generated.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def attribute_generated
  @attribute_generated
end

#callbackObject (readonly)

Returns the value of attribute callback.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def callback
  @callback
end

#conditionalObject (readonly)

Returns the value of attribute conditional.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def conditional
  @conditional
end

#defining_classObject (readonly)

Returns the value of attribute defining_class.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def defining_class
  @defining_class
end

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def fingerprint
  @fingerprint
end

#inheritedObject (readonly)

Returns the value of attribute inherited.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def inherited
  @inherited
end

#kindObject (readonly)

Returns the value of attribute kind.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def kind
  @kind
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def method_name
  @method_name
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def model
  @model
end

#originObject (readonly)

Returns the value of attribute origin.



5
6
7
# File 'lib/modelscope/callback.rb', line 5

def origin
  @origin
end

Instance Method Details

#callback_groupObject



31
32
33
# File 'lib/modelscope/callback.rb', line 31

def callback_group
  @name.to_s
end

#human_method_nameObject



40
41
42
# File 'lib/modelscope/callback.rb', line 40

def human_method_name
  Analyzers::ValidationAnalyzer.human_method_name(@callback.filter)
end

#to_sObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/modelscope/callback.rb', line 44

def to_s
  [
    "#{model.name}: #{human_method_name}",
    "kind=#{kind}_#{callback_group}",
    "origin=#{origin}",
    inherited ? "inherited=true" : nil,
    conditional ? "conditional=true" : nil,
    association_generated ? "association=true" : nil,
    attribute_generated ? "attribute=true" : nil
  ].compact.join(" ")
end

#validation_typeObject



35
36
37
38
# File 'lib/modelscope/callback.rb', line 35

def validation_type
  return nil unless callback_group == "validate"
  Analyzers::ValidationAnalyzer.detect_type(@callback.filter, model)
end