smartdown 0.12.1 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  require 'smartdown/api/question'
2
- require 'smartdown/model/element/question/country'
2
+ require 'smartdown/model/element/question'
3
3
 
4
4
  module Smartdown
5
5
  module Api
@@ -0,0 +1,9 @@
1
+ require 'smartdown/api/question'
2
+
3
+ module Smartdown
4
+ module Api
5
+ class MoneyQuestion < Question
6
+ end
7
+ end
8
+ end
9
+
@@ -8,20 +8,8 @@ module Smartdown
8
8
  attr_reader :answer, :question
9
9
 
10
10
  def initialize(elements, response)
11
- if element = elements.find{|element| element.is_a? Smartdown::Model::Element::Question::MultipleChoice}
12
- @question = MultipleChoice.new(elements)
13
- elsif element = elements.find{|element| element.is_a? Smartdown::Model::Element::Question::Country}
14
- @question = CountryQuestion.new(elements)
15
- elsif element = elements.find{|element| element.is_a? Smartdown::Model::Element::Question::Date}
16
- @question = DateQuestion.new(elements)
17
- elsif element = elements.find{|element| element.is_a? Smartdown::Model::Element::Question::Salary}
18
- @question = SalaryQuestion.new(elements)
19
- elsif element = elements.find{|element| element.is_a? Smartdown::Model::Element::Question::Text}
20
- @question = TextQuestion.new(elements)
21
- elsif element = elements.find{|element| element.is_a? Smartdown::Model::Element::Question::Postcode}
22
- @question = PostcodeQuestion.new(elements)
23
- end
24
- @answer = element.answer_type.new(response, element) if element
11
+ @question, @answer = Smartdown::Model::Element::Question.
12
+ create_question_answer(elements, response)
25
13
  end
26
14
 
27
15
  end
@@ -1,9 +1,4 @@
1
- require 'smartdown/api/multiple_choice'
2
- require 'smartdown/api/date_question'
3
- require 'smartdown/api/country_question'
4
- require 'smartdown/api/salary_question'
5
- require 'smartdown/api/text_question'
6
- require 'smartdown/api/postcode_question'
1
+ require 'smartdown/model/element/question'
7
2
 
8
3
  module Smartdown
9
4
  module Api
@@ -12,19 +7,9 @@ module Smartdown
12
7
  elements.slice_before do |element|
13
8
  element.is_a? Smartdown::Model::Element::MarkdownHeading
14
9
  end.map do |question_element_group|
15
- if question_element_group.find{|element| element.is_a? Smartdown::Model::Element::Question::MultipleChoice }
16
- Smartdown::Api::MultipleChoice.new(question_element_group)
17
- elsif question_element_group.find{ |element| element.is_a?(Smartdown::Model::Element::Question::Country) }
18
- Smartdown::Api::CountryQuestion.new(question_element_group)
19
- elsif question_element_group.find{|element| element.is_a? Smartdown::Model::Element::Question::Date}
20
- Smartdown::Api::DateQuestion.new(question_element_group)
21
- elsif question_element_group.find{|element| element.is_a? Smartdown::Model::Element::Question::Salary}
22
- Smartdown::Api::SalaryQuestion.new(question_element_group)
23
- elsif question_element_group.find{|element| element.is_a? Smartdown::Model::Element::Question::Text}
24
- Smartdown::Api::TextQuestion.new(question_element_group)
25
- elsif question_element_group.find{|element| element.is_a? Smartdown::Model::Element::Question::Postcode}
26
- Smartdown::Api::PostcodeQuestion.new(question_element_group)
27
- end
10
+ question, answer = Smartdown::Model::Element::Question.
11
+ create_question_answer(question_element_group)
12
+ question
28
13
  end.compact
29
14
  end
30
15
  end
@@ -1,3 +1,5 @@
1
+ require 'forwardable'
2
+
1
3
  module Smartdown
2
4
  module Model
3
5
  module Answer
@@ -1,23 +1,54 @@
1
1
  # encoding: utf-8
2
- #TODO: this is no technically an answer (only used for plugin formatting for now)
3
- #since we will have money questions in the near future, this todo should be removed, and the
4
- #require in interpolator.rb removed
5
-
6
2
  require_relative "base"
