rearmed 1.0.0 → 1.0.1

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: 8fa0d5cedc04978a85f2c4a0fb6513c901433e91
4
- data.tar.gz: 1bdae7cb97e455015685cb4892d8efbbf979d7ad
3
+ metadata.gz: 4b89bebeea7547a401cc55438789b46e09e82177
4
+ data.tar.gz: eb9048e4ee97652b778d424422969c3e73e6924e
5
5
  SHA512:
6
- metadata.gz: 2d5e3761d13a41c5fad2891d6c06b012e718466ae2ac2134bb16d8f140f143bb7835913dff9d79f5516fa33887623c2f37c42c45d8f7fa84c1e00236f7d0f152
7
- data.tar.gz: fe28bd2d924cc1af47222b82e69ba4ba1aefd8c154e0486b27ee0aadd51415c8cfb335b025756ed0c9056465f655868ae329860196c3c2b3511dfecc2e47e536
6
+ metadata.gz: 2d5d35e487207fe314f491965e7d9732b675da554dc49657dca0850aba11507aa681d50c3b75c3fde090c20f75edbf763f654363a239087a682c47e700f7a4f3
7
+ data.tar.gz: ad301662615d17735b59308291751a728ffa4fc24c306cb21c676c2b6fa0b9a37650f37f349b08272597bc92615b384ba41f1fb2cc8e64427380892e0c7912f8
@@ -1,8 +1,12 @@
1
1
  CHANGELOG
2
2
  ---------
3
3
 
4
- - **June.02.2016**: 0.9.1
4
+ - **1.0.0 - June 8, 2016**
5
+ - Ready for public use
6
+ - Major improvements to opt-in system
7
+ - Move some methods to Rearmed for use outside of monkey patching
8
+ - **0.9.1 - June 02, 2016**
5
9
  - Add many new methods
6
10
  - Switch to opt-in monkey patching
7
- - **April.30.2016**: 0.9.0
11
+ - **0.9.0 - April 30, 2016**
8
12
  - Gem Initial Release
data/README.md CHANGED
@@ -21,15 +21,19 @@ Run `rails g rearmed:setup` to create a settings files in `config/initializers/r
21
21
  Rearmed.enabled_patches = {
22
22
  rails_4: {
23
23
  or: false,
24
- link_to_confirm: false,
25
- find_relation_each: false,
26
- find_in_relation_batches: false,
24
+ link_to_confirm: false
27
25
  },
28
26
  rails_3: {
29
27
  hash_compact: false,
30
28
  pluck: false,
31
29
  update_columns: false,
32
- all: false,
30
+ all: false
31
+ },
32
+ rails: {
33
+ pluck_to_hash: false,
34
+ pluck_to_struct: false,
35
+ find_relation_each: false,
36
+ find_in_relation_batches: false,
33
37
  },
34
38
  string: {
35
39
  to_bool: false,
@@ -127,6 +131,14 @@ hash.only!(:foo, :bar)
127
131
 
128
132
  ### Rails
129
133
 
134
+ ##### Additional ActiveRecord Methods
135
+ ```ruby
136
+ Post.all.pluck_to_hash(:name, :category, :id)
137
+ Post.all.pluck_to_struct(:name, :category, :id)
138
+ Post.find_in_relation_batches # this returns a relation instead of an array
139
+ Post.find_relation_each # this returns a relation instead of an array
140
+ ```
141
+
130
142
  ##### Rails 3.x Backports
131
143
  ```ruby
132
144
  my_hash.compact
@@ -137,12 +149,10 @@ Post.pluck(:name, :id) # adds multi column pluck support ex. => [['first', 1], [
137
149
  ```
138
150
 
139
151
 
140
- ##### Rails 4.x Backports & Additional Methods
152
+ ##### Rails 4.x Backports
141
153
  ```ruby
142
154
  Post.where(name: 'foo').or.where(content: 'bar')
143
155
  = link_to 'Delete', post_path(post), method: :delete, confirm: "Are you sure you want to delete this post?" #returns rails 3 behaviour of allowing confirm attribute as well as data-confirm
