Class: SproutCore::FileRuleList

Inherits:
Object
  • Object
show all
Defined in:
lib/buildtasks/helpers/file_rule_list.rb

Overview

The FileRuleList applies a list of ordered rules to a file path to determine whether to reject it or approve it.

The FileRuleList actually manages multiple sets of rules, separated by target name.

The FileRuleList can read in JSON files (in either Allow or Deny mode) and SproutCore Approve Lists.

To read JSON: 
list.read_json(path, :allow)

To read Approve Lists
list.read(path)

Constant Summary collapse

ALWAYS_ACCEPTED_FILE_TYPES =
[
  '.manifest',
  '.htm',
  '.html',
  '.rhtml',
  '.png',
  '.jpg',
  '.jpeg',
  '.gif'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileRuleList

Returns a new instance of FileRuleList.



35
36
37
38
39
# File 'lib/buildtasks/helpers/file_rule_list.rb', line 35

def initialize
  @allow_by_default = false
  @ignore_list = true
  @file_rule_lists = {}
end

Instance Attribute Details

#allow_by_defaultObject

Returns the value of attribute allow_by_default.



22
23
24
# File 'lib/buildtasks/helpers/file_rule_list.rb', line 22

def allow_by_default
  @allow_by_default
end

#ignore_listObject

Returns the value of attribute ignore_list.



22
23
24
# File 'lib/buildtasks/helpers/file_rule_list.rb', line 22

def ignore_list
  @ignore_list
end

Instance Method Details

#add_rule(target, rule) ⇒ Object



41
42
43
44
45
46
# File 'lib/buildtasks/helpers/file_rule_list.rb', line 41

def add_rule(target, rule)
  @ignore_list = false
  
  @file_rule_lists[target] ||= []
  @file_rule_lists[target] << rule
end

#include?(target, file) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/buildtasks/helpers/file_rule_list.rb', line 48

def include?(target, file)
  return true if @ignore_list
  return true if ALWAYS_ACCEPTED_FILE_TYPES.include?(File.extname file)
  
  list = @file_rule_lists[target.to_s]
  return @allow_by_default if list.nil?
  
  approved = @allow_by_default
  list.each {|rule|
    _approved = rule.include? file
    approved = _approved if not _approved.nil?
  }
  
  approved
end

#read(path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/buildtasks/helpers/file_rule_list.rb', line 87

def read(path)
  @ignore_list = false
  
  mode = :allow
  target = nil
  line_number = 0
  File.new(path).each_line {|line|
    line_number += 1
    
    line.strip!
    next if line == ""
    next if line =~ /^#/
    
    target_match = /^TARGET\s+(:?<target>[^\s]+)\s*$/.match line
    if target_match
      target = target_match[:target]
      mode = :allow
      next
    end
    
    if target.nil?
      raise "Expected TARGET (target name) in Accept list at #{path}, line #{line_number}"
    end
    
    mode_match = /^(:?<mode>ALLOW|DENY)(\s+(:?<what>.*))?\s*$/i.match line
    if mode_match
      _mode = mode_match[:mode].downcase
      _mode = (_mode == "allow" ? :allow : :deny)
      
      if mode_match[:what]
        exp = mode_match[:what]
        exp = ".*" if exp == "all"
        rule = SproutCore::FileRule.new(exp, _mode)
        
        add_rule target, rule
      else
        mode = _mode
      end
      
      next
    end
    
    match = /(:?<expression>.*)$/.match(line)
    raise "Invalid rule: #{line}" if match.nil?

    rule = SproutCore::FileRule.new(match[:expression], mode)
    add_rule target, rule
  }
end

#read_json(path, mode) ⇒ Object

Read methods



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/buildtasks/helpers/file_rule_list.rb', line 67

def read_json(path, mode)
  @ignore_list = false
  
  if mode != :allow and mode != :deny
    raise "read_json must be given either mode :allow or mode :deny"
  end
  
  content = JSON.parse(File.read(path))
  
  content.each do |target, list|
    list = [list] if list.kind_of?(String)
    
    list.each do |expression|
      rule = SproutCore::FileRule.new(expression, mode)
      add_rule(target, rule)
    end
    
  end
end