7
3
 
8
4
  module Smartdown
9
5
  module Model
10
6
  module Answer
11
7
  class Money < Base
8
+
9
+ FORMAT_REGEX = /^£?\W*([\d|,|]+[\.]?[\d]*)$/
10
+
12
11
  def value_type
13
12
  ::Float
14
13
  end
15
14
 
15
+ def to_s
16
+ ('%.2f' % value).chomp('.00')
17
+ end
18
+
16
19
  def humanize
17
- number_string = "£#{'%.2f' % value}".gsub(/(\d)(?=(\d\d\d)+(?!\d))/) do |digit_to_delimit|
20
+ whole, decimal = separate_by_comma(value)
21
+ if decimal == '00'
22
+ "£#{whole}"
23
+ else
24
+ "£#{whole}.#{decimal}"
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def parse_value value
31
+ if value.is_a?(Float)
32
+ value
33
+ elsif value.is_a?(Fixnum)
34
+ Float value
35
+ else
36
+ matched_value = value.strip.match FORMAT_REGEX
37
+ if matched_value
38
+ Float matched_value[1].gsub(',','')
39
+ else
40
+ @error = 'Invalid format'
41
+ return
42
+ end
43
+ end
44
+ end
45
+
46
+ def separate_by_comma(number)
47
+ left, right = ('%.2f' % number).split('.')
48
+ left.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/) do |digit_to_delimit|
18
49
  "#{digit_to_delimit},"
19
50
  end
20
- number_string.end_with?(".00") ? number_string[0..-4] : number_string
51
+ [left, right]
21
52
  end
22
53
  end
23
54
  end
@@ -7,23 +7,19 @@ module Smartdown
7
7
  class Salary < Base
8
8
  attr_reader :period, :amount_per_period
9
9
 
10
- FORMAT_REGEX = /^£?\W*([\d|,|]+[\.]?[\d]*)[-|\W*per\W*](week|month|year)$/
10
+ FORMAT_REGEX = Regexp.new(Money::FORMAT_REGEX.source.chomp('$') +
11
+ /[-|\W*per\W*](week|month|year)$/.source)
11
12
 
12
13
  def value_type
13
14
  ::Float
14
15
  end
15
16
 
16
17
  def to_s
17
- "#{'%.2f' % amount_per_period}-#{period}"
18
+ "#{@money_per_period}-#{period}"
18
19
  end
19
20
 
20
21
  def humanize
21
- whole, decimal = separate_by_comma(amount_per_period)
22
- if decimal == "00"
23
- "£#{whole} per #{period}"
24
- else
25
- "£#{whole}.#{decimal} per #{period}"
26
- end
22
+ "#{@money_per_period.humanize} per #{period}"
27
23
  end
28
24
 
29
25
  private
@@ -33,8 +29,10 @@ module Smartdown
33
29
  @error = "Invalid format"
34
30
  return
35
31
  end
36
- @amount_per_period, @period = *matched_value[1..2]
37
- @amount_per_period = @amount_per_period.gsub(",","").to_f
32
+ amount_per_period, @period = *matched_value[1..2]
33
+
34
+ @money_per_period = Money.new(amount_per_period)
35
+ @amount_per_period = @money_per_period.value
38
36
  yearly_total
39
37
  end
40
38
 
@@ -49,13 +47,6 @@ module Smartdown
49
47
  end
50
48
  end
51
49
 
52
- def separate_by_comma(number)
53
- left, right = ('%.2f' % number).split('.')
54
- left.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/) do |digit_to_delimit|
55
- "#{digit_to_delimit},"
56
- end
57
- [left, right]
58
- end
59
50
  end
60
51
  end
61
52
  end
