Class: VirtualClass::Cache

Inherits:
Object
  • Object
show all
Defined in:
app/models/virtual_class.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



86
87
88
# File 'app/models/virtual_class.rb', line 86

def initialize
  clear_cache!
end

Instance Method Details

#all_classes(base_kpath = 'N', without_list = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/virtual_class.rb', line 108

def all_classes(base_kpath = 'N', without_list = nil)
  load_all_classes!

  filter_on = %r{\A#{base_kpath}}

  if without_list
    regexp = []
    without_list.split(',').map(&:strip).each do |without|
      if filter_class = VirtualClass[without]
        regexp << "\\A#{filter_class.kpath}"
      end
    end
    unless regexp.empty?
      filter_off = %r{\A#{regexp.join('|')}}
    end
  end

  @cache_by_kpath.values.select do |vclass|
    (vclass.kpath =~ filter_on) &&
    (filter_off.nil? || !(vclass.kpath =~ filter_off))
  end.sort {|a, b| a.kpath <=> b.kpath}
end

#build_vclass_from_real_class(real_class) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/models/virtual_class.rb', line 189

def build_vclass_from_real_class(real_class)
  # Merge virtual class from db if it exists.
  if vclass = VirtualClass.first(:conditions => {:name => real_class.name, :site_id => current_site.id})
    # ...
  else
    vclass = VirtualClass.new(:name => real_class.name)
    if real_class <= TextDocument
      vclass.monolingual = true
    end
  end
  vclass.kpath      = real_class.kpath
  vclass.real_class = real_class
  vclass.include_role real_class.schema
  vclass.instance_variable_set(:@is_real_class, true)
  vclass.site_id = current_site.id
  
  vclass
end

#clear_cache!Object



131
132
133
134
135
136
137
# File 'app/models/virtual_class.rb', line 131

def clear_cache!
  @updated_at = current_site[:roles_updated_at].to_f
  @cache_by_id    = {}
  @cache_by_kpath = {}
  @cache_by_name  = {}
  @all_classes_loaded = false
end

#find_by_id(id) ⇒ Object



90
91
92
93
94
# File 'app/models/virtual_class.rb', line 90

def find_by_id(id)
  clear_cache! if stale?

  @cache_by_id[id] || load_vclass(:id => id)
end

#find_by_kpath(kpath) ⇒ Object



96
97
98
99
100
# File 'app/models/virtual_class.rb', line 96

def find_by_kpath(kpath)
  clear_cache! if stale?

  @cache_by_kpath[kpath] || load_vclass(:kpath => kpath)
end

#find_by_name(name) ⇒ Object



102
103
104
105
106
# File 'app/models/virtual_class.rb', line 102

def find_by_name(name)
  clear_cache! if stale?

  @cache_by_name[name] || load_vclass(:name => name)
end

#load_all_classes!Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/virtual_class.rb', line 139

def load_all_classes!
  return if @all_classes_loaded

  conditions = [["site_id = ?"], current_site.id]
  unless @cache_by_id.empty?
    conditions[0] << "id NOT IN (?)"
    conditions << @cache_by_id.keys
  end

  conditions[0] = conditions[0].join(' AND ')
  
  seen_names = {}
  Node.native_classes.each do |kpath, real_class|
    seen_names[real_class.name] = true
    load_roles_and_cache(build_vclass_from_real_class(real_class))
  end

  VirtualClass.all(
    :conditions => conditions,
    :order      => 'kpath ASC').each do |vclass|
    next if seen_names[vclass.name]
    load_roles_and_cache(vclass)
  end

  @all_classes_loaded = true
end

#load_roles_and_cache(vclass) ⇒ Object



208
209
210
211
212
213
# File 'app/models/virtual_class.rb', line 208

def load_roles_and_cache(vclass)
  vclass.load_attached_roles!
  @cache_by_id[vclass.id] = vclass if vclass.id
  @cache_by_kpath[vclass.kpath] = vclass
  @cache_by_name[vclass.name]  = vclass
end

#load_vclass(conditions) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/models/virtual_class.rb', line 170

def load_vclass(conditions)
  if kpath = conditions[:kpath]
    real_class = Node.native_classes[kpath]
  elsif name = conditions[:name]
    raise if name.kind_of?(Fixnum)
    real_class = Node.native_classes_by_name[name]
  end

  if real_class
    vclass = build_vclass_from_real_class(real_class)
  else
    vclass = VirtualClass.first(:conditions => conditions.merge(:site_id => current_site.id))
  end

  load_roles_and_cache(vclass) if vclass

  vclass
end

#stale?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'app/models/virtual_class.rb', line 166

def stale?
  @updated_at < current_site[:roles_updated_at].to_f
end