crease 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ffe0dc0ffb7fd2e806caf07209cf80c4c8dc2d60
4
+ data.tar.gz: 5cecc40b58f2c6ae4bc150ed75ba380d37cfa653
5
+ SHA512:
6
+ metadata.gz: 56ad2f40fe707a4796eabeccb210e7ef242de23650c781fe1bc5aa98c5ba879449b69fecfb415ef5c2cd9ac0fb0dd8c5526bbc48204c5dfd8404068e813d11cc
7
+ data.tar.gz: c7bfb0b67ae2c887465e68877c9017ebe3f35a9141d8cd4c5464e3ac5ccf56a28811a001ecc5a4873e08ded54e5e1c3b62831a014e8a71cf03fc353db54a253f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at cloydster@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crease.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Matt Cloyd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,131 @@
1
+ # Crease
2
+
3
+ Generate dynamic text about increasing and decreasing numbers.
4
+
5
+ ## Usage
6
+
7
+ Let's say you're writing a dynamic report that needs to put data in context,
8
+ explaining it in simple English.
9
+
10
+ You want to tell the world that thetotal population of Boston increased, and
11
+ that the total population in Pittsburgh decreased, but you only want to write
12
+ one template that takes in data from each city and displays the right text.
13
+
14
+ With Crease, you can write:
15
+
16
+ ```ruby'
17
+ # app/views/city/report.html.erb
18
+ <p>
19
+ In <%= @city.name %>, the total population
20
+ <%= increased.by @city.population_in_2000, @city.population_in_2010 %>,
21
+ <%= an.increase.of(@city.population_in_2000, @city.population_in_2010).percent %>,
22
+ <p>
23
+ ```
24
+
25
+ For Boston, this evaluates to:
26
+
27
+ ```
28
+ <p>
29
+ In Boston, the total population increased by 30,018, an increase of 105%.
30
+ <p>
31
+ ```
32
+
33
+ > Note: we think that '105%' here is misleading: see the roadmap for more.
34
+
35
+ For Pittsburgh, the same template evaluates to:
36
+
37
+ ```
38
+ <p>
39
+ In Pittsburgh, the total population decreased by 28,068, an decrease of 8.4%.
40
+ <p>
41
+ ```
42
+
43
+ #### Examples
44
+
45
+ The following are examples of text you can write with Crease.
46
+
47
+ ```ruby
48
+ # In the context of a template
49
+
50
+ increased.by(1) #=> 'increased by 1.0'
51
+ increased.by(-1) #=> 'decreased by 1.0'
52
+ increased.by(2, 4) #=> 'increased by 2.0'
53
+ decreased.by(2, 4) #=> 'increased by 2.0'
54
+ # `#decreased` is just an alias for `#increased`
55
+ increased.by(1).percent #=> 'increased by 1.0%'
56
+ increased.by(2, 4).percent #=> 'increased by 200.0%'
57
+ # kind of misleading, will change in next release
58
+ an.increase.of(2) #=> 'an increase of 2.0'
59
+ ```
60
+
61
+ Inside templates, these method chains will be implicitly converted to strings,
62
+ with ERb or Haml calling `#to_s` on them.
63
+
64
+ If you are working with these outside of a template, you'll need to call `#to_s`
65
+ at the end of the method chain yourself.
66
+
67
+ ```ruby
68
+ # Outside a template
69
+
70
+ increased.by(1) #=> #<Crease::Builder:0x007fecf8a35c90 @criteria={:context=>nil, :tense=>:past, :word=>:by, :args=>[1.0], :sigfig=>2}>
71
+ increased.by(1).to_s #=> 'increased by 1.0'
72
+ ```
73
+
74
+ For dynamic templates, pass in variables instead of fixed numbers.
75
+
76
+ #### Options
77
+
78
+ ## Installation
79
+
80
+ Add this line to your application's Gemfile:
81
+
82
+ ```rubyinc
83
+ gem 'crease'
84
+ ```
85
+
86
+ And then execute:
87
+
88
+ $ bundle install
89
+
90
+ Or install it yourself as:
91
+
92
+ $ gem install crease
93
+
94
+ #### Rails
95
+
96
+ Include the `TextHelpers` module in your `ApplicationHelper`, or a more specific
97
+ helper if appropriate.
98
+
99
+ ```ruby
100
+ # app/helpers/application_helper.rb
101
+
102
+ class ApplicationHelper
103
+ include Crease::TextHelpers
104
+
105
+ # ... other helper code
106
+ end
107
+ ```
108
+
109
+ ## Development
110
+
111
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
112
+
113
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
114
+
115
+ ## Contributing
116
+
117
+ Bug reports and pull requests are welcome on GitHub at https://github.com/beechnut/crease. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
118
+
119
+ ## Roadmap
120
+
121
+ - Significant digits are passed in differently from other options, and we're not testing whether it's possible to convert them to integers.
122
+ - We'd like to have a way to configure the gem -- for Rails, in an initializer -- so that we can specify default significant figures there. If someone wants everything to be an integer by default, it would be easier to do it there.
123
+ - Might even allow a Proc-based option that sets sigfigs dynamically.
124
+ - Add documentation about understanding sigfigs.
125
+ - Right now, `increased.by(2, 4).percent` returns "increased by 200.0%", which is a little misleading. We want `changed.by(2, 4).percent` to say "changed by 200.0%", and `increased.by(2, 4).percent` to say "increased by 100%".
126
+
127
+
128
+ ## License
129
+
130
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
131
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "crease"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/crease.gemspec ADDED
@@ -0,0 +1,44 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'crease/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "crease"
8
+ spec.version = Crease::VERSION
9
+ spec.authors = ["Matt Cloyd"]
10
+ spec.email = ["cloydster@gmail.com"]
11
+
12
+ spec.summary = %q{Generate dynamic text about increasing and decreasing numbers.}
13
+ spec.description = %q{
14
+ If you're writing a dynamic, data-driven narrative, you might need to have
15
+ text like "an increase of 10%" or "decreased by 2", depending on parameters.
16
+ With Crease, you can include text helpers to write English-like code which
17
+ changes the text based on the parameters you pass it.
18
+
19
+ Crease can calculate the difference between numbers and can represent them
20
+ as percentages.
21
+ }
22
+ spec.homepage = "https://github.com/beechnut/crease"
23
+ spec.license = "MIT"
24
+
25
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
26
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
27
+ # if spec.respond_to?(:metadata)
28
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
29
+ # else
30
+ # raise "RubyGems 2.0 or newer is required to protect against " \
31
+ # "public gem pushes."
32
+ # end
33
+
34
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
35
+ f.match(%r{^(test|spec|features)/})
36
+ end
37
+ spec.bindir = "exe"
38
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
39
+ spec.require_paths = ["lib"]
40
+
41
+ spec.add_development_dependency "bundler", "~> 1.13"
42
+ spec.add_development_dependency "rake", "~> 10.0"
43
+ spec.add_development_dependency "rspec", "~> 3.0"
44
+ end
@@ -0,0 +1,102 @@
1
+ module Crease
2
+ class Builder
3
+
4
+ def criteria
5
+ @criteria ||= {}
6
+ end
7
+
8
+ def initialize(context: nil, tense: :present)
9
+ criteria.merge!({ context: context, tense: tense })
10
+ self
11
+ end
12
+
13
+ def increase
14
+ criteria.merge!({ tense: :present })
15
+ self
16
+ end
17
+
18
+ alias_method :decrease, :increase
19
+
20
+ def by(*args, sigfig: 2)
21
+ criteria.merge!({ word: :by })
22
+ criteria.merge!({ args: args.map(&:to_f) })
23
+ criteria.merge!({ sigfig: sigfig })
24
+ self
25
+ end
26
+
27
+ def of(*args, sigfig: 2)
28
+ criteria.merge!({ word: :of })
29
+ criteria.merge!({ args: args.map(&:to_f) })
30
+ criteria.merge!({ sigfig: sigfig })
31
+ self
32
+ end
33
+
34
+ def to_s
35
+ if criteria[:percent]
36
+ "#{base_string} #{percent_change}%"
37
+ else
38
+ "#{base_string} #{value}"
39
+ end
40
+ end
41
+
42
+ def percent
43
+ criteria.merge!({ percent: true })
44
+ self
45
+ end
46
+
47
+ def base_string
48
+ past = criteria[:tense] == :past ? 'd' : ''
49
+ str = "#{article}#{increase_or_decrease}#{past}"
50
+ str << " #{criteria[:word]}" if criteria[:word]
51
+ str
52
+ end
53
+
54
+ private
55
+
56
+ def article
57
+ return unless criteria[:context]
58
+ # Could use ActiveSupport #indefinitize
59
+ if increase_or_decrease == :increase
60
+ 'an '
61
+ elsif increase_or_decrease == :decrease
62
+ 'a '
63
+ end
64
+ end
65
+
66
+ def increase_or_decrease
67
+ args = criteria[:args]
68
+ if args.count == 2
69
+ args.last > args.first ? :increase : :decrease
70
+ else
71
+ args.first > 0 ? :increase : :decrease
72
+ end
73
+ end
74
+
75
+ def value
76
+ base_calculation.round(criteria[:sigfig])
77
+ end
78
+
79
+ def percent_change
80
+ args = criteria[:args]
81
+ if args.count == 2
82
+ if args.last > args.first
83
+ base_calculation
84
+ else
85
+ base_calculation.abs / args.first
86
+ end * 100.to_f
87
+ else
88
+ base_calculation
89
+ end.round(criteria[:sigfig])
90
+ end
91
+
92
+ def base_calculation
93
+ args = criteria[:args]
94
+ if args.count == 2
95
+ (args.last - args.first).abs
96
+ else
97
+ args.first.abs
98
+ end
99
+ end
100
+
101
+ end
102
+ end
@@ -0,0 +1,19 @@
1
+ module Crease
2
+ module TextHelpers
3
+
4
+ def increased
5
+ Crease::Builder.new(tense: :past)
6
+ end
7
+
8
+ alias_method :decreased, :increased
9
+
10
+ def an
11
+ Crease::Builder.new(context: :an)
12
+ end
13
+
14
+ def a
15
+ Crease::Builder.new(context: :a)
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Crease
2
+ VERSION = "0.1.1"
3
+ end
data/lib/crease.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "crease/version"
2
+ require "crease/builder"
3
+ require "crease/text_helpers"
4
+
5
+ module Crease
6
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crease
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Cloyd
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: "\n If you're writing a dynamic, data-driven narrative, you might
56
+ need to have\n text like \"an increase of 10%\" or \"decreased by 2\", depending
57
+ on parameters.\n With Crease, you can include text helpers to write English-like
58
+ code which\n changes the text based on the parameters you pass it.\n\n Crease
59
+ can calculate the difference between numbers and can represent them\n as percentages.\n
60
+ \ "
61
+ email:
62
+ - cloydster@gmail.com
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - ".gitignore"
68
+ - ".rspec"
69
+ - ".travis.yml"
70
+ - CODE_OF_CONDUCT.md
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/console
76
+ - bin/setup
77
+ - crease.gemspec
78
+ - lib/crease.rb
79
+ - lib/crease/builder.rb
80
+ - lib/crease/text_helpers.rb
81
+ - lib/crease/version.rb
82
+ homepage: https://github.com/beechnut/crease
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.5.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Generate dynamic text about increasing and decreasing numbers.
106
+ test_files: []