formatters 1.0.1
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.
- checksums.yaml +7 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +17 -0
- data/LICENSE.txt +22 -0
- data/README.md +90 -0
- data/Rakefile +2 -0
- data/formatters.gemspec +23 -0
- data/lib/formatters/formatters.rb +34 -0
- data/lib/formatters/railtie.rb +7 -0
- data/lib/formatters/version.rb +3 -0
- data/lib/formatters.rb +4 -0
- data/spec/formatters_spec.rb +52 -0
- data/spec/spec_helper.rb +2 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d946ac34fe64f63ab235ffe7755c2fbb784e49b2
|
4
|
+
data.tar.gz: 7553106d3c72f31ed48583c03b9f22f4e14dc1ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a780a4881b20839777cb99f16042ee791cec1850ef09cbb09a7e865b55c516fcd35d60dd61597bc717ae1324d45992ec1ba9a77332fb44ca90f704a758e6f1f
|
7
|
+
data.tar.gz: 1a385762c6de0c4fe7ba8badbbace4dc0e44c0b83e31de3266d3b729655b853bfdc106f6bddc3a9f4a6ed72e27cb52c1025f4d38a9f5e85dc27857f9fb80b5b0
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Keith Rowell
|
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,90 @@
|
|
1
|
+
# Formatters
|
2
|
+
|
3
|
+
Friendly data formatting for your Rails views.
|
4
|
+
|
5
|
+
Time.now.format_as :nice_date_time # => "January 8th, 2017 at 11:45"
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'formatters'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install formatters
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Create a `formatters.rb` in your `config/initializers` directory and add some formats as blocks. make a call to `config.define_format` with a symbol for the name of the format as an argument. The block code returns the formatted value.
|
24
|
+
|
25
|
+
Formatters.setup do |config|
|
26
|
+
|
27
|
+
config.define_format :amount do |value|
|
28
|
+
"%.2f" % value.round(2)
|
29
|
+
end
|
30
|
+
|
31
|
+
config.define_format :money do |value|
|
32
|
+
if value.to_f >= 0
|
33
|
+
"$#{value.format_as(:amount)}"
|
34
|
+
else
|
35
|
+
"-$#{value.abs.format_as(:amount)}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
config.define_format :invoice_number do |value|
|
40
|
+
value.to_s.rjust(5, '0')
|
41
|
+
end
|
42
|
+
|
43
|
+
config.define_format :nice_date_time do |value|
|
44
|
+
if value.kind_of? DateTime
|
45
|
+
value.strftime("%B #{value.day.ordinalize}, %Y at %H:%M")
|
46
|
+
elsif value.kind_of? Time
|
47
|
+
value.strftime("%B #{value.day.ordinalize}, %Y at %H:%M")
|
48
|
+
else
|
49
|
+
value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
config.define_format :yes_no do |value|
|
54
|
+
I18n.t((!!value).to_s)
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
To use a format in a view, or anywhere else that requires a common approach to displaying formatted data (emailers, etc), simply call the `format_as` method on your data object and pass the name of your defined format as a symbol.
|
60
|
+
|
61
|
+
true.format_as :yes_no # => "Yes"
|
62
|
+
|
63
|
+
899.99.format_as :money # => "$899.99"
|
64
|
+
|
65
|
+
Time.now.format_as :nice_date_time # => "January 8th, 2017 at 11:45"
|
66
|
+
|
67
|
+
|
68
|
+
You can chain formatters and build more complex formats from simple formats such as
|
69
|
+
|
70
|
+
config.define_format :amount do |value|
|
71
|
+
"%.2f" % value.round(2)
|
72
|
+
end
|
73
|
+
|
74
|
+
config.define_format :money do |value|
|
75
|
+
if value.to_f >= 0
|
76
|
+
"$#{value.format_as(:amount)}"
|
77
|
+
else
|
78
|
+
"-$#{value.abs.format_as(:amount)}"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
## Contributing
|
85
|
+
|
86
|
+
1. Fork it ( https://github.com/[my-github-username]/rails_default_value/fork )
|
87
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
88
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
89
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
90
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/formatters.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'formatters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "formatters"
|
8
|
+
spec.version = Formatters::VERSION
|
9
|
+
spec.authors = ["Keith Rowell"]
|
10
|
+
spec.email = ["keith@keithrowell.com"]
|
11
|
+
spec.summary = "Friendly data formatting for your Rails views."
|
12
|
+
spec.description = ""
|
13
|
+
spec.homepage = "https://github.com/keithrowell/formatters"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class Object
|
2
|
+
def format_as format
|
3
|
+
Formatters::format_as self, format
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
module Formatters
|
8
|
+
|
9
|
+
def self.setup &block
|
10
|
+
block.call self
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.format_as object, format
|
14
|
+
Base::format_as object, format
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.define_format format, &format_code
|
18
|
+
Base::define_format format, &format_code
|
19
|
+
end
|
20
|
+
|
21
|
+
class Base
|
22
|
+
|
23
|
+
@@formats = {}
|
24
|
+
|
25
|
+
def self.define_format format, &format_code
|
26
|
+
@@formats[format] = format_code
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.format_as object, format
|
30
|
+
@@formats[format].call(object)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/formatters.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
|
5
|
+
describe Formatters do
|
6
|
+
|
7
|
+
it "defines formats" do
|
8
|
+
|
9
|
+
Formatters.setup do |config|
|
10
|
+
config.define_format :money do |value|
|
11
|
+
"$#{value}"
|
12
|
+
end
|
13
|
+
|
14
|
+
config.define_format :serial_number do |value|
|
15
|
+
value.to_s.rjust(6, "0")
|
16
|
+
end
|
17
|
+
|
18
|
+
config.define_format :nice_date_time do |value|
|
19
|
+
if value.kind_of? DateTime
|
20
|
+
value.strftime("%B #{value.day}, %Y %H:%M")
|
21
|
+
elsif value.kind_of? Time
|
22
|
+
value.strftime("%B #{value.day}, %Y %H:%M")
|
23
|
+
else
|
24
|
+
value
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
value = 3.5
|
30
|
+
expect(value.format_as(:money)).to eq '$3.5'
|
31
|
+
|
32
|
+
value = '3.5'
|
33
|
+
expect(value.format_as(:money)).to eq '$3.5'
|
34
|
+
|
35
|
+
value = 3
|
36
|
+
expect(value.format_as(:money)).to eq '$3'
|
37
|
+
|
38
|
+
value = Time.parse 'March 19, 2016 20:44'
|
39
|
+
expect(value.format_as(:nice_date_time)).to eq 'March 19, 2016 20:44'
|
40
|
+
|
41
|
+
value = DateTime.parse 'March 19, 2016 20:44'
|
42
|
+
expect(value.format_as(:nice_date_time)).to eq 'March 19, 2016 20:44'
|
43
|
+
|
44
|
+
value = 1234
|
45
|
+
expect(value.format_as(:serial_number)).to eq '001234'
|
46
|
+
|
47
|
+
value = '1234'
|
48
|
+
expect(value.format_as(:serial_number)).to eq '001234'
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: formatters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Keith Rowell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-08 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: ''
|
42
|
+
email:
|
43
|
+
- keith@keithrowell.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- formatters.gemspec
|
55
|
+
- lib/formatters.rb
|
56
|
+
- lib/formatters/formatters.rb
|
57
|
+
- lib/formatters/railtie.rb
|
58
|
+
- lib/formatters/version.rb
|
59
|
+
- spec/formatters_spec.rb
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
homepage: https://github.com/keithrowell/formatters
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata: {}
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 2.5.1
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Friendly data formatting for your Rails views.
|
85
|
+
test_files:
|
86
|
+
- spec/formatters_spec.rb
|
87
|
+
- spec/spec_helper.rb
|