@@ -0,0 +1,15 @@
1
+ require 'smartdown/model/answer/money'
2
+
3
+ module Smartdown
4
+ module Model
5
+ module Element
6
+ module Question
7
+ class Money < Struct.new(:name, :alias)
8
+ def answer_type
9
+ Smartdown::Model::Answer::Money
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,68 @@
1
+ require 'forwardable'
2
+ require 'smartdown/model/element/question/multiple_choice'
3
+ require 'smartdown/model/element/question/country'
4
+ require 'smartdown/model/element/question/date'
5
+ require 'smartdown/model/element/question/money'
6
+ require 'smartdown/model/element/question/salary'
7
+ require 'smartdown/model/element/question/text'
8
+ require 'smartdown/model/element/question/postcode'
9
+ require 'smartdown/api/multiple_choice'
10
+ require 'smartdown/api/date_question'
11
+ require 'smartdown/api/country_question'
12
+ require 'smartdown/api/money_question'
13
+ require 'smartdown/api/salary_question'
14
+ require 'smartdown/api/text_question'
15
+ require 'smartdown/api/postcode_question'
16
+
17
+ module Smartdown
18
+ module Model
19
+ module Element
20
+ module Question
21
+
22
+ class << self
23
+ def create_question_answer(elements, response=nil)
24
+ matching_question_element(elements) do |element|
25
+ question = create_question(elements, element)
26
+ answer = create_answer(response, element)
27
+ return [question, answer]
28
+ end
29
+ return [nil, nil]
30
+ end
31
+
32
+ private
33
+
34
+ def matching_question_element(elements)
35
+ constants.find do |symbol|
36
+ question_type = const_get(symbol)
37
+
38
+ if element = elements.find {|e| e.is_a?(question_type) }
39
+ yield element
40
+ end
41
+ end
42
+ end
43
+
44
+ def create_question(elements, element)
45
+ question_model(element).new(elements)
46
+ end
47
+
48
+ def question_model(element)
49
+ Smartdown::Api.const_get question_model_name(element)
50
+ end
51
+
52
+ def question_model_name(element)
53
+ symbol = element.class.name.split(':').last.to_sym
54
+ name = (symbol.to_s + 'Question').to_sym
55
+ name = symbol unless Smartdown::Api.const_defined?(name)
56
+ name
57
+ end
58
+
59
+ def create_answer(response, element)
60
+ response ? element.answer_type.new(response, element) : nil
61
+ end
62
+
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+
@@ -0,0 +1,20 @@
1
+ require 'smartdown/parser/base'
2
+ require 'smartdown/parser/question'
3
+
4
+ module Smartdown
5
+ module Parser
6
+ module Element
7
+ class MoneyQuestion < Question
8
+ rule(:question_type) {
9
+ str("money")
10
+ }
11
+
12
+ rule(:money_question) {
13
+ question_tag.as(:money)
14
+ }
15
+
16
+ root(:money_question)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -4,6 +4,7 @@ require 'smartdown/parser/element/front_matter'
4
4
  require 'smartdown/parser/element/start_button'
5
5
  require 'smartdown/parser/element/multiple_choice_question'
6
6
  require 'smartdown/parser/element/date_question'
7
+ require 'smartdown/parser/element/money_question'
7
8
  require 'smartdown/parser/element/salary_question'
8
9
  require 'smartdown/parser/element/text_question'
9
10
  require 'smartdown/parser/element/country_question'
@@ -26,6 +27,7 @@ module Smartdown
26
27
  Element::TextQuestion.new |
27
28
  Element::CountryQuestion.new |
28
29
  Element::PostcodeQuestion.new |
30
+ Element::MoneyQuestion.new |
29
31
  Rules.new |
30
32
  Element::StartButton.new |
31
33
  Element::NextSteps.new |
@@ -34,7 +36,7 @@ module Smartdown
34
36
  }
35
37
 
36
38
  rule(:markdown_elements) {
37
- markdown_element.repeat(1, 1) >>
39
+ markdown_element.repeat(1, 1) >>
38
40
  (newline.repeat(1).as(:blank_line) >> markdown_element.as(:element)).repeat
39
41
  }
40
42
 
@@ -6,12 +6,7 @@ require 'smartdown/model/elements'
6
6
  require 'smartdown/model/rule'
7
7
  require 'smartdown/model/nested_rule'
8
8
  require 'smartdown/model/next_node_rules'
9
- require 'smartdown/model/element/question/multiple_choice'
10
- require 'smartdown/model/element/question/country'
11
- require 'smartdown/model/element/question/date'
12
- require 'smartdown/model/element/question/salary'
13
- require 'smartdown/model/element/question/text'
14
- require 'smartdown/model/element/question/postcode'
9
+ require 'smartdown/model/element/question'
15
10
  require 'smartdown/model/element/start_button'
