Class: Granite::Form::Model::Associations::Base
- Inherits:
-
Object
- Object
- Granite::Form::Model::Associations::Base
- Defined in:
- lib/granite/form/model/associations/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#owner ⇒ Object
Returns the value of attribute owner.
-
#reflection ⇒ Object
Returns the value of attribute reflection.
Instance Method Summary collapse
- #callback(name, object) ⇒ Object
- #evar_loaded? ⇒ Boolean
-
#initialize(owner, reflection) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #loaded! ⇒ Object
- #loaded? ⇒ Boolean
- #reload ⇒ Object
- #reset ⇒ Object
- #target ⇒ Object
- #transaction ⇒ Object
Constructor Details
#initialize(owner, reflection) ⇒ Base
Returns a new instance of Base.
9 10 11 12 13 14 |
# File 'lib/granite/form/model/associations/base.rb', line 9 def initialize(owner, reflection) @owner = owner @reflection = reflection @evar_loaded = owner.persisted? reset end |
Instance Attribute Details
#owner ⇒ Object
Returns the value of attribute owner.
6 7 8 |
# File 'lib/granite/form/model/associations/base.rb', line 6 def owner @owner end |
#reflection ⇒ Object
Returns the value of attribute reflection.
6 7 8 |
# File 'lib/granite/form/model/associations/base.rb', line 6 def reflection @reflection end |
Instance Method Details
#callback(name, object) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/granite/form/model/associations/base.rb', line 44 def callback(name, object) evaluator = reflection.[name] return true unless evaluator if evaluator.is_a?(Proc) if evaluator.arity == 1 owner.instance_exec(object, &evaluator) else evaluator.call(owner, object) end else owner.send(evaluator, object) end end |
#evar_loaded? ⇒ Boolean
21 22 23 |
# File 'lib/granite/form/model/associations/base.rb', line 21 def evar_loaded? !!@evar_loaded end |
#inspect ⇒ Object
68 69 70 |
# File 'lib/granite/form/model/associations/base.rb', line 68 def inspect "#<#{reflection.macro.to_s.camelize} #{target.inspect.truncate(50, omission: collection? ? '...]' : '...')}>" end |
#loaded! ⇒ Object
29 30 31 32 |
# File 'lib/granite/form/model/associations/base.rb', line 29 def loaded! @evar_loaded = true @loaded = true end |
#loaded? ⇒ Boolean
25 26 27 |
# File 'lib/granite/form/model/associations/base.rb', line 25 def loaded? !!@loaded end |
#reload ⇒ Object
39 40 41 42 |
# File 'lib/granite/form/model/associations/base.rb', line 39 def reload reset target end |
#reset ⇒ Object
16 17 18 19 |
# File 'lib/granite/form/model/associations/base.rb', line 16 def reset @loaded = false @target = nil end |
#target ⇒ Object
34 35 36 37 |
# File 'lib/granite/form/model/associations/base.rb', line 34 def target return @target if loaded? self.target = load_target end |
#transaction ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/granite/form/model/associations/base.rb', line 59 def transaction data = read_source.deep_dup yield rescue StandardError => e write_source data reload raise e end |