Skip to content

Commit e776478

Browse files
Seth FalconTim Hinderliter
authored andcommitted
Port enhancements of Version, VersionConstraint from cw
Add hash, eql? methods
1 parent a2f711a commit e776478

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

‎chef/lib/chef/version_class.rb‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Author:: Seth Falcon (<seth@opscode.com>)
2-
# Copyright:: Copyright 2010 Opscode, Inc.
2+
# Author:: Christopher Walters (<cw@opscode.com>)
3+
# Copyright:: Copyright 2010-2011 Opscode, Inc.
34
# License:: Apache License, Version 2.0
45
#
56
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -39,6 +40,17 @@ def <=>(v)
3940
0
4041
end
4142

43+
def hash
44+
# Didn't put any thought or research into this, probably can be
45+
# done better
46+
to_s.hash
47+
end
48+
49+
# For hash
50+
def eql?(other)
51+
other.is_a?(Version) && self == other
52+
end
53+
4254
private
4355

4456
def parse(str="")

‎chef/lib/chef/version_constraint.rb‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Author:: Seth Falcon (<seth@opscode.com>)
2-
# Copyright:: Copyright 2010 Opscode, Inc.
2+
# Author:: Christopher Walters (<cw@opscode.com>)
3+
# Copyright:: Copyright 2010-2011 Opscode, Inc.
34
# License:: Apache License, Version 2.0
45
#
56
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,6 +23,8 @@ class VersionConstraint
2223
OPS = %w(< > = <= >= ~>)
2324
PATTERN = /^(#{OPS.join('|')}) (.+)$/
2425

26+
attr_reader :op, :version
27+
2528
def initialize(constraint_spec=DEFAULT_CONSTRAINT)
2629
case constraint_spec
2730
when nil
@@ -45,6 +48,21 @@ def include?(v)
4548
do_op(version)
4649
end
4750

51+
def inspect
52+
"(#{@op} #{@version})"
53+
end
54+
55+
def to_s
56+
"#{@op} #{@version}"
57+
end
58+
59+
def eql?(o)
60+
o.class == self.class && @op == o.op && @version == o.version
61+
end
62+
alias_method :==, :eql?
63+
64+
private
65+
4866
def do_op(other_version)
4967
if STANDARD_OPS.include? @op
5068
other_version.send(@op.to_sym, @version)
@@ -64,17 +82,6 @@ def do_op(other_version)
6482
end
6583
end
6684

67-
def inspect
68-
"(#{@op} #{@version})"
69-
end
70-
71-
def to_s
72-
"#{@op} #{@version}"
73-
end
74-
75-
76-
private
77-
7885
def parse_from_array(constraint_spec)
7986
if constraint_spec.empty?
8087
parse(DEFAULT_CONSTRAINT)

0 commit comments

Comments
 (0)