Skip to content

Commit

Permalink
Make Language objects comparable (only) depending on their 639-3 code
Browse files Browse the repository at this point in the history
  • Loading branch information
bbenno committed Jun 4, 2022
1 parent 89ed407 commit ce0ea41
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/languages/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Languages
# Language defined in ISO 639-3
class Language
include Comparable

attr_reader :iso639_1, :iso639_2b, :iso639_2t, :iso639_3, :scope, :type, :name # , :comment

def initialize(csv_attributes) # rubocop:disable Metrics/AbcSize
Expand Down Expand Up @@ -43,6 +45,16 @@ def to_s
end
end

def ==(other)
other.class == self.class && other.iso639_3 == iso639_3
end

alias eql? ==

def hash
iso639_3.hash
end

def <=>(other)
other.iso639_3 <=> iso639_3
end
Expand Down
3 changes: 3 additions & 0 deletions sig/languages.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ module Languages
def living?: () -> bool
def special?: () -> bool

def ==: (untyped) -> bool
alias eql? ==
def hash: () -> Integer
def <=>: (Language) -> Integer
end
end
6 changes: 6 additions & 0 deletions test/test_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def test_that_it_has_a_name
assert_instance_of String, @language.name
end

def test_equality_depends_on_iso639_3
other_language = ::Languages::Language.new(id: 'tlh', part2b: nil, part2t: nil, part1: nil, scope: nil,
language_type: nil, ref_name: nil)
assert @language == other_language
end

::Languages::TYPES.each do |type|
define_method "test_that_respond_to_#{type}?" do
assert_respond_to @language, "#{type}?"
Expand Down

0 comments on commit ce0ea41

Please sign in to comment.