smartdown 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,7 +40,8 @@ module Smartdown
40
40
  private
41
41
  def interpolate(text, state)
42
42
  text.to_s.gsub(/%{([^}]+)}/) do |_|
43
- resolve_term($1, state)
43
+ term = resolve_term($1, state)
44
+ term.respond_to?(:humanize) ? term.humanize : term
44
45
  end
45
46
  end
46
47
 
@@ -12,6 +12,10 @@ module Smartdown
12
12
  value.strftime("%Y-%-m-%-d")
13
13
  end
14
14
 
15
+ def humanize
16
+ value.strftime("%-d %B %Y")
17
+ end
18
+
15
19
  private
16
20
  def parse_value(value)
17
21
  ::Date.parse(value)
@@ -0,0 +1,22 @@
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
+ require_relative "base"
7
+
8
+ module Smartdown
9
+ module Model
10
+ module Answer
11
+ class Money < Base
12
+ def value_type
13
+ ::Float
14
+ end
15
+
16
+ def humanize
17
+ "£#{'%.2f' % value}"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -44,7 +44,7 @@ module Smartdown
44
44
  def interpret_node(input_data)
45
45
  Smartdown::Parser::NodeInterpreter.new(input_data.name, input_data.read).interpret
46
46
  rescue Parslet::ParseFailed => error
47
- raise ParseError.new(input_data.to_s, error)
47
+ raise ParseError.new(input_data.name, error)
48
48
  end
49
49
 
50
50
  def pre_parse(flow_input)
@@ -1,3 +1,3 @@
1
1
  module Smartdown
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
@@ -1,3 +1,6 @@
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'
1
4
  require 'smartdown/engine/interpolator'
2
5
  require 'smartdown/engine/state'
3
6
  require 'parslet'
@@ -92,4 +95,45 @@ describe Smartdown::Engine::Interpolator do
92
95
  expect(interpolated_node.elements.first.content).to eq("20")
93
96
  end
94
97
  end
98
+
99
+ context "a paragraph containing function call with two arguments" do
100
+ let(:elements) { [Smartdown::Model::Element::MarkdownParagraph.new('%{multiply(number other_number)}')] }
101
+ let(:state) {
102
+ Smartdown::Engine::State.new(
103
+ current_node: node.name,
104
+ number: 10,
105
+ other_number: 2,
106
+ multiply: ->(number, other_number) { number * other_number }
107
+ )
108
+ }
109
+ it "interpolates the result of the function call" do
110
+ expect(interpolated_node.elements.first.content).to eq("20")
111
+ end
112
+ end
113
+
114
+ context "a paragraph containing a date answer" do
115
+ let(:elements) { [Smartdown::Model::Element::MarkdownParagraph.new('%{date_answer}')] }
116
+ let(:state) {
117
+ Smartdown::Engine::State.new(
118
+ current_node: node.name,
119
+ date_answer: Smartdown::Model::Answer::Date.new("date_question", "2014-1-1")
120
+ )
121
+ }
122
+ it "interpolates the result of the function call" do
123
+ expect(interpolated_node.elements.first.content).to eq("1 January 2014")
124
+ end
125
+ end
126
+
127
+ context "a paragraph containing a money answer" do
128
+ let(:elements) { [Smartdown::Model::Element::MarkdownParagraph.new('%{money_answer}')] }
129
+ let(:state) {
130
+ Smartdown::Engine::State.new(
131
+ current_node: node.name,
132
+ money_answer: Smartdown::Model::Answer::Money.new("money_answer", 12.32523)
133
+ )
134
+ }
135
+ it "interpolates the result of the function call" do
136
+ expect(interpolated_node.elements.first.content).to eq("£12.33")
137
+ end
138
+ end
95
139
  end
@@ -9,6 +9,10 @@ describe Smartdown::Model::Answer::Date do
9
9
  specify { expect(instance.value).to eql Date.new(2014, 9, 4) }
10
10
  specify { expect(instance.to_s).to eql "2014-9-4" }
11
11
 
