Class: Questions::Answer

Inherits:
Object
  • Object
show all
Defined in:
lib/questions/answer.rb

Overview

Represents an answer.

An answer has an indicator which will be used to detect which answer is selected by user. Indicators are the first few letters of its instruction. User can identify an indicator by its surrounded square brackets. After that the rest of the instruction will be displayed.

Answers can be active or inactive. Only active ansers will be displayed, inactive ones will be hidden. Answers can be special, then the indicator is in uppercase letters set.

Constant Summary collapse

SPECIAL_ENDINGS =
[:all]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*answer_hash) ⇒ Answer

Instantiates new Answer object.

Only the first key value pair will be used.

Examples:

Hash with one item

Answer.new({:overwrite => true}) #=> [o]verwrite
Answer.new(:overwrite => true) #=> [o]verwrite
Answer.new(:overwrite) #=> [o]verwrite
Answer.new({:overwrite => false}) #=> ""
Answer.new(:overwrite => false) #=> ""
Answer.new({:overwrite => nil}) #=> ""

Items of hash with more than one item will be ignored

Answer.new({:overwrite => true, :overwrite_all => false}) #=> [o]verwrite
Answer.new({:overwrite => true, :overwrite_all => true}) #=> [o]verwrite

Parameters:

  • answer_hash (Hash)

    Key (instruction); value (activeness) is ‘true` or `false`.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/questions/answer.rb', line 32

def initialize(*answer_hash)
  hash = answer_hash.first
  hash = {hash => true} if hash.is_a? Symbol

  unless [Hash, Symbol].any? { |class_name| hash.is_a? class_name }
    raise ArgumentError, "Parameter has to be a key-value-pair or a symbol"
  end

  array        = hash.first
  @instruction = array[0]
  @active      = array[1]
end

Instance Attribute Details

#indicator(first_chars = 1) ⇒ Object

Gets indicator

Examples:

with true answer

a = Answer.new(overwrite: true)
a.indicator #=> :o
a.indicator(2) #=> :ov

with special true answer

Answer.new(overwrite_all: true).indicator(2) #=> :OV

with inactive answer

Answer.new(overwrite: false).indicator #=> nil

set indicator

a = Answer.new(overwrite: true)
a.indicator #=> :o
a.indicator = :ov
a.indicator #=> :ov


102
103
104
105
106
107
108
# File 'lib/questions/answer.rb', line 102

def indicator(first_chars=1)
  return nil if inactive?
  return @indicator if @indicator
  indicator = instruction.to_s[0...first_chars]
  indicator.upcase! if special?
  indicator.to_sym
end

#instructionObject (readonly)

Gets instruction

Examples:

Hash with one item

Answer.new(overwrite: true).instruction #=> :overwrite
Answer.new(overwrite: false).instruction #=> :overwrite


50
51
52
# File 'lib/questions/answer.rb', line 50

def instruction
  @instruction
end

Instance Method Details

#active?Boolean Also known as: true?

Is answer active?

Examples:

Hash with one item

Answer.new(overwrite: true).active? #=> true
Answer.new(overwrite: false).active? #=> false

Returns:

  • (Boolean)

See Also:

  • {true?}


59
60
61
# File 'lib/questions/answer.rb', line 59

def active?
  @active
end

#inactive?Boolean Also known as: false?

Is answer inactive?

Examples:

Hash with one item

Answer.new(overwrite: true).inactive? #=> false
Answer.new(overwrite: false).inactive? #=> true

Returns:

  • (Boolean)

See Also:

  • {false?}


71
72
73
# File 'lib/questions/answer.rb', line 71

def inactive?
  !active?
end

#indicator_hashObject

Gets indicator hash

Examples:

Hash with one item

Answer.new(overwrite: true).indicator_hash #=> {:o => :overwrite}
Answer.new(overwrite: false).indicator_hash #=> nil


120
121
122
123
# File 'lib/questions/answer.rb', line 120

def indicator_hash
  return nil if inactive?
  {indicator => instruction}
end

#special?Boolean

Returns ‘true` if answer is special. Special answers are answers which ends with one of the SPECIAL_ENDINGS, `false` otherwise.

Returns:

  • (Boolean)


111
112
113
# File 'lib/questions/answer.rb', line 111

def special?
  SPECIAL_ENDINGS.any? { |ending| instruction.to_s =~ /#{ending}$/ }
end

#to_sObject

Gets string representation

Examples:

Hash with one item

Answer.new(overwrite: true).to_s #=> "[o]verwrite"
Answer.new(overwrite: false).to_s #=> ""


130
131
132
133
134
135
136
# File 'lib/questions/answer.rb', line 130

def to_s
  return nil if inactive?
  indicator = indicator()
  instruction_without_indicator = instruction.to_s[indicator.length..-1]
  humanized = instruction_without_indicator.gsub("_", " ")
  "[#{indicator}]#{humanized}"
end