16
11
  require 'smartdown/model/element/markdown_heading'
17
12
  require 'smartdown/model/element/markdown_line'
@@ -137,6 +132,13 @@ module Smartdown
137
132
  )
138
133
  }
139
134
 
135
+ rule money: { identifier: simple(:identifier), option_pairs: subtree(:option_pairs) } {
136
+ Smartdown::Model::Element::Question::Money.new(
137
+ identifier.to_s,
138
+ Smartdown::Parser::OptionPairs.transform(option_pairs).fetch('alias', nil)
139
+ )
140
+ }
141
+
140
142
  rule(:next_steps => { content: simple(:content) }) {
141
143
  Smartdown::Model::Element::NextSteps.new(content.to_s)
142
144
  }
@@ -1,3 +1,3 @@
1
1
  module Smartdown
2
- VERSION = "0.12.1"
2
+ VERSION = "0.13.0"
3
3
  end
@@ -1,7 +1,5 @@
1
1
  # encoding: utf-8
2
- #TODO: this "require" for money is here for now since there is no associated question type for money yet
3
- require 'smartdown/model/answer/money'
4
- require 'smartdown/model/element/question/country'
2
+ require 'smartdown/model/element/question'
5
3
  require 'smartdown/engine/interpolator'
6
4
  require 'smartdown/engine/state'
7
5
  require 'parslet'
@@ -120,7 +118,7 @@ describe Smartdown::Engine::Interpolator do
120
118
  let(:state) {
121
119
  Smartdown::Engine::State.new(
122
120
  current_node: node.name,
123
- money_answer: Smartdown::Model::Answer::Money.new(12.32523)
121
+ money_answer: Smartdown::Model::Answer::Money.new('12.32523')
124
122
  )
125
123
  }
126
124
  it "interpolates the result of the function call" do
@@ -3,36 +3,147 @@ require 'smartdown/model/answer/money'
3
3
 
4
4
  describe Smartdown::Model::Answer::Money do
5
5
 
6
- let(:money_float) { 523.42 }
7
- subject(:instance) { described_class.new(money_float) }
6
+ let(:money_input) { '1523.42' }
7
+ subject(:instance) { described_class.new(money_input) }
8
+
9
+ specify { expect(instance.value).to eql(1523.42) }
10
+
11
+ describe 'instantiate with integer' do
12
+ let(:money_input) { 1523 }
13
+
14
+ specify { expect(instance.value).to eql(1523.0) }
15
+ end
16
+
17
+ describe 'instantiate with float' do
18
+ let(:money_input) { 1523.12 }
19
+
20
+ specify { expect(instance.value).to eql(1523.12) }
21
+ end
22
+
23
+ describe 'to_s' do
24
+ it "returns value without comma delimiter" do
25
+ expect(instance.to_s).to eql("1523.42")
26
+ end
27
+ end
8
28
 
9
29
  describe "#humanize" do
10
30
  it "specifies money in the correct format" do
11
- expect(instance.humanize).to eql("£523.42")
31
+ expect(instance.humanize).to eql("£1,523.42")
12
32
  end
33
+
13
34
  context "rounding up" do
14
- let(:money_float) { 523.427 }
35
+ let(:money_input) { '1523.427' }
15
36
  it "rounds up amounts of money correctly" do
16
- expect(instance.humanize).to eql("£523.43")
37
+ expect(instance.humanize).to eql("£1,523.43")
17
38
  end
18
39
  end
19
40
  context "rounding down" do
20
- let(:money_float) { 523.421 }
41
+ let(:money_input) { '1523.421' }
21
42
  it "rounds down amounts of money correctly" do
22
- expect(instance.humanize).to eql("£523.42")
43
+ expect(instance.humanize).to eql("£1,523.42")
23
44
  end
24
45
  end
25
46
  context "rounds down in the .005 case" do
26
- let(:money_float) { 523.425 }
47
+ let(:money_input) { '1523.425' }
27
48
  it "rounds down amounts of money correctly" do
28
- expect(instance.humanize).to eql("£523.42")
49
+ expect(instance.humanize).to eql("£1,523.42")
29
50
  end
