diff options
| author | Jack Nagel | 2013-01-23 00:26:23 -0600 |
|---|---|---|
| committer | Jack Nagel | 2013-01-26 11:37:01 -0600 |
| commit | 8f5ea8eea6dfb3da758417e2c9bdda7d2a169408 (patch) | |
| tree | 8c2791f46ae0e3d811dfc768af3b03b56332f871 /Library/Homebrew/options.rb | |
| parent | 91f15daf73c4c30c3ccd68eeee5d512935a0685e (diff) | |
| download | homebrew-8f5ea8eea6dfb3da758417e2c9bdda7d2a169408.tar.bz2 | |
Refactor option handling internals
Currently we handle options in several ways, and it is hard to remember
what code needs an option string ("--foo"), what needs only the name
("foo") and what needs an Option object.
Now that Option objects can act as strings and be converted to JSON, we
can start using them instead of passing around strings between Formula
objects, Tab objects, and ARGV-style arrays.
The Options class is a special collection that can be queried for the
inclusion of options in any form: '--foo', 'foo', or Option.new("foo").
Diffstat (limited to 'Library/Homebrew/options.rb')
| -rw-r--r-- | Library/Homebrew/options.rb | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/Library/Homebrew/options.rb b/Library/Homebrew/options.rb new file mode 100644 index 000000000..10abc1fae --- /dev/null +++ b/Library/Homebrew/options.rb @@ -0,0 +1,78 @@ +class Option + include Comparable + + attr_reader :name, :description, :flag + + def initialize(name, description=nil) + @name = name.to_s[/^(?:--)?(.+)$/, 1] + @description = description.to_s + @flag = "--#{@name}" + end + + def to_s + flag + end + alias_method :to_str, :to_s + + def to_json + flag.inspect + end + + def <=>(other) + name <=> other.name + end + + def eql?(other) + other.is_a?(self.class) && hash == other.hash + end + + def hash + name.hash + end +end + +class Options + include Enumerable + + def initialize(*args) + @options = Set.new(*args) + end + + def each(*args, &block) + @options.each(*args, &block) + end + + def <<(o) + @options << o + self + end + + def +(o) + Options.new(@options + o) + end + + def -(o) + Options.new(@options - o) + end + + def *(arg) + @options.to_a * arg + end + + def empty? + @options.empty? + end + + def as_flags + map(&:flag) + end + + def include?(o) + any? { |opt| opt == o || opt.name == o || opt.flag == o } + end + + def to_a + @options.to_a + end + alias_method :to_ary, :to_a +end |
