Class: DansGuardian::List
- Inherits:
-
Object
- Object
- DansGuardian::List
- Defined in:
- lib/dansguardian/list.rb
Overview
this class does not inherit from ConfigFiles::Base
Instance Attribute Summary collapse
-
#includes ⇒ Object
readonly
Returns the value of attribute includes.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#listcategory ⇒ Object
readonly
Returns the value of attribute listcategory.
Instance Method Summary collapse
- #each_line(opts = {:exclude => []}) ⇒ Object
- #file_encoding ⇒ Object
- #filename ⇒ Object
-
#initialize(h = {}) ⇒ List
constructor
DansGuardian::List.new(:file = ‘/path/to/list’).
-
#read! ⇒ Object
Reads the file and fill @items ad @includes .
Constructor Details
#initialize(h = {}) ⇒ List
DansGuardian::List.new(:file = ‘/path/to/list’)
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dansguardian/list.rb', line 8 def initialize(h={}) if h.is_a? String @init = {:file => h} @file_encoding = Encoding::BINARY else @init = h end @items = [] @includes = [] @listcategory = nil #read! if @init[:file] end |
Instance Attribute Details
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
5 6 7 |
# File 'lib/dansguardian/list.rb', line 5 def includes @includes end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
5 6 7 |
# File 'lib/dansguardian/list.rb', line 5 def items @items end |
#listcategory ⇒ Object (readonly)
Returns the value of attribute listcategory.
5 6 7 |
# File 'lib/dansguardian/list.rb', line 5 def listcategory @listcategory end |
Instance Method Details
#each_line(opts = {:exclude => []}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dansguardian/list.rb', line 45 def each_line(opts={:exclude => []}) Enumerator.new do |yielder| File.foreach(@init[:file]) do |line| line.strip! line.force_encoding file_encoding case line when /^#listcategory:\s*"(.*)"/ next if opts[:exclude].include? :listcategory when /^[\s#]*\.Include/ next if opts[:exclude].include? :includes when /^[\s#]*</ next if opts[:exclude].include? :items when /^\s*#/ next if opts[:exclude].include? :comment_lines when /^\s*$/ next if opts[:exclude].include? :blank_lines end yielder.yield line end end end |
#file_encoding ⇒ Object
22 |
# File 'lib/dansguardian/list.rb', line 22 def file_encoding; @init[:file_encoding]; end |
#filename ⇒ Object
21 |
# File 'lib/dansguardian/list.rb', line 21 def filename; @init[:file]; end |
#read! ⇒ Object
Reads the file and fill @items ad @includes . This method might be overridden for non-trivial list types (?)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dansguardian/list.rb', line 26 def read! File.foreach(@init[:file]) do |line| line.force_encoding file_encoding line.strip! # Special comment used to "categorize" DG message pages if line =~ /^#listcategory:\s*"(.*)"/ @listcategory = $1 next end next if line =~ /^\s*#/ line.sub! /\s#.*$/, '' # remove comments but allow http://url#anchor if line =~ /^\.Include<(.*)>/ @includes << $1 elsif line =~ /\S/ @items << line.strip end end end |