activerecord-bixformer 0.4.3 → 0.4.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: 73a43f010ea22167ff68efdc1d5f561d3715956c
4
- data.tar.gz: e75ab454c783669f0fd9b122960d368f0b31880b
3
+ metadata.gz: 7fb89bcee1a68dc2fa2dc3b9be84e4d5720ff8db
4
+ data.tar.gz: 397f458b9bb776d27f195b0a702905b1cb0506d6
5
5
  SHA512:
6
- metadata.gz: 28bba0a9adcef771c75f9977c10dcceb0f696d346aa67935842b4fea7600b619d919dfc2c16eef55a40845618bb3f5249dcc3d0753d34a6b58b84a50a2db9371
7
- data.tar.gz: 9cbe63b03d90559515b32e5cc054249181f5b6c87d5e3895c726641af886a16664529d3153197037f50e47358cf7871a5b4f8779a0371b0ea9be2d1ab925b71c
6
+ metadata.gz: 96adb2970166144d0b9990a6d87e86a644f323f1f45450addb342a98c16631bc84091cd82bfb11a9b044e8a898d30384dd8cc0d3830ada53e1d1e61ecdb106d3
7
+ data.tar.gz: e629bb57d0c732495c8ecc304fa2a34f4c658918665dfbba4074b2566cd317190e7d57f36d4d67a0de3b167bec0b7ac85c502a962e6eb5c7cea0586b034985ff
@@ -5,16 +5,16 @@ module ActiveRecord
5
5
  def initialize(model, attribute_name, options)
6
6
  super
7
7
 
8
- unless @options[:by]
9
- raise ArgumentError.new 'Not configured required options : by'
8
+ unless @options[:formatter]
9
+ raise ArgumentError.new 'Not configured required options : formatter'
10
10
  end
11
11
 
12
- @options[:find_by] ||= if @options[:by].is_a?(::String) || @options[:by].is_a?(::Symbol)
13
- @options[:by]
14
- end
12
+ @options[:parser] ||= if @options[:formatter].is_a?(::String) || @options[:formatter].is_a?(::Symbol)
13
+ -> (v) { { @options[:formatter] => v } }
14
+ end
15
15
 
16
- unless @options[:find_by]
17
- raise ArgumentError.new 'Not configured required options : find_by'
16
+ unless @options[:parser]
17
+ raise ArgumentError.new 'Not configured required options : parser'
18
18
  end
19
19
  end
20
20
 
@@ -27,7 +27,7 @@ module ActiveRecord
27
27
 
28
28
  return nil unless foreign_record
29
29
 
30
- formatter = @options[:by]
30
+ formatter = @options[:formatter]
31
31
 
32
32
  if formatter.is_a?(::Proc)
33
33
  formatter.call(foreign_record)
@@ -39,31 +39,33 @@ module ActiveRecord
39
39
  def import(value)
40
40
  return nil unless value.present?
41
41
 
42
- find_by = @options[:find_by]
42
+ parser = @options[:parser]
43
+ find_by = @options[:find_by] || :find_by
43
44
  scope = @options[:scope] || :all
44
- finder = @options[:finder] || :find_by
45
45
  creator = @options[:creator] || :save
46
46
 
47
- condition = if find_by.is_a?(::Proc)
48
- find_by.call(value)
47
+ condition = if parser.is_a?(::Proc)
48
+ parser.call(value)
49
49
  else
50
- { find_by => value }
50
+ foreign_constant.__send__(parser, value)
51
51
  end
52
52
 
53
53
  return nil unless condition
54
54
 
55
- foreign_record = if scope.is_a?(::Proc)
56
- scope.call.__send__(finder, condition)
55
+ scoped_relation = if scope.is_a?(::Proc)
56
+ scope.call
57
+ else
58
+ foreign_constant.__send__(scope)
59
+ end
60
+
61
+ foreign_record = if find_by.is_a?(::Proc)
62
+ find_by.call(scoped_relation, condition)
57
63
  else
58
- foreign_constant.__send__(scope).__send__(finder, condition)
64
+ scoped_relation.__send__(find_by, condition)
59
65
  end
60
66
 
61
67
  if ! foreign_record && @options[:create]
62
- foreign_record = if scope.is_a?(::Proc)
63
- scope.call.build(condition)
64
- else
65
- foreign_constant.__send__(scope).build(condition)
66
- end
68
+ foreign_record = scoped_relation.build(condition)
67
69
 
68
70
  if creator.is_a?(::Proc)
69
71
  creator.call(foreign_record)
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
- VERSION = "0.4.3"
3
+ VERSION = "0.4.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-bixformer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroaki Otsu