human_value 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjA2YThmZGU4NmIyZTY5NDFiNzFmNDkzOTRkYjY5NTM3OTU5NmRlNg==
5
+ data.tar.gz: !binary |-
6
+ ODc4OGViZDJhOTRjYmNjNjdiN2JlYjZkZGIxZDI2MTdjNDA1NTlhMg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ OTI4YzM5MWQyNTdmZjEwNjAzMGUzZmQ0NWUwMWQ4ZGMxYTUyYmFjMDZhZDE3
10
+ MjJhMjAzYmFkZGJkNjI3NTM2Y2I0MDVmMTA4YjUxNWE0NGMwZmVhMjZkN2Rh
11
+ MzM2MTRlM2NiNDUxZWUyZDI5ZmM2MmZlYTE1ZTg1MmI4ZDExMzU=
12
+ data.tar.gz: !binary |-
13
+ YzA1OGUwZDc5ZjYyYWUwODEzNWVhNDJiMDBmMWE3OTZmOGQyNTI1MzNmNTBm
14
+ ZjFjZDI1ZWYwNTc1MWU2YWQwNTFlNDY5YmQ2NDMyODc0N2ViM2ZhOTg2YTcz
15
+ ZmVkOWE5NjhhZmYzNGQ3OGU3MzliYmM0Zjg0ZTQ1ZWVmYmEzY2I=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in human_value.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ben Eddy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # Human Value
2
+
3
+ `HumanValue` translates values into human-friendly formats.
4
+
5
+ For example, humans like to read "1,000,000" rather than "1000000", or "12/12/2012 at 4:30 PM MT" rather than "2012-12-12 16:30:00 -0700".
6
+
7
+ ### Example usage in Rails
8
+
9
+ In Rails, use the `h` abbreviated helper method.
10
+
11
+ ```ERB
12
+ <span><%= h 1000 %></span>
13
+ ```
14
+
15
+ to produce
16
+
17
+ ```HTML
18
+ <span>1,000</span>
19
+ ```
20
+
21
+ ## Installation
22
+
23
+ Add this line to your application's Gemfile and run `bundle install`:
24
+
25
+ gem 'human_value'
26
+
27
+ ## Default humanizations
28
+
29
+ Out of the box, `HumanValue` knows how to humanize the following types:
30
+
31
+ - Booleans
32
+ - Numerics
33
+ - Dates and times
34
+ - Enumerables
35
+ - Classes with a `model_name` (i.e. `ActiveModel` and `ActiveRecord` classes)
36
+
37
+ ## Extensions
38
+
39
+ `HumanValue` also comes with extensions for humanizing values from common Rails libraries. Extensions are enabled as follows in a `config/initializers/human_value.rb` file.
40
+
41
+ ```Ruby
42
+ HumanValue.enable_extension(:carrierwave)
43
+ ```
44
+
45
+ #### The following extensions are supported:
46
+
47
+ - `:carrierwave`
48
+ - renders image attachments as `<img>` tags
49
+ - renders file attachments as `<a>` tags
50
+
51
+ - `:naming`
52
+ - tries to render names by calling a `name` method
53
+
54
+ ## Writing custom humanizations
55
+
56
+ You may add your own humanizations using the `HumanValue.humanization` hook. You must define a `test` block (i.e. does this humanization apply) and a `coerce` block which performs the humanization.
57
+
58
+ For example, let's assume we have a `User` class with a `name` method which should fall back to the `email` method. We can add a custom humanization in `config/initializers/human_value.rb`.
59
+
60
+ ```Ruby
61
+ HumanValue.humanization :user do
62
+ test { |value| value.is_a?(User) }
63
+ coerce { |value| value.name || value.email }
64
+ end
65
+ ```
66
+
67
+ ```ERB
68
+ <% user_1 = User.new(name: "Timothy Tyler", email: "tim@gmail.com") %>
69
+ <% user_2 = User.new(name: nil, email: "anon@gmail.com") %>
70
+
71
+ <%= h user_1 %>
72
+ <%= h user_2 %>
73
+ ```
74
+
75
+ which produces
76
+
77
+ ```HTML
78
+ Timothy Tyler
79
+ anon@gmail.com
80
+ ```
81
+
82
+ ## Overriding existing humanizations
83
+
84
+ If you need to add a custom humanization which supercedes one of the default humanizations, use the `:prepend` option.
85
+
86
+ For example, perhaps you want to humanize booleans to "Yep" and "Nope" rather than "Yes" and "No".
87
+
88
+ ```Ruby
89
+ HumanValue.humanization :boolean, prepend: true do
90
+ test { |value| [true, false].include?(value) }
91
+ coerce { |value| value ? "Yep" : "Nope" }
92
+ end
93
+ ```
94
+
95
+ ## Contributing
96
+
97
+ 1. Fork it
98
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
99
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
100
+ 4. Push to the branch (`git push origin my-new-feature`)
101
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'human_value/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "human_value"
8
+ spec.version = HumanValue::VERSION
9
+ spec.authors = ["Ben Eddy"]
10
+ spec.email = ["bae@foraker.com"]
11
+ spec.description = %q{Ruby values fit for human consumption}
12
+ spec.summary = %q{HumanValue humanizes Ruby values to strings and supports adding custom humanizations}
13
+ spec.homepage = "https://github.com/foraker/human_value"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_dependency "i18n"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,55 @@
1
+ require "active_support/core_ext"
2
+ require "active_support/number_helper"
3
+ require "human_value/humanization"
4
+ require "human_value/value"
5
+ require "human_value/helpers"
6
+
7
+ Gem.find_files("human_value/extensions/**/*.rb").each { |path| require path }
8
+ Gem.find_files("human_value/integration/**/*.rb").each { |path| require path }
9
+
10
+ module HumanValue
11
+ def self.humanizations
12
+ @humanizations ||= []
13
+ end
14
+
15
+ def self.humanize(type, options = {}, &block)
16
+ modifier = options[:prepend] ? :unshift : :push
17
+ humanizations.public_send modifier, Humanization.new(type).instance_eval(&block)
18
+ end
19
+
20
+ def self.enable_extension(extension_name)
21
+ "HumanValue::Extensions::#{extension_name.to_s.classify}".constantize.load
22
+ rescue NameError
23
+ raise "Unknown extension '#{extension_name}'"
24
+ end
25
+
26
+ humanize :boolean do
27
+ test { |v| [true, false].include?(v) }
28
+ coerce { |v| v ? "Yes" : "No" }
29
+ end
30
+
31
+ humanize :number do
32
+ test { |v| v.is_a?(Numeric) }
33
+ coerce { |v| ActiveSupport::NumberHelper.number_to_delimited(v) }
34
+ end
35
+
36
+ humanize :timeish do
37
+ test { |v| v.respond_to?(:strftime) }
38
+ coerce { |v| I18n.localize(v) }
39
+ end
40
+
41
+ humanize :humanizable do
42
+ test { |v| v.respond_to?(:human) }
43
+ coerce { |v| v.human }
44
+ end
45
+
46
+ humanize :enumerable do
47
+ test { |v| v.respond_to?(:each) }
48
+ coerce { |v| Value.wrap(v).join(', ') }
49
+ end
50
+
51
+ humanize :model_name do
52
+ test { |v| v.respond_to?(:model_name) }
53
+ coerce { |v| v.model_name.human }
54
+ end
55
+ end
@@ -0,0 +1,21 @@
1
+ module HumanValue
2
+ module Extensions
3
+ module Carrierwave
4
+ def self.load
5
+ HumanValue.humanize :image do
6
+ test do |v|
7
+ v.respond_to?(:content_type) &&
8
+ v.content_type.to_s.starts_with?("image")
9
+ end
10
+
11
+ coerce { |v| "<img src='#{v}'>".html_safe }
12
+ end
13
+
14
+ HumanValue.humanize :file do
15
+ test { |v| v.respond_to?(:content_type) }
16
+ coerce { |v| "<a href='#{v}'>#{v.filename}</a>".html_safe }
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module HumanValue
2
+ module Extensions
3
+ module Naming
4
+ def self.load
5
+ HumanValue.humanize :named do
6
+ test { |v| v.respond_to?(:name) }
7
+ coerce { |v| v.name.to_s }
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,9 @@
1
+ module HumanValue
2
+ module Helpers
3
+ def humanize(value)
4
+ HumanValue::Value.new(value).to_s
5
+ end
6
+
7
+ alias :h :humanize
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ module HumanValue
2
+ class Humanization
3
+ def initialize(type)
4
+ @type
5
+ end
6
+
7
+ def matches?(value)
8
+ @test.call(value)
9
+ end
10
+
11
+ def call(value)
12
+ @coerce.call(value)
13
+ end
14
+
15
+ def test(&block)
16
+ @test = block
17
+ self
18
+ end
19
+
20
+ def coerce(&block)
21
+ @coerce = block
22
+ self
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ if defined?(::Rails)
2
+ module HumanValue
3
+ class Railtie < ::Rails::Railtie
4
+ initializer "human_value.view_helpers" do
5
+ ActionView::Base.send :include, HumanValue::Helpers
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ if defined?(Sinatra)
2
+ ::Sinatra::Application.send(:helpers, HumanValue::Helpers)
3
+ end
@@ -0,0 +1,23 @@
1
+ module HumanValue
2
+ class Value
3
+ def self.wrap(objects)
4
+ objects.map{ |object| new(object) }
5
+ end
6
+
7
+ def initialize(raw)
8
+ @raw = raw
9
+ end
10
+
11
+ def to_s
12
+ HumanValue.humanizations.each do |humanization|
13
+ return humanization.call(raw) if humanization.matches?(raw)
14
+ end
15
+
16
+ raw.to_s
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :raw
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module HumanValue
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ module HumanValue
4
+ describe Value do
5
+ before do
6
+ HumanValue.enable_extension(:carrierwave)
7
+ end
8
+
9
+ describe "an image attachment" do
10
+ it "returns an image tag" do
11
+ attachment = double({
12
+ content_type: "image/jpeg",
13
+ to_s: "/path/to/file.jpg"
14
+ })
15
+ expect(coerced(attachment)).to eq "<img src='/path/to/file.jpg'>"
16
+ end
17
+ end
18
+
19
+ describe "a file attachment" do
20
+ it "returns an link to the file" do
21
+ attachment = double({
22
+ content_type: "application/pdf",
23
+ filename: "file.pdf",
24
+ to_s: "/path/to/file.pdf"
25
+ })
26
+
27
+ expect(coerced(attachment)).to eq "<a href='/path/to/file.pdf'>file.pdf</a>"
28
+ end
29
+ end
30
+ end
31
+ end
32
+
File without changes
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ module HumanValue
4
+ describe Value do
5
+ before do
6
+ HumanValue.enable_extension(:naming)
7
+ end
8
+
9
+ describe "a named thing" do
10
+ it "returns the name" do
11
+ named = double(name: "Billows Wendley")
12
+ expect(coerced(named)).to eq "Billows Wendley"
13
+ end
14
+
15
+ it "handles missing names" do
16
+ named = double(name: nil)
17
+ expect(coerced(named)).to eq ""
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ module HumanValue
4
+ describe Value do
5
+ describe "#to_s" do
6
+ describe "a string" do
7
+ it "returns the string" do
8
+ expect(coerced("ABC")).to eq "ABC"
9
+ end
10
+ end
11
+
12
+ describe "nil" do
13
+ it "returns an empty string" do
14
+ expect(coerced(nil)).to eq ""
15
+ end
16
+ end
17
+
18
+ describe "a boolean" do
19
+ it "returns 'Yes' for true" do
20
+ expect(coerced(true)).to eq "Yes"
21
+ end
22
+
23
+ it "returns 'No' for false" do
24
+ expect(coerced(false)).to eq "No"
25
+ end
26
+ end
27
+
28
+ describe "a number" do
29
+ it "returns a number string" do
30
+ expect(coerced(400)).to eq "400"
31
+ end
32
+
33
+ it "adds delimiters" do
34
+ expect(coerced(4000)).to eq "4,000"
35
+ end
36
+ end
37
+
38
+ describe "a humanizable value" do
39
+ it "returns 'Yes' for true" do
40
+ humanizable = double(human: "Humanized")
41
+ expect(coerced(humanizable)).to eq "Humanized"
42
+ end
43
+ end
44
+
45
+ describe "a value with a model name" do
46
+ it "returns the human model name" do
47
+ humanizable = double(model_name: double(human: "Humanized"))
48
+ expect(coerced(humanizable)).to eq "Humanized"
49
+ end
50
+ end
51
+
52
+ describe "a time sort of thing" do
53
+ it "localizes Times" do
54
+ expect(coerced(Time.new(2012, 12, 12))).to eq "12/12/2012"
55
+ end
56
+
57
+ it "localizes Dates" do
58
+ expect(coerced(Date.new(2012, 12, 12))).to eq "12/12/2012"
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,28 @@
1
+ require "human_value"
2
+
3
+ RSpec.configure do |config|
4
+ class MockI18n
5
+ def initialize(translations = {})
6
+ @translations = translations
7
+ end
8
+
9
+ def localize(value)
10
+ value.strftime('%m/%d/%Y')
11
+ end
12
+
13
+ def translate(value, options = {})
14
+ @translations.fetch(value) do
15
+ options[:default]
16
+ end
17
+ end
18
+ end
19
+
20
+ config.before do
21
+ stub_const("I18n", MockI18n.new)
22
+ end
23
+
24
+ def coerced(value)
25
+ HumanValue::Value.new(value).to_s
26
+ end
27
+ end
28
+
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: human_value
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Eddy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: i18n
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Ruby values fit for human consumption
84
+ email:
85
+ - bae@foraker.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - human_value.gemspec
96
+ - lib/human_value.rb
97
+ - lib/human_value/extensions/carrierwave.rb
98
+ - lib/human_value/extensions/naming.rb
99
+ - lib/human_value/helpers.rb
100
+ - lib/human_value/humanization.rb
101
+ - lib/human_value/integration/rails.rb
102
+ - lib/human_value/integration/sinatra.rb
103
+ - lib/human_value/value.rb
104
+ - lib/human_value/version.rb
105
+ - spec/extensions/carrierwave_spec.rb
106
+ - spec/extensions/nameable.rb
107
+ - spec/extensions/naming_spec.rb
108
+ - spec/human_value_spec.rb
109
+ - spec/spec_helper.rb
110
+ homepage: https://github.com/foraker/human_value
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.0.3
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: HumanValue humanizes Ruby values to strings and supports adding custom humanizations
134
+ test_files:
135
+ - spec/extensions/carrierwave_spec.rb
136
+ - spec/extensions/nameable.rb
137
+ - spec/extensions/naming_spec.rb
138
+ - spec/human_value_spec.rb
139
+ - spec/spec_helper.rb
140
+ has_rdoc: