liquefied 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +69 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/liquefied/version.rb +3 -0
- data/lib/liquefied.rb +63 -0
- data/liquefied.gemspec +25 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d4d2b366587f41600a910b0c82ac82240b681796
|
4
|
+
data.tar.gz: 469c2944864fdea289b59b873173822261eaaf75
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 455e56a607ceb5e6be3c5507e75f7255a13b57db87a6f24b62000ee3c52acdb3cc45c99fc4080b2439c234ed8f4258799cb9500dd4c417968f01a8a76e4c3c8d
|
7
|
+
data.tar.gz: 6a023cb24f13ba8799d09d06c3800504a4376345aacd8fe169ca545bb71b2e33ce74a4ff4eb99fbbd94b35571f112a3887563e2556af85d63373fb3dfd56c5df
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Andrew Vit
|
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,69 @@
|
|
1
|
+
# Liquefied
|
2
|
+
|
3
|
+
Keep your value objects from setting into their final output format.
|
4
|
+
|
5
|
+
When formatting values in decorator objects we often want to have a default
|
6
|
+
format for their string output, but we also want to access the original values
|
7
|
+
to do arithmetic or other operations. Liquefied lets you have it both ways:
|
8
|
+
|
9
|
+
```
|
10
|
+
price = Liquefied.new(12.999) { |val| "$%.2f USD" % val }
|
11
|
+
price.zero? # false
|
12
|
+
price > 10 # true
|
13
|
+
price + 1 # 13.999
|
14
|
+
|
15
|
+
price.to_s # "$12.99 USD"
|
16
|
+
puts price # $12.99 USD
|
17
|
+
```
|
18
|
+
|
19
|
+
The method that invokes the block is a finalizer, which unwraps the liquefied
|
20
|
+
value to its output format. The default finalizer is `to_s`, which makes this
|
21
|
+
pattern helpful for implicit formatting in templates, so `<%= price %>`
|
22
|
+
outputs the formatted value.
|
23
|
+
|
24
|
+
The finalizer can be set to any other method you want.
|
25
|
+
|
26
|
+
### Use with ActiveSupport Core Extensions
|
27
|
+
|
28
|
+
ActiveSupport extends core Ruby classes with a format option on `to_s`, which
|
29
|
+
enables a simplified usage pattern instead of specifying a block:
|
30
|
+
|
31
|
+
```
|
32
|
+
require 'active_support/core_ext'
|
33
|
+
|
34
|
+
date = Date.new(2016, 1, 1)
|
35
|
+
|
36
|
+
date.to_s # "2016-01-01"
|
37
|
+
date.to_s(:long) # "January 1, 2016"
|
38
|
+
|
39
|
+
date = Liquefied.new(date, :long)
|
40
|
+
|
41
|
+
date.to_s # "January 1, 2016"
|
42
|
+
date.to_s(:short) # "Jan 1, 2016"
|
43
|
+
```
|
44
|
+
|
45
|
+
## Installation
|
46
|
+
|
47
|
+
Add this line to your application's Gemfile:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
gem 'liquefied'
|
51
|
+
```
|
52
|
+
|
53
|
+
And then execute:
|
54
|
+
|
55
|
+
$ bundle
|
56
|
+
|
57
|
+
Or install it yourself as:
|
58
|
+
|
59
|
+
$ gem install liquefied
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at
|
64
|
+
https://github.com/avit/liquefied.
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
69
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "liquefied"
|
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
data/lib/liquefied.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'liquefied/version'
|
2
|
+
|
3
|
+
class Liquefied < BasicObject
|
4
|
+
|
5
|
+
# Public: Wrap an object with a default finalizer method
|
6
|
+
#
|
7
|
+
# The object remains wrapped and responds to all its original methods
|
8
|
+
# until the finalizer method is called. The finalizer method can be
|
9
|
+
# set up with default arguments, which are passed if the method is called
|
10
|
+
# implicitly or with no options.
|
11
|
+
#
|
12
|
+
# object - The original value to wrap
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# Liquefied.new(12.333, :to_s) { |val| "%.2f" % val }
|
16
|
+
# Liquefied.new(Date.new(2016,1,1), :to_s, :long)
|
17
|
+
# #=> "January 1, 2016"
|
18
|
+
#
|
19
|
+
def initialize(original, *default_args, method: :to_s, &default_block)
|
20
|
+
@original = original
|
21
|
+
@finalizer = method
|
22
|
+
@default_args = default_args
|
23
|
+
@default_block = default_block
|
24
|
+
end
|
25
|
+
|
26
|
+
def inspect
|
27
|
+
"#<Liquefied(#{@original.class}):#{@original.object_id}>"
|
28
|
+
end
|
29
|
+
|
30
|
+
def object
|
31
|
+
@original
|
32
|
+
end
|
33
|
+
|
34
|
+
def ==(other)
|
35
|
+
@original == other
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def method_missing(method, *args, &block)
|
41
|
+
if method == @finalizer
|
42
|
+
_finalize!(*args, &block)
|
43
|
+
else
|
44
|
+
result = @original.public_send(method, *args, &block)
|
45
|
+
if result.class == @original.class
|
46
|
+
::Liquefied.new(result, *@default_args, method: @finalizer, &@default_block)
|
47
|
+
else
|
48
|
+
result
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def _finalize!(*args, &block)
|
54
|
+
args = @default_args if args.empty?
|
55
|
+
block = block || @default_block
|
56
|
+
if block
|
57
|
+
block.call(@original, *args)
|
58
|
+
else
|
59
|
+
@original.public_send(@finalizer, *args, &block)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/liquefied.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'liquefied/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "liquefied"
|
8
|
+
spec.version = Liquefied::VERSION
|
9
|
+
spec.authors = ["Andrew Vit"]
|
10
|
+
spec.email = ["andrew@avit.ca"]
|
11
|
+
|
12
|
+
spec.summary = %q{Defer value formatting until output}
|
13
|
+
spec.description = %q{Monadic wrapper for values that lets you set up a default finalizer.}
|
14
|
+
spec.homepage = "https://github.com/avit/liquefied"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: liquefied
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Vit
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-22 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Monadic wrapper for values that lets you set up a default finalizer.
|
56
|
+
email:
|
57
|
+
- andrew@avit.ca
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/liquefied.rb
|
72
|
+
- lib/liquefied/version.rb
|
73
|
+
- liquefied.gemspec
|
74
|
+
homepage: https://github.com/avit/liquefied
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Defer value formatting until output
|
98
|
+
test_files: []
|
99
|
+
has_rdoc:
|