acts_as_splittable 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d4595fb64c43327405f29ae3530fe069a772599
4
- data.tar.gz: e40f06000ded452b21769446530453e8eba7243d
3
+ metadata.gz: 9c322425fe4d630018ee8191f6faa6dac4e28429
4
+ data.tar.gz: e4991fe6c463cdac97e36df4121efcd316f43d55
5
5
  SHA512:
6
- metadata.gz: 0b1ccfc10ca315b1b35d733c767c54b241c1c19996d30d1a0e5ccb6f0a2d88b50c966eaf4f723e704678fe25587d821025846a8b8eaf1888bae6e8a92d9bd814
7
- data.tar.gz: 61a3e79425cad2336247816305eaaf51dec3e7579d4237a0fd46f5027df5d15cc728ae433617ac6f8a6d6e23560fe7e108b75e772cf43e634e2bed79d7379635
6
+ metadata.gz: e7143c847c534b3ffe2146ab7b148a8e8ec154a0e906a4130c4a7d1fb3faec7da57236b7aea94ff882c0579192b082f8a57ad33981bc4f94b8210f57d51251e6
7
+ data.tar.gz: 5e844b023d12fd13779bd052d10507c532c50c98c71dfa577d9ae04b7b778dce242f1173297e718b2d020d4b4a7413de1ea63078e2029772c3112682ef17108c
@@ -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"]
11
- s.email = ["ioiioioloo@gmail.com"]
10
+ s.authors = ["tatat", "takkkun"]
11
+ s.email = ["ioiioioloo@gmail.com", "heartery@gmail.com"]
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."
@@ -1,3 +1,3 @@
1
1
  module ActsAsSplittable
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -56,7 +56,7 @@ module ActsAsSplittable
56
56
 
57
57
  unless value.nil?
58
58
  values = if on_split
59
- on_split.is_a?(Symbol) ? send(on_split, value) : on_split.(value)
59
+ run_callback(on_split, value)
60
60
  elsif value
61
61
  if split
62
62
  value.to_s.split *(split.is_a?(Array) ? split : [split])
@@ -83,16 +83,26 @@ module ActsAsSplittable
83
83
  partials = partials.map{|partial| send(partial) }
84
84
 
85
85
  unless partials.any?(&:nil?)
86
- send :"#{column}=", on_join.is_a?(Symbol) ? send(on_join, partials) : on_join.(partials)
86
+ send :"#{column}=", run_callback(on_join, partials)
87
87
  end
88
88
  end
89
-
89
+
90
90
  self
91
91
  end
92
92
 
93
93
  def splittable_partials
94
94
  @splittable_partials ||= {}
95
95
  end
96
+
97
+ private
98
+
99
+ def run_callback(callback, *args)
100
+ if callback.is_a?(Proc)
101
+ instance_exec(*args, &callback)
102
+ else
103
+ send(callback, *args)
104
+ end
105
+ end
96
106
  end
97
107
  end
98
108
 
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  [Splittable, SplittableInherited, SplittableInheritedInherited].each do |klass|
4
4
  describe klass do
5
-
5
+
6
6
  before :each do
7
7
  @splittable1 = klass.new(name: "#{klass.name} 1")
8
8
  @splittable1.email_local = 'splittable'
@@ -88,3 +88,48 @@ require 'spec_helper'
88
88
  end
89
89
  end
90
90
  end
91
+
92
+ describe Splittable do
93
+ context 'when was given a proc to callbacks' do
94
+ it 'should call in the record' do
95
+ model_class = Class.new(ActiveRecord::Base) do
96
+ self.table_name = :splittables
97
+
98
+ acts_as_splittable
99
+
100
+ splittable :birthday, {
101
+ partials: [:birthday_year, :birthday_month, :birthday_day],
102
+
103
+ on_split: -> (value) {
104
+ year, month, day = value.chars.each_slice(2).map(&:join).map(&:to_i)
105
+ [year + birthday_base, month, day]
106
+ },
107
+
108
+ on_join: -> (values) {
109
+ year, month, day = values.map(&:to_i)
110
+ '%02d%02d%02d' % [year - birthday_base, month, day]
111
+ }
112
+ }
113
+
114
+ def birthday_base
115
+ birthday_era == 'showa' ? 1925 : birthday_era == 'heisei' ? 1988 : 0
116
+ end
117
+ end
118
+
119
+ model = model_class.create!(
120
+ birthday_era: 'heisei',
121
+ birthday_year: 1989,
122
+ birthday_month: 7,
123
+ birthday_day: 7
124
+ )
125
+
126
+ model.birthday = '010707'
127
+
128
+ model = model_class.find(model.id)
129
+
130
+ model.birthday_year.should == 1989
131
+ model.birthday_month.should == 7
132
+ model.birthday_day.should == 7
133
+ end
134
+ end
135
+ end
data/spec/schema.rb CHANGED
@@ -5,8 +5,10 @@ ActiveRecord::Schema.define version: 0 do
5
5
  t.string "email"
6
6
  t.string "postal_code"
7
7
  t.string "phone_number"
8
+ t.string "birthday_era"
9
+ t.string "birthday"
8
10
  t.datetime "created_at"
9
11
  t.datetime "updated_at"
10
12
  end
11
13
 
12
- end
14
+ end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_splittable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - tatat
8
+ - takkkun
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-06-16 00:00:00.000000000 Z
12
+ date: 2013-06-17 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rails
@@ -75,6 +76,7 @@ dependencies:
75
76
  description: Create virtual attributes.
76
77
  email:
77
78
  - ioiioioloo@gmail.com
79
+ - heartery@gmail.com
78
80
  executables: []
79
81
  extensions: []
80
82
  extra_rdoc_files: []