Class: Amalgam::TemplateFinder::Rule
- Inherits:
-
Object
- Object
- Amalgam::TemplateFinder::Rule
- Defined in:
- lib/amalgam/template_finder.rb
Constant Summary collapse
- @@rules =
{}
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(list) ⇒ Rule
constructor
A new instance of Rule.
- #list ⇒ Object
- #look_up_single(pages) ⇒ Object
Constructor Details
#initialize(list) ⇒ Rule
Returns a new instance of Rule.
24 25 26 27 |
# File 'lib/amalgam/template_finder.rb', line 24 def initialize(list) @list = list @result = 0 end |
Class Method Details
.load(root, model) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/amalgam/template_finder.rb', line 10 def self.load(root,model) @@rules[model] = [] path = "#{root}/app/views/#{model.tableize}/" Find.find(path).each do |p| if File.file?(p) && Amalgam.accepted_formats.include?(File.extname(p)) p.slice!(path) list = p.split('/') list[list.length-1] = list.last.split('.').first @@rules[model] << Rule.new(list) end end @@rules[model].sort{|x,y| y.list.length<=>x.list.length} end |
.look_up(pages, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/amalgam/template_finder.rb', line 37 def self.look_up(pages, = {}) list = [] @@rules[[:path] || pages.first.class.model_name.tableize].clone.each{|rule| list << rule.look_up_single(pages.clone)} result1 = list.max{|x,y| x[1] <=> y[1]} result2 = list.select{|x| x[0].first != 'default' && x[0].first != 'show'}.max{|x,y| x[1] <=> y[1]} return result1[0] unless result2 result = result1[1]>result2[1] ? result1[0] : result2[0] end |
.rules ⇒ Object
29 30 31 |
# File 'lib/amalgam/template_finder.rb', line 29 def self.rules @@rules end |
Instance Method Details
#list ⇒ Object
33 34 35 |
# File 'lib/amalgam/template_finder.rb', line 33 def list @list end |
#look_up_single(pages) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/amalgam/template_finder.rb', line 46 def look_up_single(pages) result = 0 is_self = true list = @list.clone.reverse<<nil pages<<nil level = nil rule = list.shift while list.present? && pages.present? page = pages.shift break unless page check_result = check(rule,page,is_self) is_self = false if is_self #puts check_result.to_s + ':'+rule+":"+page.template_keys.to_s if check_result>0 || rule.match(/^&?(\d+)/) result += check_result*(level&&rule!='default' ? 10*(1.0/level) : 1) unless rule=='default'&&pages.length>1&&list.length<=1 level = nil rule = list.shift unless rule.match(/^&?(\d+)/) if rule && rule.match(/^&?(\d+)/) position = rule.match(/^&?(\d+)/)[1].to_i-1 if pages.length > position+1 rule = list.shift position.times{pages.shift} level = position + 1 else break end end else if level return [@list,0] end end end if list.present? return [@list,0] else return [@list,result] end end |