Module: FlagpoleSitta::BracketRetrieval::ClassMethods

Defined in:
lib/flagpole_sitta/bracket_retrieval.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/flagpole_sitta/bracket_retrieval.rb', line 71

def [] key 
  clazz = self
  #If its in cache return that, unless blank, then return nil.
  if value = FlagpoleSitta::CommonFs.flagpole_cache_read(get_br_key(key)) || FlagpoleSitta::CommonFs.flagpole_cache_exist?(get_br_key(key))
    if value.present?
      value = self.safe_content? ? value.html_safe : value
    else
      #Always return nil even if the return value is blank.
      #Also if its blank we don't want to try to create it again
      #thus the reason for this odd nested if statement.
      value = nil
    end
  #Else if the object is in the database put it into the cache then return it.
  elsif obj = self.send("find_by_#{self.key_field}", key)
    value = obj.send(self.value_field)
    FlagpoleSitta::CommonFs.flagpole_cache_write(get_br_key(key), value)
    value = value && self.safe_content? ? value.html_safe : value
  #Else create the corresponding object as blank, and return nil.
  #The last line there is why this extension should never be used with user generated content.
  else
    rec = self.create(self.key_field.to_sym => key, self.value_field.to_sym => self.default_value)
    FlagpoleSitta::CommonFs.flagpole_cache_write(get_br_key(key), rec.send(self.value_field))
    value = nil
  end
  value
end

#default_valueObject

Will look up the object chain till it finds what it was set to, or not set too. Default value cannot be nil.



67
68
69
# File 'lib/flagpole_sitta/bracket_retrieval.rb', line 67

def default_value
  result = @_default_value || (self.superclass.respond_to?(:default_value) ? self.superclass.default_value : nil) || ""
end

#get_br_key(key) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/flagpole_sitta/bracket_retrieval.rb', line 42

def get_br_key key

  clazz = self

  "#{clazz}/BracketRetrieval/#{key}"

end

#key_fieldObject

Will look up the object chain till it finds what it was set to, or not set too.



56
57
58
# File 'lib/flagpole_sitta/bracket_retrieval.rb', line 56

def key_field
  result = @_key_field || (self.superclass.respond_to?(:key_field) ? self.superclass.key_field : nil) || "name"
end

#safe_content?Boolean

Will look up the object chain till it finds what it was set to, or not set too.

Returns:

  • (Boolean)


51
52
53
# File 'lib/flagpole_sitta/bracket_retrieval.rb', line 51

def safe_content?
  result = @_safe_content || (self.superclass.respond_to?(:safe_content?) ? self.superclass.safe_content? : nil) || false
end

#value_fieldObject

Will look up the object chain till it finds what it was set to, or not set too.



61
62
63
# File 'lib/flagpole_sitta/bracket_retrieval.rb', line 61

def value_field
  result = @_value_field || (self.superclass.respond_to?(:value_field) ? self.superclass.value_field : nil) || "content"
end