acts_as_splittable 0.0.5 → 0.0.6
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 +4 -4
- data/README.md +19 -1
- data/acts_as_splittable.gemspec +2 -2
- data/lib/acts_as_splittable/config.rb +17 -0
- data/lib/acts_as_splittable/splittable.rb +27 -42
- data/lib/acts_as_splittable/splitter.rb +66 -0
- data/lib/acts_as_splittable/version.rb +1 -1
- data/lib/acts_as_splittable.rb +67 -47
- data/spec/models/splittable_spec.rb +128 -92
- data/spec/models.rb +9 -1
- metadata +6 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 46aa795a10f340366785314f8d7b06c134aef5d5
|
|
4
|
+
data.tar.gz: ac8a335ccb40e3a20a0c5e9276e4cad6b1f78881
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4e1d5fc52de895056ae9655ff439526d7660ee616299b21d0b263d0371f476d883a0c6d5ac761b15f521dbe2f6babafb3b22d4c85295023525f6a3b23539f60c
|
|
7
|
+
data.tar.gz: b81343e23eb3e736e20637ab50c83252fec7eaa2e4afdcf24ad3582938358ac0ec64bd78b0cf13dd3b037ac242779b0dde712cf43aa2fe61f21279a0a7e9bea4
|
data/README.md
CHANGED
|
@@ -171,6 +171,24 @@ p splittable.email_local #=> "splittable"
|
|
|
171
171
|
p splittable.email_domain #=> "example.com"
|
|
172
172
|
```
|
|
173
173
|
|
|
174
|
+
### Use Delimiter (Shorthand)
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
class Splittable < ActiveRecord::Base
|
|
178
|
+
|
|
179
|
+
acts_as_splittable split_on_change: true, join_on_change: true
|
|
180
|
+
|
|
181
|
+
splittable :email, delimiter: '@', partials: [:email_local, :email_domain]
|
|
182
|
+
# :delimiter will be expanded to:
|
|
183
|
+
# {
|
|
184
|
+
# split: ['@'],
|
|
185
|
+
# on_join: Proc.new{|values| values.join('@')}
|
|
186
|
+
# }
|
|
187
|
+
|
|
188
|
+
# ...
|
|
189
|
+
end
|
|
190
|
+
```
|
|
191
|
+
|
|
174
192
|
Contributing
|
|
175
193
|
--------------------
|
|
176
194
|
|
|
@@ -178,4 +196,4 @@ Contributing
|
|
|
178
196
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
179
197
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
180
198
|
4. Push to the branch (`git push origin my-new-feature`)
|
|
181
|
-
5. Create new Pull Request
|
|
199
|
+
5. Create new Pull Request
|
data/acts_as_splittable.gemspec
CHANGED
|
@@ -7,8 +7,8 @@ require "acts_as_splittable/version"
|
|
|
7
7
|
Gem::Specification.new do |s|
|
|
8
8
|
s.name = "acts_as_splittable"
|
|
9
9
|
s.version = ActsAsSplittable::VERSION
|
|
10
|
-
s.authors = ["tatat", "takkkun"]
|
|
11
|
-
s.email = ["ioiioioloo@gmail.com", "heartery@gmail.com"]
|
|
10
|
+
s.authors = ["tatat", "takkkun", "wneko"]
|
|
11
|
+
s.email = ["ioiioioloo@gmail.com", "heartery@gmail.com", "aoki@u-ne.co"]
|
|
12
12
|
s.homepage = "https://github.com/tatat/acts_as_splittable"
|
|
13
13
|
s.summary = "Create virtual attributes."
|
|
14
14
|
s.description = "Create virtual attributes."
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module ActsAsSplittable
|
|
2
|
+
class Config
|
|
3
|
+
def splitters
|
|
4
|
+
@splitters ||= []
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def splitter(key)
|
|
8
|
+
splitters.find do |splitter|
|
|
9
|
+
splitter.name == key.to_sym
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def inherit!(other)
|
|
14
|
+
splitters.replace (other.splitters + splitters).uniq(&:name)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -1,57 +1,34 @@
|
|
|
1
1
|
module ActsAsSplittable
|
|
2
|
-
|
|
3
2
|
module Splittable
|
|
4
3
|
|
|
5
|
-
def
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
else
|
|
9
|
-
column = column.to_sym
|
|
10
|
-
split, pattern, partials, on_split, on_join = self.class.splittable_columns[column]
|
|
11
|
-
value = send(column)
|
|
12
|
-
|
|
13
|
-
unless value.nil?
|
|
14
|
-
values = if on_split
|
|
15
|
-
splittable_run_callback(on_split, value)
|
|
16
|
-
elsif value
|
|
17
|
-
if split
|
|
18
|
-
value.to_s.split *(split.is_a?(Array) ? split : [split])
|
|
19
|
-
elsif matches = value.to_s.match(pattern)
|
|
20
|
-
matches[1..(matches.length - 1)]
|
|
21
|
-
end
|
|
22
|
-
end || []
|
|
4
|
+
def splittable_partials
|
|
5
|
+
@splittable_partials ||= {}
|
|
6
|
+
end
|
|
23
7
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
8
|
+
def split_column_values!(columns = nil)
|
|
9
|
+
splittable_aggregate_columns(columns) do |column, splitter|
|
|
10
|
+
value = __send__(column) or next
|
|
27
11
|
|
|
28
|
-
|
|
12
|
+
values = splitter.split(value, self)
|
|
13
|
+
splitter.partials.zip(values).each do |key, value|
|
|
14
|
+
__send__ :"#{key}=", value
|
|
29
15
|
end
|
|
16
|
+
reset_splittable_changed_partials splitter.partials
|
|
30
17
|
end
|
|
31
|
-
|
|
32
18
|
self
|
|
33
19
|
end
|
|
34
20
|
|
|
35
|
-
def join_column_values!(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
split, pattern, partials, on_split, on_join = self.class.splittable_columns[column.to_sym]
|
|
40
|
-
values = partials.map{|partial| send(partial) }
|
|
21
|
+
def join_column_values!(columns = nil)
|
|
22
|
+
splittable_aggregate_columns(columns) do |column, splitter|
|
|
23
|
+
values = splitter.partials.map {|partial| __send__(partial) }
|
|
24
|
+
next if values.include?(nil)
|
|
41
25
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
reset_splittable_changed_partials partials
|
|
45
|
-
end
|
|
26
|
+
__send__ :"#{column}=", splitter.restore(values, self)
|
|
27
|
+
reset_splittable_changed_partials splitter.partials
|
|
46
28
|
end
|
|
47
|
-
|
|
48
29
|
self
|
|
49
30
|
end
|
|
50
31
|
|
|
51
|
-
def splittable_partials
|
|
52
|
-
@splittable_partials ||= {}
|
|
53
|
-
end
|
|
54
|
-
|
|
55
32
|
protected
|
|
56
33
|
|
|
57
34
|
attr_writer :splittable_changed_partials
|
|
@@ -70,6 +47,15 @@ module ActsAsSplittable
|
|
|
70
47
|
end
|
|
71
48
|
|
|
72
49
|
private
|
|
50
|
+
def splittable_aggregate_columns(columns = nil)
|
|
51
|
+
config = self.class.splittable_config
|
|
52
|
+
columns = columns ? Array(columns) : config.splitters.collect(&:name)
|
|
53
|
+
columns.collect!(&:to_sym)
|
|
54
|
+
|
|
55
|
+
columns.collect do |column|
|
|
56
|
+
yield(column, config.splitter(column)) if block_given?
|
|
57
|
+
end
|
|
58
|
+
end
|
|
73
59
|
|
|
74
60
|
def splittable_run_callback(callback, *args)
|
|
75
61
|
if callback.is_a?(Proc)
|
|
@@ -78,7 +64,6 @@ module ActsAsSplittable
|
|
|
78
64
|
send(callback, *args)
|
|
79
65
|
end
|
|
80
66
|
end
|
|
81
|
-
|
|
82
|
-
end
|
|
83
67
|
|
|
84
|
-
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
module ActsAsSplittable
|
|
2
|
+
class Splitter < Struct.new(:name, :for_split, :pattern, :partials, :on_split, :on_join)
|
|
3
|
+
DEFAULTS = {
|
|
4
|
+
on_join: Proc.new {|values| values.join }
|
|
5
|
+
}.freeze
|
|
6
|
+
|
|
7
|
+
ALIASES = {
|
|
8
|
+
split: :for_split,
|
|
9
|
+
column: :name,
|
|
10
|
+
regex: :pattern,
|
|
11
|
+
}.freeze
|
|
12
|
+
|
|
13
|
+
def initialize(options = {})
|
|
14
|
+
@options = options
|
|
15
|
+
|
|
16
|
+
options = DEFAULTS.merge(options)
|
|
17
|
+
delimiter = options.delete(:delimiter)
|
|
18
|
+
|
|
19
|
+
options.each do |key, value|
|
|
20
|
+
case key
|
|
21
|
+
when *ALIASES.keys
|
|
22
|
+
self[ALIASES[key]] = value
|
|
23
|
+
else
|
|
24
|
+
self[key] = value
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
if delimiter
|
|
29
|
+
self.for_split = [delimiter]
|
|
30
|
+
self.on_join = Proc.new {|values| values.join(delimiter)}
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
self.partials ||= pattern_members
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def split(value, delegate = nil)
|
|
37
|
+
case
|
|
38
|
+
when on_split
|
|
39
|
+
delegation(delegate || self, on_split, value)
|
|
40
|
+
when for_split
|
|
41
|
+
value.to_s.split *Array(for_split)
|
|
42
|
+
when pattern
|
|
43
|
+
value.to_s.match(pattern).to_a.tap(&:shift)
|
|
44
|
+
else
|
|
45
|
+
Array(value)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def restore(values, delegate = nil)
|
|
50
|
+
delegation(delegate || self, on_join, values)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
def delegation(target, method, *args)
|
|
55
|
+
if method.is_a?(Proc)
|
|
56
|
+
target.instance_exec(*args, &method)
|
|
57
|
+
else
|
|
58
|
+
target.__send__(method, *args)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def pattern_members
|
|
63
|
+
pattern.names.map(&:to_sym)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
data/lib/acts_as_splittable.rb
CHANGED
|
@@ -2,11 +2,13 @@ $LOAD_PATH.unshift File.dirname(__FILE__)
|
|
|
2
2
|
|
|
3
3
|
require 'active_record'
|
|
4
4
|
require 'acts_as_splittable/splittable'
|
|
5
|
+
require 'acts_as_splittable/splitter'
|
|
6
|
+
require 'acts_as_splittable/config'
|
|
5
7
|
|
|
6
8
|
module ActsAsSplittable
|
|
7
9
|
|
|
8
10
|
def acts_as_splittable(options = {})
|
|
9
|
-
options
|
|
11
|
+
options.reverse_merge!(
|
|
10
12
|
callbacks: true,
|
|
11
13
|
predicates: false,
|
|
12
14
|
join_on_change: false,
|
|
@@ -29,65 +31,37 @@ module ActsAsSplittable
|
|
|
29
31
|
end
|
|
30
32
|
|
|
31
33
|
module ClassMethods
|
|
32
|
-
SPLITTABLE_DEFAULT_JOIN_PROCESS = Proc.new{|values| values.join }
|
|
33
|
-
|
|
34
34
|
attr_writer :splittable_options
|
|
35
35
|
|
|
36
|
-
def splittable_options
|
|
36
|
+
def splittable_options
|
|
37
37
|
@splittable_options ||= {}
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
def with_splittable_options(other_options = {})
|
|
41
|
-
|
|
42
|
-
self.splittable_options =
|
|
41
|
+
old = splittable_options
|
|
42
|
+
self.splittable_options = old.merge(other_options)
|
|
43
43
|
Proc.new.(splittable_options)
|
|
44
|
-
|
|
44
|
+
ensure
|
|
45
|
+
self.splittable_options = old
|
|
45
46
|
end
|
|
46
47
|
|
|
47
|
-
def
|
|
48
|
-
@
|
|
48
|
+
def splittable_config
|
|
49
|
+
@splittable_config ||= Config.new
|
|
49
50
|
end
|
|
50
51
|
|
|
51
52
|
def splittable(column, options)
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
partials.each do |partial|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
define_method :"#{partial}=" do |value|
|
|
62
|
-
splittable_partials[partial] = value
|
|
63
|
-
splittable_changed_partials << partial unless splittable_changed_partial? partial
|
|
64
|
-
|
|
65
|
-
self.class.with_splittable_options split_on_change: false do |options|
|
|
66
|
-
join_column_values! column if options[:join_on_change]
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
if splittable_options[:predicates]
|
|
71
|
-
define_method :"#{partial}_changed?" do
|
|
72
|
-
splittable_changed_partial? partial
|
|
73
|
-
end
|
|
74
|
-
end
|
|
53
|
+
options.merge!(name: column.to_sym)
|
|
54
|
+
splitter = Splitter.new(options)
|
|
55
|
+
splittable_config.splitters << splitter
|
|
56
|
+
|
|
57
|
+
splitter.partials.each do |partial|
|
|
58
|
+
define_splittable_getter(partial)
|
|
59
|
+
define_splittable_setter(partial, splitter)
|
|
60
|
+
define_splittable_predicator(partial) if splittable_options[:predicates]
|
|
75
61
|
end
|
|
76
62
|
|
|
77
63
|
if splittable_options[:split_on_change]
|
|
78
|
-
|
|
79
|
-
def #{column}=(value)
|
|
80
|
-
if defined?(super)
|
|
81
|
-
super
|
|
82
|
-
elsif respond_to?(:write_attribute, true)
|
|
83
|
-
write_attribute :#{column}, value
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
self.class.with_splittable_options join_on_change: false do
|
|
87
|
-
split_column_values! :#{column}
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
EOS
|
|
64
|
+
define_splittable_setter_hook(splitter.name)
|
|
91
65
|
end
|
|
92
66
|
end
|
|
93
67
|
|
|
@@ -95,7 +69,7 @@ module ActsAsSplittable
|
|
|
95
69
|
super
|
|
96
70
|
|
|
97
71
|
child.splittable_options = splittable_options.dup
|
|
98
|
-
child.
|
|
72
|
+
child.splittable_config.inherit! splittable_config
|
|
99
73
|
end
|
|
100
74
|
|
|
101
75
|
protected
|
|
@@ -103,8 +77,54 @@ module ActsAsSplittable
|
|
|
103
77
|
def splittable_module
|
|
104
78
|
@splittable_module ||= Module.new
|
|
105
79
|
end
|
|
106
|
-
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def define_splittable_getter(partial)
|
|
84
|
+
define_method partial do
|
|
85
|
+
splittable_partials[partial]
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def define_splittable_setter(partial, splitter)
|
|
90
|
+
define_method :"#{partial}=" do |value|
|
|
91
|
+
splittable_partials[partial] = value
|
|
92
|
+
|
|
93
|
+
unless splittable_changed_partial? partial
|
|
94
|
+
splittable_changed_partials << partial
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
self.class.with_splittable_options split_on_change: false do |options|
|
|
98
|
+
if options[:join_on_change]
|
|
99
|
+
join_column_values! splitter.name
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def define_splittable_predicator(partial)
|
|
106
|
+
define_method :"#{partial}_changed?" do
|
|
107
|
+
splittable_changed_partial? partial
|
|
108
|
+
end
|
|
109
|
+
end
|
|
107
110
|
|
|
111
|
+
def define_splittable_setter_hook(name)
|
|
112
|
+
splittable_module.module_eval do
|
|
113
|
+
define_method("#{name}=") do |value|
|
|
114
|
+
if defined?(super)
|
|
115
|
+
super(value)
|
|
116
|
+
elsif respond_to?(:write_attribute, true)
|
|
117
|
+
write_attribute name, value
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
self.class.with_splittable_options join_on_change: false do
|
|
121
|
+
split_column_values! name
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
end
|
|
108
128
|
end
|
|
109
129
|
|
|
110
130
|
ActiveRecord::Base.extend ActsAsSplittable
|
|
@@ -1,129 +1,133 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
shared_examples_for 'splittable' do
|
|
4
|
+
before :each do
|
|
5
|
+
@splittable1 = described_class.new(name: "#{described_class.name} 1")
|
|
6
|
+
@splittable1.email_local = 'splittable'
|
|
7
|
+
@splittable1.email_domain = 'example.com'
|
|
8
|
+
@splittable1.postal_code1 = '012'
|
|
9
|
+
@splittable1.postal_code2 = '3456'
|
|
10
|
+
@splittable1.phone_number1 = '012'
|
|
11
|
+
@splittable1.phone_number2 = '3456'
|
|
12
|
+
@splittable1.phone_number3 = '7890'
|
|
13
|
+
@splittable1.save!
|
|
14
|
+
|
|
15
|
+
@splittable2 = described_class.create!(
|
|
16
|
+
name: "#{described_class.name} 2",
|
|
17
|
+
email_local: 'splittable',
|
|
18
|
+
email_domain: 'example.com',
|
|
19
|
+
postal_code1: '012',
|
|
20
|
+
postal_code2: '3456',
|
|
21
|
+
phone_number1: '012',
|
|
22
|
+
phone_number2: '3456',
|
|
23
|
+
phone_number3: '7890',
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
@splittables = [@splittable1, @splittable2]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it 'should assign attributes' do
|
|
30
|
+
@splittables.each do |record|
|
|
31
|
+
record.assign_attributes(postal_code1: '987', postal_code2: '6543')
|
|
32
|
+
|
|
33
|
+
record.postal_code1.should == '987'
|
|
34
|
+
record.postal_code2.should == '6543'
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'should join partials before save' do
|
|
39
|
+
@splittables.each do |record|
|
|
40
|
+
record.email.should == 'splittable@example.com'
|
|
41
|
+
record.postal_code.should == '0123456'
|
|
42
|
+
record.phone_number.should == '01234567890'
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'should split columns after initialize' do
|
|
47
|
+
@splittables.each do |record|
|
|
48
|
+
splittable = described_class.find(record.id)
|
|
49
|
+
|
|
50
|
+
splittable.email_local.should == 'splittable'
|
|
51
|
+
splittable.email_domain.should == 'example.com'
|
|
52
|
+
splittable.postal_code1.should == '012'
|
|
53
|
+
splittable.postal_code2.should == '3456'
|
|
54
|
+
splittable.phone_number1.should == '012'
|
|
55
|
+
splittable.phone_number2.should == '3456'
|
|
56
|
+
splittable.phone_number3.should == '7890'
|
|
57
|
+
end
|
|
58
|
+
end
|
|
5
59
|
|
|
60
|
+
context 'when nil includes in partials or value of column is nil' do
|
|
6
61
|
before :each do
|
|
7
|
-
@splittable1
|
|
8
|
-
@splittable1.email_local = 'splittable'
|
|
9
|
-
@splittable1.email_domain = 'example.com'
|
|
10
|
-
@splittable1.postal_code1 = '012'
|
|
11
|
-
@splittable1.postal_code2 = '3456'
|
|
12
|
-
@splittable1.phone_number1 = '012'
|
|
13
|
-
@splittable1.phone_number2 = '3456'
|
|
14
|
-
@splittable1.phone_number3 = '7890'
|
|
62
|
+
@splittable1 = described_class.new(name: "#{described_class.name} 1")
|
|
15
63
|
@splittable1.save!
|
|
16
64
|
|
|
17
|
-
@splittable2 =
|
|
18
|
-
name:
|
|
19
|
-
email_local: 'splittable',
|
|
20
|
-
email_domain: 'example.com',
|
|
21
|
-
postal_code1: '012',
|
|
22
|
-
postal_code2: '3456',
|
|
23
|
-
phone_number1: '012',
|
|
24
|
-
phone_number2: '3456',
|
|
25
|
-
phone_number3: '7890',
|
|
65
|
+
@splittable2 = described_class.create!(
|
|
66
|
+
name: "#{described_class.name} 2"
|
|
26
67
|
)
|
|
27
68
|
|
|
28
69
|
@splittables = [@splittable1, @splittable2]
|
|
29
70
|
end
|
|
30
71
|
|
|
31
|
-
it 'should
|
|
72
|
+
it 'should not join partials before save' do
|
|
32
73
|
@splittables.each do |record|
|
|
33
|
-
record.
|
|
34
|
-
|
|
35
|
-
record.postal_code1.should == '987'
|
|
36
|
-
record.postal_code2.should == '6543'
|
|
74
|
+
record.email.should be_nil
|
|
37
75
|
end
|
|
38
76
|
end
|
|
39
77
|
|
|
40
|
-
it 'should
|
|
78
|
+
it 'should not split columns after initialize' do
|
|
41
79
|
@splittables.each do |record|
|
|
42
|
-
record.
|
|
43
|
-
record.postal_code.should == '0123456'
|
|
44
|
-
record.phone_number.should == '01234567890'
|
|
45
|
-
end
|
|
46
|
-
end
|
|
80
|
+
splittable = described_class.find(record.id)
|
|
47
81
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
splittable = klass.find(record.id)
|
|
51
|
-
|
|
52
|
-
splittable.email_local.should == 'splittable'
|
|
53
|
-
splittable.email_domain.should == 'example.com'
|
|
54
|
-
splittable.postal_code1.should == '012'
|
|
55
|
-
splittable.postal_code2.should == '3456'
|
|
56
|
-
splittable.phone_number1.should == '012'
|
|
57
|
-
splittable.phone_number2.should == '3456'
|
|
58
|
-
splittable.phone_number3.should == '7890'
|
|
82
|
+
splittable.email_local.should be_nil
|
|
83
|
+
splittable.email_domain.should be_nil
|
|
59
84
|
end
|
|
60
85
|
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
61
88
|
|
|
62
|
-
|
|
63
|
-
before do
|
|
64
|
-
@splittable = klass.new
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
it 'should return true when value is changed until #split_column_values! or #join_column_values! is called' do
|
|
68
|
-
@splittable.email_local_changed?.should_not be_true
|
|
69
|
-
@splittable.email_domain_changed?.should_not be_true
|
|
70
|
-
|
|
71
|
-
@splittable.email_local = 'splittable'
|
|
72
|
-
@splittable.email_local_changed?.should be_true
|
|
73
|
-
|
|
74
|
-
@splittable.email_domain = 'example.com'
|
|
75
|
-
@splittable.email_domain_changed?.should be_true
|
|
76
|
-
|
|
77
|
-
@splittable.join_column_values!
|
|
78
|
-
|
|
79
|
-
@splittable.email_local_changed?.should_not be_true
|
|
80
|
-
@splittable.email_domain_changed?.should_not be_true
|
|
89
|
+
shared_examples_for 'splittable with callbacks' do
|
|
81
90
|
|
|
82
|
-
|
|
83
|
-
|
|
91
|
+
describe '#*_changed?' do
|
|
92
|
+
before do
|
|
93
|
+
@splittable = described_class.new
|
|
94
|
+
end
|
|
84
95
|
|
|
85
|
-
|
|
86
|
-
|
|
96
|
+
it 'should return true when value is changed until #split_column_values! or #join_column_values! is called' do
|
|
97
|
+
@splittable.email_local_changed?.should_not be_true
|
|
98
|
+
@splittable.email_domain_changed?.should_not be_true
|
|
87
99
|
|
|
88
|
-
|
|
100
|
+
@splittable.email_local = 'splittable'
|
|
101
|
+
@splittable.email_local_changed?.should be_true
|
|
89
102
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
end
|
|
93
|
-
end
|
|
103
|
+
@splittable.email_domain = 'example.com'
|
|
104
|
+
@splittable.email_domain_changed?.should be_true
|
|
94
105
|
|
|
95
|
-
|
|
96
|
-
before :each do
|
|
97
|
-
@splittable1 = klass.new(name: "#{klass.name} 1")
|
|
98
|
-
@splittable1.save!
|
|
106
|
+
@splittable.join_column_values!
|
|
99
107
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
)
|
|
108
|
+
@splittable.email_local_changed?.should_not be_true
|
|
109
|
+
@splittable.email_domain_changed?.should_not be_true
|
|
103
110
|
|
|
104
|
-
|
|
105
|
-
|
|
111
|
+
@splittable.email_local = nil
|
|
112
|
+
@splittable.email_local_changed?.should be_true
|
|
106
113
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
record.email.should be_nil
|
|
110
|
-
end
|
|
111
|
-
end
|
|
114
|
+
@splittable.email_domain = nil
|
|
115
|
+
@splittable.email_domain_changed?.should be_true
|
|
112
116
|
|
|
113
|
-
|
|
114
|
-
@splittables.each do |record|
|
|
115
|
-
splittable = Splittable.find(record.id)
|
|
117
|
+
@splittable.split_column_values!
|
|
116
118
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
end
|
|
120
|
-
end
|
|
119
|
+
@splittable.email_local_changed?.should_not be_true
|
|
120
|
+
@splittable.email_domain_changed?.should_not be_true
|
|
121
121
|
end
|
|
122
122
|
end
|
|
123
|
+
|
|
123
124
|
end
|
|
124
125
|
|
|
125
126
|
describe Splittable do
|
|
126
|
-
|
|
127
|
+
it_behaves_like 'splittable'
|
|
128
|
+
it_behaves_like 'splittable with callbacks'
|
|
129
|
+
|
|
130
|
+
context 'when was given a proc to callbacks' do
|
|
127
131
|
it 'should call in the record' do
|
|
128
132
|
model_class = Class.new(ActiveRecord::Base) do
|
|
129
133
|
self.table_name = :splittables
|
|
@@ -167,6 +171,19 @@ describe Splittable do
|
|
|
167
171
|
end
|
|
168
172
|
end
|
|
169
173
|
|
|
174
|
+
|
|
175
|
+
describe SplittableInherited do
|
|
176
|
+
it_behaves_like 'splittable'
|
|
177
|
+
it_behaves_like 'splittable with callbacks'
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
describe SplittableInheritedInherited do
|
|
182
|
+
it_behaves_like 'splittable'
|
|
183
|
+
it_behaves_like 'splittable with callbacks'
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
|
|
170
187
|
describe SplittableSplitOrJoinOnChange do
|
|
171
188
|
|
|
172
189
|
it 'should join partials when one of partials is set and all of them are not nil' do
|
|
@@ -242,4 +259,23 @@ describe SplittableSplitOrJoinOnChangeWithAliasAttribute do
|
|
|
242
259
|
splittable1.email_domain.should == '1.example.com'
|
|
243
260
|
end
|
|
244
261
|
|
|
245
|
-
end
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
describe SplittableUseDelimiter do
|
|
265
|
+
|
|
266
|
+
let (:splittable) do
|
|
267
|
+
SplittableUseDelimiter.new(email: 'splittable@example.com')
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
it 'should split value when value is set' do
|
|
271
|
+
splittable.email_local.should == 'splittable'
|
|
272
|
+
splittable.email_domain.should == 'example.com'
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
it 'should join partials before save' do
|
|
276
|
+
splittable.email_local = 'splittable+1'
|
|
277
|
+
splittable.email_domain = 'mail.example.com'
|
|
278
|
+
splittable.email.should == 'splittable+1@mail.example.com'
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
end
|
data/spec/models.rb
CHANGED
|
@@ -65,4 +65,12 @@ class SplittableSplitOrJoinOnChangeWithAliasAttribute < ActiveRecord::Base
|
|
|
65
65
|
splittable :email, pattern: EMAIL_SPLIT_PATTERN, on_join: EMAIL_JOIN_PROCESS
|
|
66
66
|
|
|
67
67
|
alias_attribute :email_address, :email
|
|
68
|
-
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
class SplittableUseDelimiter < ActiveRecord::Base
|
|
71
|
+
self.table_name = 'splittables'
|
|
72
|
+
|
|
73
|
+
acts_as_splittable join_on_change: true, split_on_change: true, callbacks: false
|
|
74
|
+
|
|
75
|
+
splittable :email, delimiter: '@', partials: [:email_local, :email_domain]
|
|
76
|
+
end
|
metadata
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: acts_as_splittable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tatat
|
|
8
8
|
- takkkun
|
|
9
|
+
- wneko
|
|
9
10
|
autorequire:
|
|
10
11
|
bindir: bin
|
|
11
12
|
cert_chain: []
|
|
12
|
-
date: 2013-06-
|
|
13
|
+
date: 2013-06-24 00:00:00.000000000 Z
|
|
13
14
|
dependencies:
|
|
14
15
|
- !ruby/object:Gem::Dependency
|
|
15
16
|
name: rails
|
|
@@ -77,6 +78,7 @@ description: Create virtual attributes.
|
|
|
77
78
|
email:
|
|
78
79
|
- ioiioioloo@gmail.com
|
|
79
80
|
- heartery@gmail.com
|
|
81
|
+
- aoki@u-ne.co
|
|
80
82
|
executables: []
|
|
81
83
|
extensions: []
|
|
82
84
|
extra_rdoc_files: []
|
|
@@ -89,7 +91,9 @@ files:
|
|
|
89
91
|
- Rakefile
|
|
90
92
|
- acts_as_splittable.gemspec
|
|
91
93
|
- lib/acts_as_splittable.rb
|
|
94
|
+
- lib/acts_as_splittable/config.rb
|
|
92
95
|
- lib/acts_as_splittable/splittable.rb
|
|
96
|
+
- lib/acts_as_splittable/splitter.rb
|
|
93
97
|
- lib/acts_as_splittable/version.rb
|
|
94
98
|
- lib/tasks/acts_as_splittable_tasks.rake
|
|
95
99
|
- spec/database.yml.sample
|