12
+ describe "humanize" do
13
+ let(:date_string) { "2000-1-10" }
14
+ specify { expect(instance.humanize).to eql "10 January 2000" }
15
+ end
12
16
 
13
17
  describe "comparisons" do
14
18
  let(:date_string) { "2000-1-10" }
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require 'smartdown/model/answer/money'
3
+
4
+ describe Smartdown::Model::Answer::Money do
5
+
6
+ let(:money_float) { 523.42 }
7
+ subject(:instance) { described_class.new(nil, money_float) }
8
+
9
+ describe "#humanize" do
10
+ it "specifies money in the correct format" do
11
+ expect(instance.humanize).to eql("£523.42")
12
+ end
13
+ context "rounding up" do
14
+ let(:money_float) { 523.427 }
15
+ it "rounds up amounts of money correctly" do
16
+ expect(instance.humanize).to eql("£523.43")
17
+ end
18
+ end
19
+ context "rounding down" do
20
+ let(:money_float) { 523.421 }
21
+ it "rounds down amounts of money correctly" do
22
+ expect(instance.humanize).to eql("£523.42")
23
+ end
24
+ end
25
+ context "rounds down in the .005 case" do
26
+ let(:money_float) { 523.425 }
27
+ it "rounds down amounts of money correctly" do
28
+ expect(instance.humanize).to eql("£523.42")
29
+ end
30
+ end
31
+ end
32
+ end
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.5.2
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2014-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parslet
16
- requirement: &12608840 !ruby/object:Gem::Requirement
16
+ requirement: &18591420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *12608840
24
+ version_requirements: *18591420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &12606660 !ruby/object:Gem::Requirement
27
+ requirement: &18589340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *12606660
35
+ version_requirements: *18589340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &12629520 !ruby/object:Gem::Requirement
38
+ requirement: &18612100 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *12629520
46
+ version_requirements: *18612100
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: gem_publisher
49
- requirement: &12628900 !ruby/object:Gem::Requirement
49
+ requirement: &18611440 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *12628900
57
+ version_requirements: *18611440
58
58
  description:
59
59
  email: david.heath@digital.cabinet-office.gov.uk
60
60
  executables:
@@ -120,6 +120,7 @@ files:
120
120
  - lib/smartdown/model/answer/date.rb
121
121
  - lib/smartdown/model/answer/base.rb
122
122
  - lib/smartdown/model/answer/salary.rb
123
+ - lib/smartdown/model/answer/money.rb
123
124
  - lib/smartdown/model/answer/multiple_choice.rb
124
125
  - lib/smartdown/model/element/conditional.rb
125
126
  - lib/smartdown/model/element/markdown_paragraph.rb
@@ -186,6 +187,7 @@ files:
186
187
  - spec/engine_spec.rb
187
188
  - spec/model/node_spec.rb
188
189
  - spec/model/flow_spec.rb
190
+ - spec/model/answer/money_spec.rb
189
191
  - spec/model/answer/base_spec.rb
190
192
  - spec/model/answer/salary_spec.rb
191
193
  - spec/model/answer/date_spec.rb
@@ -210,7 +212,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
210
212
  version: '0'
211
213
  segments:
212
214
  - 0
213
- hash: -3468443137399922310
215
+ hash: -2081171920268489084
214
216
  required_rubygems_version: !ruby/object:Gem::Requirement
215
217
  none: false
216
218
  requirements:
@@ -219,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
221
  version: '0'
220
222
  segments:
221
223
  - 0
222
- hash: -3468443137399922310
224
+ hash: -2081171920268489084
223
225
  requirements: []
224
226
  rubyforge_project:
225
227
  rubygems_version: 1.8.11
@@ -278,6 +280,7 @@ test_files:
278
280
  - spec/engine_spec.rb
279
281
  - spec/model/node_spec.rb
280
282
  - spec/model/flow_spec.rb
283
+ - spec/model/answer/money_spec.rb
281
284
  - spec/model/answer/base_spec.rb
282
285
  - spec/model/answer/salary_spec.rb
283
286
  - spec/model/answer/date_spec.rb