Module: EasyClassConstants::ClassMethods

Defined in:
lib/easy_class_constants.rb

Instance Method Summary collapse

Instance Method Details

#accessible_constant(name, value) ⇒ Object



22
23
24
25
26
# File 'lib/easy_class_constants.rb', line 22

def accessible_constant(name, value)
  name = name.to_s.upcase
  self.send(:class_variable_set, "@@#{name}", value)
  self.send(:cattr_accessor, name)
end

#hash_indexed_by_readable_constants(hash_variable_name, hash_contents) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/easy_class_constants.rb', line 28

def hash_indexed_by_readable_constants(hash_variable_name, hash_contents)
  hash_variable_name = hash_variable_name.to_s.upcase
  self.send(:class_variable_set, "@@#{hash_variable_name}", ActiveSupport::OrderedHash.new)
  self.send(:cattr_reader, hash_variable_name)
  hash_contents.each do |*args|
    args.flatten!
    constant_name = args[0].to_s.upcase

    if args.size > 2
      hash_index = args[1]
      hash_value = args[2]
      variable_value = hash_index
    else
      hash_index = args[0]
      hash_value = args[1]
      variable_value = hash_value
    end


    puts "WARNING: in .hash_indexed_by_readable_constants. Constant is already defined: #{constant_name}" if (self.send(:class_variable_get, "@@#{constant_name}").present? rescue false)
    self.send(:class_variable_set, "@@#{constant_name}", variable_value)
    self.send(:class_variable_get, "@@#{hash_variable_name}")[hash_index] = hash_value
    self.send(:cattr_reader, constant_name)
  end
end

#readable_constant(name, value) ⇒ Object



10
11
12
13
14
# File 'lib/easy_class_constants.rb', line 10

def readable_constant(name, value)
  name = name.to_s.upcase
  self.send(:class_variable_set, "@@#{name}", value)
  self.send(:cattr_reader, name)
end

#writable_constant(name, value) ⇒ Object



16
17
18
19
20
# File 'lib/easy_class_constants.rb', line 16

def writable_constant(name, value)
  name = name.to_s.upcase
  self.send(:class_variable_set, "@@#{name}", value)
  self.send(:cattr_writer, name)
end