Class: Ankit::Card
- Inherits:
-
Object
- Object
- Ankit::Card
- Defined in:
- lib/ankit/card.rb,
lib/ankit/challenge.rb
Instance Attribute Summary collapse
-
#deckname ⇒ Object
readonly
Returns the value of attribute deckname.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
Instance Method Summary collapse
- #corrected_original_over(wrong) ⇒ Object
- #decorated_original(&block) ⇒ Object
- #diff_from_original(text, &block) ⇒ Object
- #hidden_original ⇒ Object
- #hilight_against_original(wrong) ⇒ Object
-
#initialize(params) ⇒ Card
constructor
A new instance of Card.
- #match?(text) ⇒ Boolean
- #mixed_hilight_for_flash(wrong) ⇒ Object
- #name ⇒ Object
- #original ⇒ Object
- #plain_original ⇒ Object
- #translation ⇒ Object
Constructor Details
#initialize(params) ⇒ Card
Returns a new instance of Card.
24 25 26 |
# File 'lib/ankit/card.rb', line 24 def initialize(params) @params = params end |
Instance Attribute Details
#deckname ⇒ Object (readonly)
Returns the value of attribute deckname.
7 8 9 |
# File 'lib/ankit/card.rb', line 7 def deckname @deckname end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
7 8 9 |
# File 'lib/ankit/card.rb', line 7 def source @source end |
Class Method Details
.parse(text) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ankit/card.rb', line 9 def self.parse(text) lines = text.split(/\r?\n/).select { |l| !/^\#/.match(l) and !/^\s*$/.match(l) } return nil if lines.empty? params = lines.inject({}) do |a, line| case line when /^(\w+)\:(.*)/ a[$1.downcase.to_sym] = $2.strip end a end self.new(params) end |
Instance Method Details
#corrected_original_over(wrong) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/ankit/challenge.rb', line 50 def corrected_original_over(wrong) diff_from_original(wrong) do |ch| case ch.action when "=" ch.new_element when "!", "+" StylableText.styled_text(ch.new_element, :correct) when "-" "" else raise end end end |
#decorated_original(&block) ⇒ Object
54 55 56 57 |
# File 'lib/ankit/card.rb', line 54 def decorated_original(&block) decoed = original.gsub(/\[(.*?)\]/) { |t| block.call(Regexp.last_match) } decoed != original ? decoed : decoed.gsub(/^(.*)$/) { |t| block.call(Regexp.last_match) } end |
#diff_from_original(text, &block) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/ankit/card.rb', line 47 def diff_from_original(text, &block) changes = Diff::LCS.sdiff(text, plain_original) changes.map do |ch| block.call(ch) end.join("") end |
#hidden_original ⇒ Object
49 |
# File 'lib/ankit/challenge.rb', line 49 def hidden_original; decorated_original{ |m| StylableText.styled_text(m[1], :hidden) }; end |
#hilight_against_original(wrong) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ankit/challenge.rb', line 65 def hilight_against_original(wrong) diff_from_original(wrong) do |ch| case ch.action when "=" ch.old_element when "!", "-" StylableText.styled_text(ch.old_element, :wrong) when "+" "" else raise end end end |
#match?(text) ⇒ Boolean
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ankit/card.rb', line 35 def match?(text) return :match if plain_original == text return :wrong if 1 < (text.length - plain_original.length).abs hiddens = decorated_original { |m| "*"*m[1].size }.chars.to_a inside_essentials = to_enum(:diff_from_original, text).find do |ch| ch.action != "=" && hiddens[ch.old_position] == "*" end inside_essentials ? :wrong : :typo end |
#mixed_hilight_for_flash(wrong) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ankit/challenge.rb', line 80 def mixed_hilight_for_flash(wrong) diff_from_original(wrong) do |ch| case ch.action when "=" StylableText.styled_text(ch.old_element, :fyi) when "!" StylableText.styled_text(ch.old_element, :wrong) + StylableText.styled_text(ch.new_element, :correct) when "-" StylableText.styled_text(ch.old_element, :wrong) when "+" StylableText.styled_text(ch.new_element, :correct) else raise end end end |
#name ⇒ Object
31 32 33 |
# File 'lib/ankit/card.rb', line 31 def name original.gsub(/\W+/, "-").gsub(/^\-/, "").gsub(/\-$/, "").downcase end |
#original ⇒ Object
28 |
# File 'lib/ankit/card.rb', line 28 def original() @params[:o]; end |
#plain_original ⇒ Object
59 |
# File 'lib/ankit/card.rb', line 59 def plain_original; decorated_original { |m| m[1] }; end |
#translation ⇒ Object
29 |
# File 'lib/ankit/card.rb', line 29 def translation() @params[:t]; end |