duck_record 0.0.12 → 0.0.13

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: 94d686747e67e2fd89839e277156b05e45bae20a
4
- data.tar.gz: aedff996d41cc23adf9686041eb0817e2db29f65
3
+ metadata.gz: a40d7265442d33cb9b0523abc991f2425c3d87ff
4
+ data.tar.gz: 266ba229695849261a975fd2924cc9837a095671
5
5
  SHA512:
6
- metadata.gz: 59898083c6f86b92da56fb15b990c1df664647319def15335f56b414a1fe41133f2fde382eec4738f2eaa29e32cfd4f2f5cf6207559dc1f9be14e14a25abbf31
7
- data.tar.gz: b1dd8db43eb6f791bc4c0671202ec0e36a9b59b3a28e5db43b45a1b75a5a53c45a7dcba324aa6697dcfc3160f7e4bcf2ae3985b3fa04c65cd5ecbd3a61a5edb1
6
+ metadata.gz: 363119060b6e6191f6fe401a121c6548b2ca3ae28f5875a95db30aaffbb29d952c8baa0a6a37f3deb4085a24389af87c4b698eb950bcf9b9a46c1761a1a82721
7
+ data.tar.gz: bc7e1560401aa6afebc76066457085b1b0898e04b69feff43cf7d4ae72693bfe164022c604575752463941360b4ca8bed5ff868d90ad4e7bf0764bc92570c1bd
@@ -0,0 +1,46 @@
1
+ class ArrayWithoutBlank < Array
2
+ def self.new(*several_variants)
3
+ arr = super
4
+ arr.reject!(&:blank?)
5
+ arr
6
+ end
7
+
8
+ def initialize_copy(other_ary)
9
+ super other_ary.reject(&:blank?)
10
+ end
11
+
12
+ def replace(other_ary)
13
+ super other_ary.reject(&:blank?)
14
+ end
15
+
16
+ def push(obj, *smth)
17
+ return self if obj.blank?
18
+ super
19
+ end
20
+
21
+ def insert(*args)
22
+ super *args.reject(&:blank?)
23
+ end
24
+
25
+ def []=(index, obj)
26
+ return self[index] if obj.blank?
27
+ super
28
+ end
29
+
30
+ def concat(other_ary)
31
+ super other_ary.reject(&:blank?)
32
+ end
33
+
34
+ def +(other_ary)
35
+ super other_ary.reject(&:blank?)
36
+ end
37
+
38
+ def <<(obj)
39
+ return self if obj.blank?
40
+ super
41
+ end
42
+
43
+ def to_ary
44
+ Array.new(self)
45
+ end
46
+ end
@@ -305,11 +305,12 @@ module DuckRecord #:nodoc:
305
305
 
306
306
  self.class.reflections.keys.each do |k|
307
307
  records = send(k)
308
- sub_hash = if records.respond_to?(:to_ary)
309
- records.to_ary.map { |a| a.to_h }
310
- else
311
- records.to_h
312
- end
308
+ sub_hash =
309
+ if records.respond_to?(:to_ary)
310
+ records.to_ary.map { |a| a.to_h }
311
+ else
312
+ records.to_h
313
+ end
313
314
 
314
315
  if include_empty || sub_hash.any?
315
316
  hash[k] = sub_hash
@@ -15,5 +15,18 @@ module DuckRecord #:nodoc:
15
15
 
16
16
  super(options)
17
17
  end
18
+
19
+ private
20
+
21
+ def read_attribute_for_serialization(key)
22
+ v = send(key)
23
+ if v.respond_to?(:to_ary)
24
+ v.to_ary
25
+ elsif v.respond_to?(:to_hash)
26
+ v.to_hash
27
+ else
28
+ v
29
+ end
30
+ end
18
31
  end
19
32
  end
@@ -0,0 +1,36 @@
1
+ module DuckRecord
2
+ module Type # :nodoc:
3
+ class ArrayWithoutBlank < ActiveModel::Type::Value # :nodoc:
4
+ include ActiveModel::Type::Helpers::Mutable
5
+
6
+ attr_reader :subtype
7
+ delegate :type, :user_input_in_time_zone, :limit, to: :subtype
8
+
9
+ def initialize(subtype)
10
+ @subtype = subtype
11
+ end
12
+
13
+ def cast(value)
14
+ type_cast_array(value, :cast)
15
+ end
16
+
17
+ def ==(other)
18
+ other.is_a?(Array) && subtype == other.subtype
19
+ end
20
+
21
+ def map(value, &block)
22
+ value.map(&block)
23
+ end
24
+
25
+ private
26
+
27
+ def type_cast_array(value, method)
28
+ if value.is_a?(::Array)
29
+ ::ArrayWithoutBlank.new value.map { |item| type_cast_array(item, method) }
30
+ else
31
+ @subtype.public_send(method, value)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -9,6 +9,7 @@ require "duck_record/type/time"
9
9
  require "duck_record/type/json"
10
10
 
11
11
  require "duck_record/type/array"
12
+ require "duck_record/type/array_without_blank"
12
13
 
13
14
  require "duck_record/type/unsigned_integer"
14
15
  require "duck_record/type/decimal_without_scale"
@@ -67,5 +68,6 @@ module DuckRecord
67
68
  register(:json, Type::JSON, override: false)
68
69
 
69
70
  add_modifier({ array: true }, Type::Array)
71
+ add_modifier({ array_without_blank: true }, Type::ArrayWithoutBlank)
70
72
  end
71
73
  end
@@ -1,3 +1,3 @@
1
1
  module DuckRecord
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
data/lib/duck_record.rb CHANGED
@@ -2,6 +2,8 @@ require "active_support"
2
2
  require "active_support/rails"
3
3
  require "active_model"
4
4
 
5
+ require "core_ext/array_without_blank"
6
+
5
7
  require "duck_record/type"
6
8
  require "duck_record/attribute_set"
7
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duck_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - jasl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-03 00:00:00.000000000 Z
11
+ date: 2017-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -80,6 +80,7 @@ files:
80
80
  - MIT-LICENSE
81
81
  - README.md
82
82
  - Rakefile
83
+ - lib/core_ext/array_without_blank.rb
83
84
  - lib/duck_record.rb
84
85
  - lib/duck_record/associations.rb
85
86
  - lib/duck_record/associations/association.rb
@@ -121,6 +122,7 @@ files:
121
122
  - lib/duck_record/translation.rb
122
123
  - lib/duck_record/type.rb
123
124
  - lib/duck_record/type/array.rb
125
+ - lib/duck_record/type/array_without_blank.rb
124
126
  - lib/duck_record/type/date.rb
125
127
  - lib/duck_record/type/date_time.rb
126
128
  - lib/duck_record/type/decimal_without_scale.rb