Module: Guise::Introspection

Defined in:
lib/guise/introspection.rb

Overview

Introspection handles checking if a record has one or more guise records associated with it.

Instance Method Summary collapse

Instance Method Details

#has_any_guises?(*values) ⇒ true, false

Checks if the record has any guise records with identified by any of the specified values.

Parameters:

  • value (Array<String, Class, Symbol>)

    guise to check

Returns:

  • (true, false)


30
31
32
# File 'lib/guise/introspection.rb', line 30

def has_any_guises?(*values)
  values.any? { |value| has_guise?(value) }
end

#has_guise?(value) ⇒ true, false

Checks if the record has a guise record identified by on the specified value.

Parameters:

  • value (String, Class, Symbol)

    guise to check

Returns:

  • (true, false)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/guise/introspection.rb', line 12

def has_guise?(value)
  value = value.to_s.classify

  unless guise_options.values.include?(value)
    raise ArgumentError, "no such guise #{value}"
  end

  association(guise_options.association_name).reader.any? do |record|
    !record.marked_for_destruction? &&
      record[guise_options.attribute] == value
  end
end

#has_guises?(*values) ⇒ true, false

Checks if the record has guise records for all of the specified values.

Parameters:

  • value (Array<String, Class, Symbol>)

    guise to check

Returns:

  • (true, false)


39
40
41
# File 'lib/guise/introspection.rb', line 39

def has_guises?(*values)
  values.all? { |value| has_guise?(value) }
end