Class: ActiveDelegate::Delegator
- Inherits:
-
Object
- Object
- ActiveDelegate::Delegator
- Defined in:
- lib/active_delegate/delegator.rb
Overview
Validates delegation target and sets basic options
Direct Known Subclasses
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #association_class ⇒ Object
- #association_name ⇒ Object
- #association_reflection ⇒ Object
- #default_options ⇒ Object
- #delegation_args(available = []) ⇒ Object
- #delegation_options ⇒ Object
- #delegation_prefix ⇒ Object
-
#initialize(model, options) ⇒ Delegator
constructor
A new instance of Delegator.
Constructor Details
#initialize(model, options) ⇒ Delegator
Returns a new instance of Delegator.
8 9 10 11 |
# File 'lib/active_delegate/delegator.rb', line 8 def initialize(model, ) @model = model @options = .merge(.symbolize_keys) end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/active_delegate/delegator.rb', line 6 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/active_delegate/delegator.rb', line 6 def @options end |
Instance Method Details
#association_class ⇒ Object
52 53 54 |
# File 'lib/active_delegate/delegator.rb', line 52 def association_class association_reflection.klass end |
#association_name ⇒ Object
41 42 43 |
# File 'lib/active_delegate/delegator.rb', line 41 def association_name @options[:to] end |
#association_reflection ⇒ Object
45 46 47 48 49 50 |
# File 'lib/active_delegate/delegator.rb', line 45 def association_reflection reflection = model.reflect_on_association(association_name) return reflection unless reflection.nil? raise "#{model.name} don't have the association #{association_name}" end |
#default_options ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/active_delegate/delegator.rb', line 13 def { except: [], only: [], to: nil, allow_nil: false } end |
#delegation_args(available = []) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/active_delegate/delegator.rb', line 26 def delegation_args(available = []) included = Array([:only]).map(&:to_sym) excluded = Array([:except]).map(&:to_sym) available = Array(available).map(&:to_sym) available &= included if included.any? available -= excluded if excluded.any? available end |
#delegation_options ⇒ Object
22 23 24 |
# File 'lib/active_delegate/delegator.rb', line 22 def .select { |k, _v| k.in? %i[to allow_nil prefix] } end |
#delegation_prefix ⇒ Object
36 37 38 39 |
# File 'lib/active_delegate/delegator.rb', line 36 def delegation_prefix prefix = [:prefix] prefix == true ? association_name : prefix end |