30
51
  end
31
52
  context "no pence" do
32
- let(:money_float) { 523.00 }
53
+ let(:money_input) { '1523.00' }
33
54
  it "rounds down amounts of money correctly" do
34
- expect(instance.humanize).to eql("£523")
55
+ expect(instance.humanize).to eql("£1,523")
35
56
  end
36
57
  end
37
58
  end
59
+
60
+ describe "errors" do
61
+ context "invalid formatting" do
62
+ let(:money_input) {"Loads'a'money"}
63
+
64
+ it "Has errors" do
65
+ expect(instance.error).to eql("Invalid format")
66
+ end
67
+ end
68
+
69
+ context "no input" do
70
+ let(:money_input) { nil }
71
+
72
+ it "Has errors" do
73
+ expect(instance.error).to eql("Please answer this question")
74
+ end
75
+ end
76
+ end
77
+
78
+ describe "comparisons" do
79
+ let(:money_input) { '62,400' }
80
+
81
+ context "comparing against Float" do
82
+ specify { expect(instance == 62400.0).to eql true }
83
+
84
+ specify { expect(instance < 62400.1).to eql true }
85
+ specify { expect(instance < 62400.0).to eql false }
86
+ specify { expect(instance < 62399.9).to eql false }
87
+
88
+ specify { expect(instance > 62400.1).to eql false }
89
+ specify { expect(instance > 62400.0).to eql false }
90
+ specify { expect(instance > 62399.9).to eql true }
91
+
92
+ specify { expect(instance <= 62400.1).to eql true }
93
+ specify { expect(instance <= 62400.0).to eql true }
94
+ specify { expect(instance <= 62399.9).to eql false }
95
+
96
+ specify { expect(instance >= 62400.1).to eql false }
97
+ specify { expect(instance >= 62400.0).to eql true }
98
+ specify { expect(instance >= 62399.9).to eql true }
99
+ end
100
+
101
+ context "comparing against Integer" do
102
+ specify { expect(instance == 62400).to eql true }
103
+
104
+ specify { expect(instance < 62401).to eql true }
105
+ specify { expect(instance < 62400).to eql false }
106
+ specify { expect(instance < 62399).to eql false }
107
+
108
+ specify { expect(instance > 62401).to eql false }
109
+ specify { expect(instance > 62400).to eql false }
110
+ specify { expect(instance > 62399).to eql true }
111
+
112
+ specify { expect(instance <= 62401).to eql true }
113
+ specify { expect(instance <= 62400).to eql true }
114
+ specify { expect(instance <= 62399).to eql false }
115
+
116
+ specify { expect(instance >= 62401).to eql false }
117
+ specify { expect(instance >= 62400).to eql true }
118
+ specify { expect(instance >= 62399).to eql true }
119
+ end
120
+
121
+ context "comparing against strings" do
122
+ specify { expect(instance == '62400').to eql true }
123
+ specify { expect(instance < '62400.1').to eql true }
124
+ specify { expect(instance > '62400').to eql false }
125
+ specify { expect(instance <= '62400.1').to eql true }
126
+ specify { expect(instance >= '62400').to eql true }
127
+ end
128
+
129
+ context "comparing against Answer::Money" do
130
+ specify { expect(instance == described_class.new(money_input)).to eql true }
131
+
132
+ specify { expect(instance < described_class.new('62,400.1')).to eql true }
133
+ specify { expect(instance < described_class.new('62,400')).to eql false }
134
+ specify { expect(instance < described_class.new('62,399.99')).to eql false }
135
+
136
+ specify { expect(instance > described_class.new('62,400.1')).to eql false }
137
+ specify { expect(instance > described_class.new('62,400')).to eql false }
138
+ specify { expect(instance > described_class.new('62,399.99')).to eql true }
139
+
140
+ specify { expect(instance <= described_class.new('62,400.1')).to eql true }
141
+ specify { expect(instance <= described_class.new('62,400')).to eql true }
142
+ specify { expect(instance <= described_class.new('62,399.99')).to eql false }
143
+
144
+ specify { expect(instance >= described_class.new('62,400.1')).to eql false }
145
+ specify { expect(instance >= described_class.new('62,400')).to eql true }
146
+ specify { expect(instance >= described_class.new('62,399.99')).to eql true }
147
+ end
148
+ end
38
149
  end
