Class: Guise::Options Private
- Inherits:
-
Object
- Object
- Guise::Options
- Defined in:
- lib/guise/options.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Instance Attribute Summary collapse
- #association_class ⇒ Object private
- #association_name ⇒ Object readonly private
- #association_name_singular ⇒ Object readonly private
- #association_options ⇒ Object readonly private
- #attribute ⇒ Object readonly private
- #scopes ⇒ Object readonly private
- #source_class ⇒ Object readonly private
- #values ⇒ Object readonly private
Instance Method Summary collapse
- #default_association_options ⇒ Object private
-
#initialize(source_class, *values) ⇒ Options
constructor
private
A new instance of Options.
- #register_scope(guise_value, scope) ⇒ Object private
- #scope(guise_value, scope_type) ⇒ Object private
- #source_association_name ⇒ Object private
Constructor Details
#initialize(source_class, *values) ⇒ Options
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Options.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/guise/options.rb', line 16 def initialize(source_class, *values) = values. if values.empty? raise ArgumentError, "must specify values in `has_guises`" end @source_class = source_class @values = values.map(&:to_s).to_set @association_name = .delete(:association) || DEFAULT_ASSOCIATION_NAME @association_name_singular = @association_name.to_s.singularize @attribute = .delete(:attribute) || DEFAULT_ATTRIBUTE_NAME @association_options = .reverse_merge!() @scopes = values.inject(HashWithIndifferentAccess.new) do |all, value| all.merge!(value => {}) end @scopes.freeze end |
Instance Attribute Details
#association_class ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
59 60 61 62 63 64 65 |
# File 'lib/guise/options.rb', line 59 def association_class if defined?(@association_class) @association_class else raise "`guise_for` was not called on the association class" end end |
#association_name ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
4 5 6 |
# File 'lib/guise/options.rb', line 4 def association_name @association_name end |
#association_name_singular ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
4 5 6 |
# File 'lib/guise/options.rb', line 4 def association_name_singular @association_name_singular end |
#association_options ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
4 5 6 |
# File 'lib/guise/options.rb', line 4 def @association_options end |
#attribute ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
4 5 6 |
# File 'lib/guise/options.rb', line 4 def attribute @attribute end |
#scopes ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
4 5 6 |
# File 'lib/guise/options.rb', line 4 def scopes @scopes end |
#source_class ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
4 5 6 |
# File 'lib/guise/options.rb', line 4 def source_class @source_class end |
#values ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
4 5 6 |
# File 'lib/guise/options.rb', line 4 def values @values end |
Instance Method Details
#default_association_options ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 |
# File 'lib/guise/options.rb', line 71 def { foreign_key: "#{source_association_name}_id".to_sym } end |
#register_scope(guise_value, scope) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/guise/options.rb', line 47 def register_scope(guise_value, scope) value_scopes = @scopes.fetch(guise_value) do raise InvalidGuiseValue.new(guise_value, source_class) end if value_scopes.key?(scope.type) raise "`#{scope.type}' scope already defined for #{source_class}" end value_scopes[scope.type] = scope end |
#scope(guise_value, scope_type) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
37 38 39 40 41 42 43 44 45 |
# File 'lib/guise/options.rb', line 37 def scope(guise_value, scope_type) value_scopes = @scopes.fetch(guise_value) do raise InvalidGuiseValue.new(guise_value, source_class) end value_scopes.fetch(scope_type) do raise ArgumentError, "`#{scope_type}' is not a valid type of scope" end end |
#source_association_name ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
67 68 69 |
# File 'lib/guise/options.rb', line 67 def source_association_name source_class.model_name.singular.to_sym end |