decanter 0.5.0 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/Gemfile.lock +1 -1
- data/lib/decanter.rb +3 -2
- data/lib/decanter/base.rb +3 -0
- data/lib/decanter/core.rb +11 -9
- data/lib/decanter/extensions.rb +3 -4
- data/lib/decanter/railtie.rb +9 -0
- data/lib/decanter/value_parser.rb +5 -0
- data/lib/decanter/value_parser/base.rb +2 -0
- data/lib/decanter/value_parser/boolean_parser.rb +5 -9
- data/lib/decanter/value_parser/core.rb +29 -2
- data/lib/decanter/value_parser/date_parser.rb +5 -4
- data/lib/decanter/value_parser/datetime_parser.rb +13 -0
- data/lib/decanter/value_parser/float_parser.rb +13 -0
- data/lib/decanter/value_parser/integer_parser.rb +15 -0
- data/lib/decanter/value_parser/phone_parser.rb +13 -0
- data/lib/decanter/value_parser/string_parser.rb +5 -2
- data/lib/decanter/version.rb +1 -1
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f0715bf7c746e8f542d5830ff831d01f2082b173
|
4
|
+
data.tar.gz: 4a2a9c5001d035801b7f5c1c86fcf579630e2c61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b66f3d3a23e34d3f375aec969a6e62d6b1b0a1b8f9b41eebfef6c48872fab0010bb2a952f97c0c3a3d0d9c546fd4686972059182ddc5a3a506122fbae77e2e7f
|
7
|
+
data.tar.gz: 110960e0df21ba1c2512eae1dc77345d4df059a1851f442e9bb8709f761e6d2f23a270a71c01467f7d1dd8090f1e245a10b748b543fa2165791383208916691e
|
data/.rspec
ADDED
data/Gemfile.lock
CHANGED
data/lib/decanter.rb
CHANGED
@@ -9,15 +9,16 @@ module Decanter
|
|
9
9
|
def self.decanter_for(klass_or_sym)
|
10
10
|
name = klass_or_sym.is_a?(Class) ?
|
11
11
|
klass_or_sym.name :
|
12
|
-
klass_or_sym.to_s.singularize.
|
12
|
+
klass_or_sym.to_s.singularize.camelize
|
13
13
|
|
14
14
|
@@decanters["#{name}Decanter"] || (raise NameError.new("unknown decanter #{name}Decanter"))
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
require 'decanter/version'
|
19
|
-
require 'active_support/
|
19
|
+
require 'active_support/all'
|
20
20
|
require 'decanter/base'
|
21
21
|
require 'decanter/core'
|
22
22
|
require 'decanter/extensions'
|
23
23
|
require 'decanter/value_parser'
|
24
|
+
require 'decanter/railtie' if defined? ::Rails::Railtie
|
data/lib/decanter/base.rb
CHANGED
data/lib/decanter/core.rb
CHANGED
@@ -3,20 +3,19 @@ module Decanter
|
|
3
3
|
|
4
4
|
def self.included(base)
|
5
5
|
base.extend(ClassMethods)
|
6
|
-
Decanter.register(base)
|
7
6
|
end
|
8
7
|
|
9
8
|
module ClassMethods
|
10
9
|
|
11
10
|
def associations
|
12
|
-
@associations ||= {}
|
11
|
+
@associations ||= {}.with_indifferent_access
|
13
12
|
end
|
14
13
|
|
15
14
|
def inputs
|
16
|
-
@inputs ||= {}
|
15
|
+
@inputs ||= {}.with_indifferent_access
|
17
16
|
end
|
18
17
|
|
19
|
-
def input(name, type, **options)
|
18
|
+
def input(name=nil, type, **options)
|
20
19
|
set_input options, {
|
21
20
|
name: name,
|
22
21
|
options: options.reject { |k| k == :context },
|
@@ -32,7 +31,7 @@ module Decanter
|
|
32
31
|
(inputs[context || :default] || {})[name]
|
33
32
|
end
|
34
33
|
|
35
|
-
def has_many(name, **options)
|
34
|
+
def has_many(name=nil, **options)
|
36
35
|
set_association options, {
|
37
36
|
key: options[:key] || "#{name}_attributes".to_sym,
|
38
37
|
name: name,
|
@@ -41,7 +40,7 @@ module Decanter
|
|
41
40
|
}
|
42
41
|
end
|
43
42
|
|
44
|
-
def has_one(name, **options)
|
43
|
+
def has_one(name=nil, **options)
|
45
44
|
set_association options, {
|
46
45
|
key: options[:key] || "#{name}_attributes".to_sym,
|
47
46
|
name: name,
|
@@ -79,6 +78,7 @@ module Decanter
|
|
79
78
|
end
|
80
79
|
|
81
80
|
def decant(args={}, context=nil)
|
81
|
+
args = args.to_h if args.is_a?(ActionController::Parameters) # i.e. Rails 5
|
82
82
|
Hash[
|
83
83
|
args.map { |name, value| handle_arg(name, value, context) }.compact
|
84
84
|
]
|
@@ -87,7 +87,7 @@ module Decanter
|
|
87
87
|
def handle_arg(name, value, context)
|
88
88
|
case
|
89
89
|
when input_cfg = input_for(name, context)
|
90
|
-
[name, parse(input_cfg[:type], value)]
|
90
|
+
[name, parse(name, input_cfg[:type], value, input_cfg[:options])]
|
91
91
|
when assoc = has_one_for(name, context)
|
92
92
|
[assoc.pop[:key], Decanter::decanter_for(assoc.first).decant(value, context)]
|
93
93
|
when assoc = has_many_for(name, context)
|
@@ -98,8 +98,10 @@ module Decanter
|
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
def parse(type, val)
|
102
|
-
|
101
|
+
def parse(name, type, val, options)
|
102
|
+
type ?
|
103
|
+
ValueParser.value_parser_for(type).parse(name, val, options) :
|
104
|
+
val
|
103
105
|
end
|
104
106
|
end
|
105
107
|
end
|
data/lib/decanter/extensions.rb
CHANGED
@@ -6,12 +6,12 @@ module Decanter
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def decant_update(args={}, context=nil)
|
9
|
-
self.attributes = self.decant(args, context)
|
9
|
+
self.attributes = self.class.decant(args, context)
|
10
10
|
self.save(context: context)
|
11
11
|
end
|
12
12
|
|
13
13
|
def decant_update!(args={}, context=nil)
|
14
|
-
self.attributes = self.decant(args, context)
|
14
|
+
self.attributes = self.class.decant(args, context)
|
15
15
|
self.save!(context: context)
|
16
16
|
end
|
17
17
|
|
@@ -26,9 +26,8 @@ module Decanter
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def decant(args, context)
|
29
|
-
decanter_for(self).decant(args, context)
|
29
|
+
Decanter.decanter_for(self).decant(args, context)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
34
|
-
ActiveRecord::Base.include(Decanter::Extensions) if defined? ActiveRecord
|
@@ -15,4 +15,9 @@ end
|
|
15
15
|
require_relative 'value_parser/base'
|
16
16
|
require_relative 'value_parser/core'
|
17
17
|
require_relative 'value_parser/boolean_parser'
|
18
|
+
require_relative 'value_parser/date_parser'
|
19
|
+
require_relative 'value_parser/datetime_parser'
|
18
20
|
require_relative 'value_parser/string_parser'
|
21
|
+
require_relative 'value_parser/phone_parser'
|
22
|
+
require_relative 'value_parser/float_parser'
|
23
|
+
require_relative 'value_parser/integer_parser'
|
@@ -1,15 +1,11 @@
|
|
1
1
|
module Decanter
|
2
2
|
module ValueParser
|
3
3
|
class BooleanParser < Base
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
true
|
10
|
-
else
|
11
|
-
false
|
12
|
-
end
|
4
|
+
|
5
|
+
allow TrueClass, FalseClass
|
6
|
+
|
7
|
+
parser do |name, val, options|
|
8
|
+
[1, '1'].include?(val) || !!/true/i.match(val.to_s)
|
13
9
|
end
|
14
10
|
end
|
15
11
|
end
|
@@ -1,14 +1,41 @@
|
|
1
1
|
module Decanter
|
2
2
|
module ValueParser
|
3
3
|
module Core
|
4
|
+
|
4
5
|
def self.included(base)
|
5
6
|
base.extend(ClassMethods)
|
6
7
|
ValueParser.register(base)
|
7
8
|
end
|
8
9
|
|
9
10
|
module ClassMethods
|
10
|
-
|
11
|
-
|
11
|
+
|
12
|
+
def parse(name, val=nil, options={})
|
13
|
+
|
14
|
+
if val.blank?
|
15
|
+
if options[:required]
|
16
|
+
raise ArgumentError.new("No value for required argument: #{name}")
|
17
|
+
else
|
18
|
+
return val
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
if @allowed && @allowed.include?(val.class)
|
23
|
+
return val
|
24
|
+
end
|
25
|
+
|
26
|
+
unless @parser
|
27
|
+
raise ArgumentError.new("No parser for argument: #{name} with type: #{val.class}")
|
28
|
+
end
|
29
|
+
|
30
|
+
@parser.call(name, val, options)
|
31
|
+
end
|
32
|
+
|
33
|
+
def parser(&block)
|
34
|
+
@parser = block
|
35
|
+
end
|
36
|
+
|
37
|
+
def allow(*args)
|
38
|
+
@allowed = args
|
12
39
|
end
|
13
40
|
end
|
14
41
|
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module Decanter
|
2
2
|
module ValueParser
|
3
3
|
class DateParser < Base
|
4
|
-
|
4
|
+
|
5
|
+
allow Date
|
6
|
+
|
7
|
+
parser do |name, val, options|
|
5
8
|
parse_format = options.fetch(:parse_format, '%m/%d/%Y')
|
6
|
-
|
7
|
-
return input_value if input_value.is_a?(Date)
|
8
|
-
::Date.strptime(input_value, parse_format)
|
9
|
+
::Date.strptime(val, parse_format)
|
9
10
|
end
|
10
11
|
end
|
11
12
|
end
|
data/lib/decanter/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decanter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Francis
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-02-
|
12
|
+
date: 2016-02-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -75,6 +75,7 @@ executables: []
|
|
75
75
|
extensions: []
|
76
76
|
extra_rdoc_files: []
|
77
77
|
files:
|
78
|
+
- ".rspec"
|
78
79
|
- Gemfile
|
79
80
|
- Gemfile.lock
|
80
81
|
- LICENSE.txt
|
@@ -87,11 +88,16 @@ files:
|
|
87
88
|
- lib/decanter/base.rb
|
88
89
|
- lib/decanter/core.rb
|
89
90
|
- lib/decanter/extensions.rb
|
91
|
+
- lib/decanter/railtie.rb
|
90
92
|
- lib/decanter/value_parser.rb
|
91
93
|
- lib/decanter/value_parser/base.rb
|
92
94
|
- lib/decanter/value_parser/boolean_parser.rb
|
93
95
|
- lib/decanter/value_parser/core.rb
|
94
96
|
- lib/decanter/value_parser/date_parser.rb
|
97
|
+
- lib/decanter/value_parser/datetime_parser.rb
|
98
|
+
- lib/decanter/value_parser/float_parser.rb
|
99
|
+
- lib/decanter/value_parser/integer_parser.rb
|
100
|
+
- lib/decanter/value_parser/phone_parser.rb
|
95
101
|
- lib/decanter/value_parser/string_parser.rb
|
96
102
|
- lib/decanter/version.rb
|
97
103
|
homepage: https://github.com/launchpadlab/decanter
|
@@ -115,7 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
121
|
version: '0'
|
116
122
|
requirements: []
|
117
123
|
rubyforge_project:
|
118
|
-
rubygems_version: 2.4.
|
124
|
+
rubygems_version: 2.4.5.1
|
119
125
|
signing_key:
|
120
126
|
specification_version: 4
|
121
127
|
summary: Form Parser for Rails
|