Class: ModelScope::Callback
- Inherits:
-
Object
- Object
- ModelScope::Callback
- Defined in:
- lib/modelscope/callback.rb
Instance Attribute Summary collapse
-
#association_generated ⇒ Object
readonly
Returns the value of attribute association_generated.
-
#attribute_generated ⇒ Object
readonly
Returns the value of attribute attribute_generated.
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#conditional ⇒ Object
readonly
Returns the value of attribute conditional.
-
#defining_class ⇒ Object
readonly
Returns the value of attribute defining_class.
-
#fingerprint ⇒ Object
readonly
Returns the value of attribute fingerprint.
-
#inherited ⇒ Object
readonly
Returns the value of attribute inherited.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
Instance Method Summary collapse
- #callback_group ⇒ Object
- #human_method_name ⇒ Object
-
#initialize(model:, rails_callback:, name:, defining_class:) ⇒ Callback
constructor
A new instance of Callback.
- #to_s ⇒ Object
- #validation_type ⇒ Object
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_generated ⇒ Object (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_generated ⇒ Object (readonly)
Returns the value of attribute attribute_generated.
5 6 7 |
# File 'lib/modelscope/callback.rb', line 5 def attribute_generated @attribute_generated end |
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
5 6 7 |
# File 'lib/modelscope/callback.rb', line 5 def callback @callback end |
#conditional ⇒ Object (readonly)
Returns the value of attribute conditional.
5 6 7 |
# File 'lib/modelscope/callback.rb', line 5 def conditional @conditional end |
#defining_class ⇒ Object (readonly)
Returns the value of attribute defining_class.
5 6 7 |
# File 'lib/modelscope/callback.rb', line 5 def defining_class @defining_class end |
#fingerprint ⇒ Object (readonly)
Returns the value of attribute fingerprint.
5 6 7 |
# File 'lib/modelscope/callback.rb', line 5 def fingerprint @fingerprint end |
#inherited ⇒ Object (readonly)
Returns the value of attribute inherited.
5 6 7 |
# File 'lib/modelscope/callback.rb', line 5 def inherited @inherited end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
5 6 7 |
# File 'lib/modelscope/callback.rb', line 5 def kind @kind end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
5 6 7 |
# File 'lib/modelscope/callback.rb', line 5 def method_name @method_name end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/modelscope/callback.rb', line 5 def model @model end |
#origin ⇒ Object (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_group ⇒ Object
31 32 33 |
# File 'lib/modelscope/callback.rb', line 31 def callback_group @name.to_s end |
#human_method_name ⇒ Object
40 41 42 |
# File 'lib/modelscope/callback.rb', line 40 def human_method_name Analyzers::ValidationAnalyzer.human_method_name(@callback.filter) end |
#to_s ⇒ Object
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_type ⇒ Object
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 |