Class: Smailer::Models::Property

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/smailer/models/property.rb

Constant Summary collapse

@@cache_created_at =
Time.now

Class Method Summary collapse

Class Method Details

.clear_cache_if_needed!Object



19
20
21
22
23
24
# File 'lib/smailer/models/property.rb', line 19

def self.clear_cache_if_needed!
  if Time.now - @@cache_created_at > 1.minute
    remove_class_variable :@@properties rescue nil
    @@cache_created_at = Time.now
  end
end

.get(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/smailer/models/property.rb', line 31

def self.get(name)
  self.setup_cache!
  name = name.to_s

  unless @@properties.has_key?(name)
    property = find_by_name name
    @@properties[name] = property ? property.value : nil
  end

  @@properties[name]
end

.get_all(name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/smailer/models/property.rb', line 43

def self.get_all(name)
  self.setup_cache!
  name, cache_key = name.to_s, "#{name}[]"

  unless @@properties.has_key?(cache_key)
    properties = all(:conditions => ['name LIKE ?', "#{name}%"]).map { |p| [p.name, p.value] }
    @@properties[cache_key] = Hash[ properties ]
  end

  @@properties[cache_key]
end

.get_boolean(name) ⇒ Object



55
56
57
# File 'lib/smailer/models/property.rb', line 55

def self.get_boolean(name)
  self.get(name).to_s.strip =~ /^(true|t|yes|y|on|1)$/i ? true : false
end

.setup_cache!Object



26
27
28
29
# File 'lib/smailer/models/property.rb', line 26

def self.setup_cache!
  self.clear_cache_if_needed!
  @@properties ||= {} unless defined?(@@properties)
end