Class: Guise::Options Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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)
  options = values.extract_options!

  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 =
    options.delete(:association) || DEFAULT_ASSOCIATION_NAME
  @association_name_singular = @association_name.to_s.singularize
  @attribute = options.delete(:attribute) || DEFAULT_ATTRIBUTE_NAME
  @association_options = options.reverse_merge!(default_association_options)

  @scopes = values.inject(HashWithIndifferentAccess.new) do |all, value|
    all.merge!(value => {})
  end
  @scopes.freeze
end

Instance Attribute Details

#association_classObject

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_nameObject (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_singularObject (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_optionsObject (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
  @association_options
end

#attributeObject (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

#scopesObject (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_classObject (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

#valuesObject (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_optionsObject

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 default_association_options
  { 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_nameObject

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