144
- Post.find_in_relation_batches # this returns a relation instead of an array
145
- Post.find_relation_each # this returns a relation instead of an array
146
156
  ```
147
157
 
148
158
  # Credits
@@ -4,11 +4,9 @@ module Rearmed
4
4
  class SetupGenerator < Rails::Generators::Base
5
5
 
6
6
  def setup
7
- contents = <<eos
7
+ create_file "config/initializers/rearmed.rb", <<eos
8
8
  Rearmed.enabled_patches = {
9
9
  rails_4: {
10
- find_relation_each: false,
11
- find_in_relation_batches: false,
12
10
  or: false,
13
11
  link_to_confirm: false
14
12
  },
@@ -18,6 +16,12 @@ Rearmed.enabled_patches = {
18
16
  update_columns: false,
19
17
  all: false
20
18
  },
19
+ rails: {
20
+ pluck_to_hash: false,
21
+ pluck_to_struct: false,
22
+ find_relation_each: false,
23
+ find_in_relation_batches: false,
24
+ },
21
25
  string: {
22
26
  to_bool: false,
23
27
  valid_integer: false,
@@ -28,8 +32,6 @@ Rearmed.enabled_patches = {
28
32
  dig: false
29
33
  },
30
34
  array: {
31
- index_all: false,
32
- find: false,
33
35
  dig: false,
34
36
  delete_first: false
35
37
  },
@@ -48,8 +50,6 @@ Rearmed.enabled_patches = {
48
50
 
49
51
  require 'rearmed/apply_patches'
50
52
  eos
51
-
52
- create_file "config/initializers/rearmed.rb", contents
53
53
  end
54
54
 
55
55
  end
@@ -0,0 +1,107 @@
1
+ enabled = Rearmed.enabled_patches[:rails] == true
2
+
3
+ if defined?(ActiveRecord)
4
+
5
+ if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :pluck_to_hash) == true
6
+ ActiveRecord::Base.class_eval do
7
+ def self.pluck_to_hash(*keys)
8
+ hash_type = keys[-1].is_a?(Hash) ? keys.pop.fetch(:hash_type, HashWithIndifferentAccess) : HashWithIndifferentAccess
9
+ block_given = block_given?
10
+ keys, formatted_keys = format_keys(keys)
11
+ keys_one = keys.size == 1
12
+
13
+ pluck(*keys).map do |row|
14
+ value = hash_type[formatted_keys.zip(keys_one ? [row] : row)]
15
+ block_given ? yield(value) : value
16
+ end
17
+ end
18
+
19
+ def self.pluck_to_struct(*keys)
20
+ struct_type = keys[-1].is_a?(Hash) ? keys.pop.fetch(:struct_type, Struct) : Struct
21
+ block_given = block_given?
22
+ keys, formatted_keys = format_keys(keys)
23
+ keys_one = keys.size == 1
24
+
25
+ struct = struct_type.new(*formatted_keys)
26
+ pluck(*keys).map do |row|
27
+ value = keys_one ? struct.new(*[row]) : struct.new(*row)
28
+ block_given ? yield(value) : value
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def self.format_keys(keys)
35
+ if keys.blank?
36
+ [column_names, column_names]
37
+ else
38
+ [
39
+ keys,
40
+ keys.map do |k|
41
+ case k
42
+ when String
43
+ k.split(/\bas\b/i)[-1].strip.to_sym
44
+ when Symbol
45
+ k
46
+ end
47
+ end
48
+ ]
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ ActiveRecord::Batches.module_eval do
55
+ if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :find_in_relation_batches) == true
56
+ def find_in_relation_batches(options = {})
57
+ options.assert_valid_keys(:start, :batch_size)
58
+
59
+ relation = self
60
+ start = options[:start]
61
+ batch_size = options[:batch_size] || 1000
62
+
63
+ unless block_given?
64
+ return to_enum(:find_in_relation_batches, options) do
65
+ total = start ? where(table[primary_key].gteq(start)).size : size
66
+ (total - 1).div(batch_size) + 1
67
+ end
68
+ end
69
+
70
+ if logger && (arel.orders.present? || arel.taken.present?)
71
+ logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size")
72
+ end
73
+
74
+ relation = relation.reorder(batch_order).limit(batch_size)
75
+ #records = start ? relation.where(table[primary_key].gteq(start)).to_a : relation.to_a
76
+ records = start ? relation.where(table[primary_key].gteq(start)) : relation
77
+
78
+ while records.any?
79
+ records_size = records.size
80
+ primary_key_offset = records.last.id
81
+ raise "Primary key not included in the custom select clause" unless primary_key_offset
82
+
83
+ yield records
84
+
85
+ break if records_size < batch_size
86
+
87
+ records = relation.where(table[primary_key].gt(primary_key_offset))#.to_a
88
+ end
89
+ end
90
+ end
91
+
92
+ if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails, :find_relation_each) == true
93
+ def find_relation_each(options = {})
94
+ if block_given?
95
+ find_in_relation_batches(options) do |records|
96
+ records.each { |record| yield record }
97
+ end
98
+ else
99
+ enum_for :find_relation_each, options do
100
+ options[:start] ? where(table[primary_key].gteq(options[:start])).size : size
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ end
@@ -1,7 +1,7 @@
1
- rails_3_enabled = Rearmed.enabled_patches[:rails_3] == true
1
+ enabled = Rearmed.enabled_patches[:rails_3] == true
2
2
 
3
3
  if defined?(ActiveSupport) && ActiveSupport::VERSION::MAJOR < 4
4
- if rails_3_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails_3, :hash_compact) == true
4
+ if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails_3, :hash_compact) == true
5
5
  Hash.class_eval do
6
6
  def compact
7
7
  self.select{|_, value| !value.nil?}
@@ -16,7 +16,7 @@ end
16
16
 
17
17
  if defined?(ActiveRecord) && ActiveRecord::VERSION::MAJOR < 4
18
18
 
19
- if rails_3_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails_3, :all) == true
19
+ if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails_3, :all) == true
20
20
  ActiveRecord::FinderMethods.module_eval do
21
21
  def all(*args)
22
22
  args.any? ? apply_finder_options(args.first) : self
@@ -24,7 +24,7 @@ if defined?(ActiveRecord) && ActiveRecord::VERSION::MAJOR < 4
24
24
  end
25
25
  end
26
26
 
27
- if rails_3_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails_3, :update_columns) == true
27
+ if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails_3, :update_columns) == true
28
28
  ActiveRecord::Persistence::ClassMethods.module_eval do
29
29
  def update_columns(attributes)
30
30
  raise ActiveRecordError, "cannot update a new record" if new_record?
@@ -45,7 +45,7 @@ if defined?(ActiveRecord) && ActiveRecord::VERSION::MAJOR < 4
45
45
  end
46
46
  end
47
47
 
48
- if rails_3_enabled || Rearmed.dig(Rearmed.enabled_patches, :rails_3, :pluck) == true
48
+ if enabled || Rearmed.dig(Rearmed.enabled_patches, :rails_3, :pluck) == true
49
49
  ActiveRecord::Relation.class_eval do
50
50
  def pluck(*args)
51
51
  args.map! do |column_name|
@@ -1,3 +1,3 @@
1
1
  module Rearmed
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rearmed
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Ganger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-08 00:00:00.000000000 Z
11
+ date: 2016-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -71,12 +71,13 @@ files:
71
71
  - lib/rearmed/monkey_patches/enumerable.rb
72
72
  - lib/rearmed/monkey_patches/hash.rb
73
73
  - lib/rearmed/monkey_patches/object.rb
74
+ - lib/rearmed/monkey_patches/rails.rb
74
75
  - lib/rearmed/monkey_patches/rails_3.rb
75
76
  - lib/rearmed/monkey_patches/rails_4.rb
76
77
  - lib/rearmed/monkey_patches/string.rb
77
78
  - lib/rearmed/version.rb
78
79
  - test/tc_rearmed.rb
79
- homepage: https://github.com/westonganger/rearmed_rb
80
+ homepage: https://github.com/westonganger/rearmed-rb
80
81
  licenses: []
81
82
  metadata: {}
82
83
  post_install_message: