Class: Questions::Answer

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

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`.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/questions/answer.rb', line 24

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


94
95
96
97
98
99
100
# File 'lib/questions/answer.rb', line 94

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


42
43
44
# File 'lib/questions/answer.rb', line 42

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?}


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

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?}


63
64
65
# File 'lib/questions/answer.rb', line 63

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


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

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)


103
104
105
# File 'lib/questions/answer.rb', line 103

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 #=> ""


122
123
124
125
126
127
# File 'lib/questions/answer.rb', line 122

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