@@ -1,5 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require 'smartdown/model/answer/date'
3
+ require 'smartdown/model/answer/money'
3
4
  require 'smartdown/model/element/question/date'
4
5
 
5
6
  describe Smartdown::Model::Answer::Salary do
@@ -11,7 +12,7 @@ describe Smartdown::Model::Answer::Salary do
11
12
  specify { expect(instance.amount_per_period).to eql(500.00) }
12
13
 
13
14
  it "as a string, it should declare itself in the initial format provided" do
14
- expect(instance.to_s).to eql("500.00-week")
15
+ expect(instance.to_s).to eql("500-week")
15
16
  end
16
17
 
17
18
  describe "#humanize" do
@@ -71,7 +72,6 @@ describe Smartdown::Model::Answer::Salary do
71
72
  expect(instance.error).to eql("Please answer this question")
72
73
  end
73
74
  end
74
-
75
75
  end
76
76
 
77
77
  context "declared by week" do
@@ -89,7 +89,6 @@ describe Smartdown::Model::Answer::Salary do
89
89
  specify { expect(instance.value).to eql 20000.0 }
90
90
  end
91
91
 
92
-
93
92
  describe "comparisons" do
94
93
  let(:salary_string) { "1200-week" } # equivalent to 62,400 yearly or 5200 monthly
95
94
 
@@ -136,7 +135,7 @@ describe Smartdown::Model::Answer::Salary do
136
135
  specify { expect(instance >= "5199.9-month").to eql true }
137
136
  end
138
137
 
139
- context "comparing against Answer::Salaries" do
138
+ context "comparing against Answer::Salary" do
140
139
  specify { expect(instance == described_class.new("1200-week")).to eql true }
141
140
 
142
141
  specify { expect(instance < described_class.new("1200.1-week")).to eql true }
@@ -0,0 +1,70 @@
1
+ require 'smartdown/model/element/question'
2
+
3
+ describe Smartdown::Model::Element::Question do
4
+
5
+ let(:elements) { [
6
+ Smartdown::Model::Element::Question::Money.new('10.30'),
7
+ Smartdown::Model::Element::Question::Date.new('2014-10-01')
8
+ ] }
9
+
10
+ describe 'constants()' do
11
+ it 'has expected order' do
12
+ expect(described_class.constants).to eq [
13
+ :MultipleChoice,
14
+ :Country,
15
+ :Date,
16
+ :Money,
17
+ :Salary,
18
+ :Text,
19
+ :Postcode
20
+ ]
21
+ end
22
+ end
23
+
24
+ describe 'create_question_answer()' do
25
+
26
+ context 'with no matching elements' do
27
+ before do
28
+ @question, @answer = described_class.create_question_answer([])
29
+ end
30
+
31
+ it 'does not create question or answer' do
32
+ expect(@question).to be_nil
33
+ expect(@answer).to be_nil
34
+ end
35
+ end
36
+
37
+ context 'with response value not given' do
38
+ before do
39
+ @question, @answer = described_class.create_question_answer(elements)
40
+ end
41
+
42
+ it 'should create question with correct class' do
43
+ expect(@question).to be_a Smartdown::Api::DateQuestion
44
+ end
45
+
46
+ it 'should not create answer' do
47
+ expect(@answer).to be_nil
48
+ end
49
+ end
50
+
51
+ context 'with response value given' do
52
+ before do
53
+ @question, @answer = described_class.create_question_answer(elements, '2014-10-01')
54
+ end
55
+
56
+ it 'should create question with correct class' do
57
+ expect(@question).to be_a Smartdown::Api::DateQuestion
58
+ end
59
+
60
+ it 'should create answer with correct class' do
61
+ expect(@answer).to be_a Smartdown::Model::Answer::Date
62
+ end
63
+
64
+ it 'should create answer with correct value' do
65
+ expect(@answer.value.to_s).to eq('2014-10-01')
66
+ end
67
+ end
68
+ end
69
+ end
70
+
@@ -0,0 +1,57 @@
1
+ require 'smartdown/parser/node_parser'
2
+ require 'smartdown/parser/node_interpreter'
3
+ require 'smartdown/parser/element/money_question'
4
+
5
+ describe Smartdown::Parser::Element::MoneyQuestion do
6
+ subject(:parser) { described_class.new }
7
+
8
+ context 'with question tag' do
9
+ let(:source) { '[money: court_fee]' }
10
+
11
+ it 'parses' do
12
+ should parse(source).as(
13
+ money: {
14
+ identifier: 'court_fee',
15
+ option_pairs:[],
16
+ }
17
+ )
18
+ end
19
+
20
+ describe 'transformed' do
21
+ let(:node_name) { 'my_node' }
22
+ subject(:transformed) {
23
+ Smartdown::Parser::NodeInterpreter.new(node_name, source, parser: parser).interpret
24
+ }
25
+
26
+ it { should eq(Smartdown::Model::Element::Question::Money.new('court_fee')) }
27
+ end
28
+ end
29
+
30
+ context 'with question tag and alias' do
31
+ let(:source) { '[money: court_fee, alias: claim_fee]' }
32
+
33
+ it 'parses' do
34
+ should parse(source).as(
35
+ money: {
36
+ identifier: 'court_fee',
37
+ option_pairs: [
38
+ {
39
+ key: 'alias',
40
+ value: 'claim_fee',
41
+ }
42
+ ]
43
+ }
44
+ )
45
+ end
46
+
47
+ describe 'transformed' do
48
+ let(:node_name) { 'my_node' }
49
+ subject(:transformed) {
50
+ Smartdown::Parser::NodeInterpreter.new(node_name, source, parser: parser).interpret
51
+ }
52
+
53
+ it { should eq(Smartdown::Model::Element::Question::Money.new('court_fee', 'claim_fee')) }
54
+ end
55
+ end
56
+
57
+ end
@@ -3,10 +3,7 @@ require 'smartdown/model/node'
3
3
  require 'smartdown/model/element/markdown_heading'
