Class: Lossfully::InputRules
- Inherits:
-
Object
- Object
- Lossfully::InputRules
- Includes:
- Comparable
- Defined in:
- lib/lossfully/input_rules.rb
Overview
The InputRules class wraps up conditions and provides a way to test if a file meets those conditions. It also allows the conditions to be sorted so that more restrictive conditions are tried before less restrictive. The sorting hopefully does what seems natural; it looks at first at the regexp, then the file type (as returned by soxi -t), the file extension, the bitrate threshold, and finally if a block is given. For example, the following encode rules are shown in the order that they would be tested against every file (even though the rules would be checked in this order even if the below encode statements were in a different order):
encode [:mp3, 128, /bach/] do
...
end
encode [:mp3, 128, /bach/] => ...
encode [:mp3, /bach/] => ...
encode [:mp3, 128] => ...
encode :mp3 => ...
encode :lossy => ...
encode :audio => ...
encode :everything => ...
It’s obviously only a partial order; see the code for compare_strictness if you need to know exactly what it’s doing.
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#extension ⇒ Object
readonly
Returns the value of attribute extension.
-
#max_bitrate ⇒ Object
readonly
Returns the value of attribute max_bitrate.
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#<=>(x) ⇒ Object
Order by strictness, which is the proper order to test things in.
-
#compare_strictness(x) ⇒ Object
return -1 if self is less strict, 1 if self is more strict.
-
#initialize(array = [], &block) ⇒ InputRules
constructor
A new instance of InputRules.
- #test(file_or_path) ⇒ Object
Constructor Details
#initialize(array = [], &block) ⇒ InputRules
Returns a new instance of InputRules.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lossfully/input_rules.rb', line 53 def initialize array=[], &block raise unless array.kind_of? Array @block = block array.each do |x| @type = x if x.kind_of? Symbol @max_bitrate = x if x.kind_of? Numeric if x.kind_of? String @extension = (x[0..0] == '.') || x== '' ? x : '.' + x end @regexp = x if x.kind_of? Regexp end @type ||= :everything @max_bitrate ||= 0 @extension ||= '' @regexp ||= // end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
72 73 74 |
# File 'lib/lossfully/input_rules.rb', line 72 def block @block end |
#extension ⇒ Object (readonly)
Returns the value of attribute extension.
72 73 74 |
# File 'lib/lossfully/input_rules.rb', line 72 def extension @extension end |
#max_bitrate ⇒ Object (readonly)
Returns the value of attribute max_bitrate.
72 73 74 |
# File 'lib/lossfully/input_rules.rb', line 72 def max_bitrate @max_bitrate end |
#regexp ⇒ Object (readonly)
Returns the value of attribute regexp.
72 73 74 |
# File 'lib/lossfully/input_rules.rb', line 72 def regexp @regexp end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
72 73 74 |
# File 'lib/lossfully/input_rules.rb', line 72 def type @type end |
Instance Method Details
#<=>(x) ⇒ Object
Order by strictness, which is the proper order to test things in
127 128 129 |
# File 'lib/lossfully/input_rules.rb', line 127 def <=> x -1 * compare_strictness(x) end |
#compare_strictness(x) ⇒ Object
return -1 if self is less strict, 1 if self is more strict
132 133 134 135 136 137 138 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 165 166 167 168 169 170 171 172 |
# File 'lib/lossfully/input_rules.rb', line 132 def compare_strictness x return nil unless x.class == self.class if @regexp != x.regexp return -1 if @regexp == // return 1 if x.regexp == // return nil end if @type != x.type return -1 if @type == :everything return 1 if x.type == :everything return -1 if @type == :audio return 1 if x.type == :audio # these don't have to be comparable since they're mutual exclusive return -1 if @type == :nonaudio return 1 if x.type == :nonaudio return -1 if @type == :lossless return 1 if x.type == :lossless return -1 if @type == :lossy return 1 if x.type == :lossy return -1 * (@type.to_s <=> x.type.to_s) end if @extension != x.extension return -1 if @extension == '' return 1 if x.extension == '' return -1 * (@extension <=> x.extension) end b = @max_bitrate <=> x.max_bitrate return b unless b == 0 if @block || x.block return nil if @block && x.block return -1 if ! @block return 1 if ! x.block end return 0 end |
#test(file_or_path) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 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 |
# File 'lib/lossfully/input_rules.rb', line 74 def test file_or_path file = if file_or_path.kind_of? AudioFile file_or_path else AudioFile.new(file_or_path) end # unless file.is_audio? # return false unless [:everything, :nonaudio].include?(@type) # return false unless file.path =~ @regexp # (return block.call(file.path)) if @block # return true # end if @type != :everything if [:audio, :lossy, :lossless].include? @type return false unless file.is_audio? end if @type == :lossy return false if LOSSLESS_TYPES.include? file.type elsif @type == :lossless return false unless LOSSLESS_TYPES.include? file.type elsif @type == :nonaudio return false if file.is_audio? elsif @type != :audio v = [:vorbis, :ogg] return false unless (file.type == @type) || (v.include?(file.type) && v.include?(@type)) end end if @max_bitrate > 0 return false unless file.bitrate_kbps > @max_bitrate end if @extension != '' return false unless File.extname(file.path) == @extension end if @regexp != // return false unless file.path =~ @regexp end if @block # return block.call(file.path) # TODO: decide if this should be file or file.path return block.call(file) end return true end |