mixed_number_rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +45 -0
- data/lib/mixed_number_rails.rb +5 -0
- data/lib/mixed_number_rails/mixed_number.rb +53 -0
- data/lib/mixed_number_rails/mixed_number_attribute.rb +27 -0
- data/lib/mixed_number_rails/null_mixed_number.rb +23 -0
- data/lib/mixed_number_rails/version.rb +3 -0
- data/lib/tasks/mixed_number_rails_tasks.rake +4 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b8676681fd3432474f090ce15134a2a326aad6d8
|
4
|
+
data.tar.gz: 66b1146079528a526d3b6d031d0476024877ef00
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 86fe6a4339559badae1df6aa9b069a647c9de32d7618385dd957843ad575848f3fee8cf17b909e902372f893bfc383ee56a7449510740425f0f2fe17036cbf6d
|
7
|
+
data.tar.gz: e372e45a852815fcc83d5e68da6c74aba91c51e7cb458bb838e89c6fa05c876319c861db85144e338912451ea843b16262e865e7ac6bd6680c701eea5e3bf64b
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2015 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'MixedNumberRails'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
33
|
+
|
34
|
+
def set_remote_url(url)
|
35
|
+
`git remote set-url origin #{url}`
|
36
|
+
puts "Set remote url to #{url}"
|
37
|
+
end
|
38
|
+
|
39
|
+
task :ssh do
|
40
|
+
set_remote_url('git@github.com:tpadjen/mixed_number_rails.git')
|
41
|
+
end
|
42
|
+
|
43
|
+
task :http do
|
44
|
+
set_remote_url('https://github.com/tpadjen/mixed_number_rails.git')
|
45
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "mixed_number"
|
2
|
+
require_relative "null_mixed_number"
|
3
|
+
|
4
|
+
class MixedNumber
|
5
|
+
|
6
|
+
class << self
|
7
|
+
alias :parse_with_errors :parse
|
8
|
+
|
9
|
+
def parse(input)
|
10
|
+
parse_with_errors(input)
|
11
|
+
rescue MixedNumber::MixedNumberFormatError
|
12
|
+
NullMixedNumber.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def load(n)
|
16
|
+
self.parse(decode(n))
|
17
|
+
end
|
18
|
+
|
19
|
+
def dump(obj)
|
20
|
+
return nil if obj.nil?
|
21
|
+
|
22
|
+
unless obj.is_a?(self) || obj.respond_to?(:to_m)
|
23
|
+
raise ::ActiveRecord::SerializationTypeMismatch,
|
24
|
+
"Attribute was supposed to be a #{self}, but was a #{obj.class}. -- #{obj.inspect}"
|
25
|
+
end
|
26
|
+
|
27
|
+
encode(obj.to_m)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def encode(mixed)
|
33
|
+
f = ("%.32f" % mixed.to_f)[0..32]
|
34
|
+
w = mixed.whole.to_s
|
35
|
+
n = mixed.fraction.numerator.to_s
|
36
|
+
d = mixed.fraction.denominator.to_s
|
37
|
+
[f, w, n, d].join(":") + ":--encoded--"
|
38
|
+
end
|
39
|
+
|
40
|
+
def decode(n)
|
41
|
+
return n if !n
|
42
|
+
|
43
|
+
if n =~ /--encoded--/
|
44
|
+
f, w, n, d = *n.split(':')
|
45
|
+
"#{w} #{n}/#{d}"
|
46
|
+
else
|
47
|
+
n.to_m
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module MixedNumberRails
|
2
|
+
module MixedNumberAttribute
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def mixed_number_attribute(attribute)
|
7
|
+
serialize attribute, MixedNumber
|
8
|
+
|
9
|
+
validate -> {
|
10
|
+
if read_attribute(attribute).is_a?(NullMixedNumber)
|
11
|
+
errors.add(attribute, "is not a valid mixed number")
|
12
|
+
end
|
13
|
+
}
|
14
|
+
|
15
|
+
define_method(attribute.to_s + '=') do |value|
|
16
|
+
if value == nil
|
17
|
+
write_attribute(attribute, value)
|
18
|
+
else
|
19
|
+
write_attribute(attribute, MixedNumber(value))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ActiveRecord::Base.send :include, MixedNumberRails::MixedNumberAttribute
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mixed_number_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- tpadjen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.10
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.10
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mixed_number
|
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: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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
|
+
description:
|
70
|
+
email:
|
71
|
+
- tpadjen@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- MIT-LICENSE
|
77
|
+
- Rakefile
|
78
|
+
- lib/mixed_number_rails.rb
|
79
|
+
- lib/mixed_number_rails/mixed_number.rb
|
80
|
+
- lib/mixed_number_rails/mixed_number_attribute.rb
|
81
|
+
- lib/mixed_number_rails/null_mixed_number.rb
|
82
|
+
- lib/mixed_number_rails/version.rb
|
83
|
+
- lib/tasks/mixed_number_rails_tasks.rake
|
84
|
+
homepage: https://github.com/tpadjen/mixed_number_rails
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.2.2
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Store mixed numbers in an ActiveRecord model seamlessly with built-in validation.
|
108
|
+
test_files: []
|