4
4
  require 'smartdown/model/element/markdown_line'
5
5
  require 'smartdown/model/element/start_button'
6
- require 'smartdown/model/element/question/multiple_choice'
7
- require 'smartdown/model/element/question/date'
8
- require 'smartdown/model/element/question/salary'
9
- require 'smartdown/model/element/question/text'
6
+ require 'smartdown/model/element/question'
10
7
  require 'smartdown/model/element/conditional'
11
8
  require 'smartdown/model/next_node_rules'
12
9
  require 'smartdown/model/rule'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.1
4
+ version: 0.13.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-01-09 00:00:00.000000000 Z
12
+ date: 2015-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parslet
16
- requirement: &20811800 !ruby/object:Gem::Requirement
16
+ requirement: &16864100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *20811800
24
+ version_requirements: *16864100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: uk_postcode
27
- requirement: &20810920 !ruby/object:Gem::Requirement
27
+ requirement: &16863580 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.1
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *20810920
35
+ version_requirements: *16863580
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &20810340 !ruby/object:Gem::Requirement
38
+ requirement: &16863120 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 3.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *20810340
46
+ version_requirements: *16863120
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
- requirement: &20809660 !ruby/object:Gem::Requirement
49
+ requirement: &16862640 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *20809660
57
+ version_requirements: *16862640
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: pry
60
- requirement: &20808900 !ruby/object:Gem::Requirement
60
+ requirement: &16862140 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *20808900
68
+ version_requirements: *16862140
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: gem_publisher
71
- requirement: &20808260 !ruby/object:Gem::Requirement
71
+ requirement: &16861580 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *20808260
79
+ version_requirements: *16861580
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: timecop
82
- requirement: &20937900 !ruby/object:Gem::Requirement
82
+ requirement: &16861040 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,7 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *20937900
90
+ version_requirements: *16861040
91
91
  description:
92
92
  email: david.heath@digital.cabinet-office.gov.uk
93
93
  executables:
@@ -112,6 +112,7 @@ files:
112
112
  - lib/smartdown/parser/element/markdown_blank_line.rb
113
113
  - lib/smartdown/parser/element/salary_question.rb
114
114
  - lib/smartdown/parser/element/postcode_question.rb
