Module: Class::EnumeratedStateClassMethods

Defined in:
lib/enumerated_state.rb

Instance Method Summary collapse

Instance Method Details

#enumerated_state_property_exists?(enum_attr, prop_name) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/enumerated_state.rb', line 89

def enumerated_state_property_exists?(enum_attr, prop_name)
  @_enumerated_state[enum_attr].has_key?(prop_name) rescue false
end

#get_enumerated_state_property(enum_attr, prop_name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/enumerated_state.rb', line 73

def get_enumerated_state_property(enum_attr, prop_name)
  if @_enumerated_state.has_key?(enum_attr)
    if @_enumerated_state[enum_attr].has_key?(prop_name)
      return @_enumerated_state[enum_attr][prop_name]
    end
  end
  klass = self
  while (subclass = klass.superclass)
    if subclass.respond_to?(:get_enumerated_state_property)
      return subclass.get_enumerated_state_property(enum_attr, prop_name)
    else
      klass = subclass
    end
  end
  return nil
end

#get_module_for_enumerated_state(attribute, value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/enumerated_state.rb', line 54

def get_module_for_enumerated_state(attribute, value)
  value = value.to_sym unless value.nil?
  return self.get_enumerated_state_property(attribute, value) if self.enumerated_state_property_exists?(attribute, value)

  module_prefix = self.get_enumerated_state_property(attribute, :module_prefix) || ''
  module_name = module_prefix + value.to_s.split(/_/).map(&:capitalize).join
  strict = self.get_enumerated_state_property(attribute, :strict)
  _module = begin
    self.class_eval(module_name)
  rescue
    begin
      Kernel.class_eval(module_name)
    rescue Exception => ex
      raise ex if strict
    end
  end
  self.set_enumerated_state_property(attribute, value, _module)
  _module
end

#set_enumerated_state_property(enum_attr, prop_name, value) ⇒ Object



92
93
94
95
96
# File 'lib/enumerated_state.rb', line 92

def set_enumerated_state_property(enum_attr, prop_name, value)
  @_enumerated_state ||= {}
  @_enumerated_state[enum_attr] ||= {}
  @_enumerated_state[enum_attr][prop_name] = value
end