lib/selectable_attr/enum.rb in selectable_attr-0.3.16 vs lib/selectable_attr/enum.rb in selectable_attr-0.3.17

- old
+ new

@@ -28,10 +28,32 @@ def each(&block) entries.each(&block) end + def ==(other) + return false unless length == other.length + other_entries = other.entries + # ruby-1.8系ではEnumeratir#with_indexが使えないので1.8でも使用可能な書き方に変更しました。 + # entries.map.with_index{|e, i| e == other_entries[i]}.all? # for 1.9 + entries.each_with_index do |e, i| + return false unless e == other_entries[i] + end + true + end + + def ===(other) + return false unless length == other.length + other_entries = other.entries + # ruby-1.8系ではEnumeratir#with_indexが使えないので1.8でも使用可能な書き方に変更しました。 + # entries.map.with_index{|e, i| e === other_entries[i]}.all? + entries.each_with_index do |e, i| + return false unless e === other_entries[i] + end + true + end + def define(id, key, name, options = nil, &block) entry = Entry.new(self, id, key, name, options, &block) entry.instance_variable_set(:@defined_in_code, true) @entries << entry end @@ -121,11 +143,11 @@ end alias_method :size, :length class Entry BASE_ATTRS = [:id, :key, :name] - attr_reader :id, :key + attr_reader :id, :key, :options attr_reader :defined_in_code def initialize(enum, id, key, name, options = nil, &block) @enum = enum @id = id @key = key @@ -145,9 +167,27 @@ @options ? @options[option_key] : nil end def match?(options) @options === options + end + + def ==(other) + case other + when SelectableAttr::Enum::Entry + self.id == other.id + else + false + end + end + + def ===(other) + case other + when SelectableAttr::Enum::Entry + (self.id == other.id) && (self.key == other.key) + else + false + end end def null? false end