Skip to content

Commit

Permalink
Refactor some of the messy code in the conjugation method to tidy thi…
Browse files Browse the repository at this point in the history
…ngs up. Move exceptions from the "has_x?" methods to the method calling them - one shouldn't hit an exception just because they check if a verb is allowed...
  • Loading branch information
Ben Smith committed Apr 1, 2013
1 parent 91d1e9a commit 9fb6af7
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions lib/base/language.rb
Expand Up @@ -65,43 +65,41 @@ def subjects
end

def conjugate(verb, tense, subject)
if has_tense? tense and @tenses[tense].requires_subject?
subject ||= @subjects[0]
raise TenseException, "Unknown tense #{tense}" unless has_tense? tense
raise VerbException, "Unknown verb #{verb}" unless has_verb? verb
unless has_subject? subject or subject.nil?
raise SubjectException, "Unknown subject #{subject}"
end
if subject and not @tenses[tense].takes_subject?
raise SubjectException, "Tense #{tense} does not expect a subject."
end
if has_verb? verb and has_subject? subject
conjugations = @verbs[verb].map do |v|
v.conjugate(@tenses[tense], subject)
end
if any_the_same(conjugations.map(&:to_s))
conjugations.map(&:translate)
else
conjugations
end

conjugations = @verbs[verb].map do |v|
v.conjugate(@tenses[tense], subject)
end
if needs_translation(conjugations)
conjugations.map(&:translate)
else
conjugations
end
end

private

def has_tense? tense
@tenses.key? tense or
raise TenseException, "Unknown tense #{tense}"
@tenses.key? tense
end

def has_verb? verb
@verbs.key? verb or
raise VerbException, "Unknown verb #{verb}"
@verbs.key? verb
end

def has_subject? subject
subject.nil? or @subjects.include? subject or
raise SubjectException, "Unknown subject #{subject}"
@subjects.include? subject
end

def any_the_same array
array.any? { |r| array.count(r) > 1 }
def needs_translation conjugations
string_conjugations = conjugations.map(&:to_s)
conjugations.size > 1 or string_conjugations.any? do |r|
string_conjugations.count(r) > 1
end
end
end

Expand Down

0 comments on commit 9fb6af7

Please sign in to comment.