115
+ - lib/smartdown/parser/element/money_question.rb
115
116
  - lib/smartdown/parser/element/conditional.rb
116
117
  - lib/smartdown/parser/element/start_button.rb
117
118
  - lib/smartdown/parser/element/front_matter.rb
@@ -130,6 +131,7 @@ files:
130
131
  - lib/smartdown/api/postcode_question.rb
131
132
  - lib/smartdown/api/flow.rb
132
133
  - lib/smartdown/api/question_page.rb
134
+ - lib/smartdown/api/money_question.rb
133
135
  - lib/smartdown/api/state.rb
134
136
  - lib/smartdown/api/node.rb
135
137
  - lib/smartdown/api/directory_input.rb
@@ -174,6 +176,7 @@ files:
174
176
  - lib/smartdown/model/answer/money.rb
175
177
  - lib/smartdown/model/answer/multiple_choice.rb
176
178
  - lib/smartdown/model/answer/text.rb
179
+ - lib/smartdown/model/element/question.rb
177
180
  - lib/smartdown/model/element/conditional.rb
178
181
  - lib/smartdown/model/element/start_button.rb
179
182
  - lib/smartdown/model/element/markdown_line.rb
@@ -183,6 +186,7 @@ files:
183
186
  - lib/smartdown/model/element/question/salary.rb
184
187
  - lib/smartdown/model/element/question/postcode.rb
185
188
  - lib/smartdown/model/element/question/country.rb
189
+ - lib/smartdown/model/element/question/money.rb
186
190
  - lib/smartdown/model/element/question/multiple_choice.rb
187
191
  - lib/smartdown/model/element/question/text.rb
188
192
  - lib/smartdown/model/next_node_rules.rb
@@ -212,6 +216,7 @@ files:
212
216
  - spec/parser/element/salary_question_spec.rb
213
217
  - spec/parser/element/markdown_line_spec.rb
214
218
  - spec/parser/element/text_question_spec.rb
219
+ - spec/parser/element/money_question_spec.rb
215
220
  - spec/parser/option_pairs_transform_spec.rb
216
221
  - spec/parser/node_parser_spec.rb
217
222
  - spec/parser/snippet_pre_parser_spec.rb
@@ -280,6 +285,7 @@ files:
280
285
  - spec/model/answer/date_spec.rb
281
286
  - spec/model/answer/postcode_spec.rb
282
287
  - spec/model/answer/country_spec.rb
288
+ - spec/model/element/question_spec.rb
283
289
  - spec/model/predicates/not_operation_spec.rb
284
290
  - spec/model/predicates/set_membership_spec.rb
285
291
  - spec/model/predicates/or_operation_spec.rb
@@ -303,7 +309,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
303
309
  version: '0'
304
310
  segments:
305
311
  - 0
306
- hash: 3149710437722645719
312
+ hash: 2163453024912914140
307
313
  required_rubygems_version: !ruby/object:Gem::Requirement
308
314
  none: false
309
315
  requirements:
@@ -312,7 +318,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
312
318
  version: '0'
313
319
  segments:
314
320
  - 0
315
- hash: 3149710437722645719
321
+ hash: 2163453024912914140
316
322
  requirements: []
317
323
  rubyforge_project:
318
324
  rubygems_version: 1.8.11
@@ -341,6 +347,7 @@ test_files:
341
347
  - spec/parser/element/salary_question_spec.rb
342
348
  - spec/parser/element/markdown_line_spec.rb
343
349
  - spec/parser/element/text_question_spec.rb
350
+ - spec/parser/element/money_question_spec.rb
344
351
  - spec/parser/option_pairs_transform_spec.rb
345
352
  - spec/parser/node_parser_spec.rb
346
353
  - spec/parser/snippet_pre_parser_spec.rb
@@ -409,6 +416,7 @@ test_files:
409
416
  - spec/model/answer/date_spec.rb
410
417
  - spec/model/answer/postcode_spec.rb
411
418
  - spec/model/answer/country_spec.rb
419
+ - spec/model/element/question_spec.rb
412
420
  - spec/model/predicates/not_operation_spec.rb
413
421
  - spec/model/predicates/set_membership_spec.rb
414
422
  - spec/model/predicates/or_operation_spec.rb