Module: EnumeratedAttribute::Attribute::ClassMethods

Defined in:
lib/enumerated_attribute/attribute/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#define_enumerated_attribute_custom_method(symbol, attr_name, value, negated) ⇒ Object



36
37
38
39
40
41
# File 'lib/enumerated_attribute/attribute/class_methods.rb', line 36

def define_enumerated_attribute_custom_method(symbol, attr_name, value, negated)
  define_method symbol do
    ival = read_enumerated_attribute(attr_name)
    negated ? ival != value : ival == value
  end
end

#enumerated_attribute_allows_nil?(name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/enumerated_attribute/attribute/class_methods.rb', line 27

def enumerated_attribute_allows_nil?(name)
  (descriptor = self.enumerated_attributes[name.to_sym]) && descriptor.allows_nil?
end

#enumerated_attribute_allows_value?(name, value) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/enumerated_attribute/attribute/class_methods.rb', line 30

def enumerated_attribute_allows_value?(name, value)
  return (false) unless (descriptor = self.enumerated_attributes[name.to_sym])
  return descriptor.allows_nil? if (value == nil || value == '')
  descriptor.allows_value?(value)
end

#enumerated_attributes(all = true) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/enumerated_attribute/attribute/class_methods.rb', line 7

def enumerated_attributes(all=true)
  return @enumerated_attributes unless all
  return @all_enumerated_attributes_cache if @all_enumerated_attributes_cache
  @all_enumerated_attributes_cache = @enumerated_attributes ?  @enumerated_attributes.dup : {}
  klass = self.superclass
  while (klass)
    if (klass.respond_to?(:enumerated_attributes))
      if (sub_enums = klass.enumerated_attributes)
        @all_enumerated_attributes_cache = sub_enums.merge @all_enumerated_attributes_cache
        break
      end
    end
    klass = klass.superclass
  end
  @all_enumerated_attributes_cache
end

#has_enumerated_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/enumerated_attribute/attribute/class_methods.rb', line 24

def has_enumerated_attribute?(name)
  !name.nil? && !!self.enumerated_attributes.key?(name.to_sym)
end

#refresh_enumerated_attributesObject



4
5
6
# File 'lib/enumerated_attribute/attribute/class_methods.rb', line 4

def refresh_enumerated_attributes
  @all_enumerated_attributes_cache = nil
end