active_hash 2.1.0 → 2.2.0

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
  SHA256:
3
- metadata.gz: 5a92e60195ef8ad5fbd50981978f78755d5f670ba04dd3b97604454e9c466e14
4
- data.tar.gz: a00b0870924b1cc46e5f0fc63a75847bcd54532f4f6e8c9c0367c7058ec079e1
3
+ metadata.gz: 88911736a7baf25f50316f0f8cff64da3d75b0cf4e5ec4da716acae358390308
4
+ data.tar.gz: aec89dbc9f68f10e86b2e2b7ea4bdecfd42043a917baf7e3aa7a0c85f15969e3
5
5
  SHA512:
6
- metadata.gz: 1419297eb2d3427cad33b04bf4a29c646b9852e89008df56269128635fabe55a16742a9c9f4a0de4cd8fd398bddfb1dcd36730f3fd63283ba92764c1f83260e4
7
- data.tar.gz: a89ca9f078a9258d5c6f079505abffe438250444e2afa45f2f9d0934afa4ed29cbb06de211a6e392905887c5770724f873541a8f84e58253385dc93d00a35ef1
6
+ metadata.gz: 80a28ac67124190ca5dcff815a0e0dc923f3cc5a8401965de64ae53e0feb8901fd3bc6bfcedd4deb8c86b83ba4b16190b2e098eee0a2ee3e5db6b07bd1ad0815
7
+ data.tar.gz: 25de0710d0f570a6486153f46a987d8f95187b71f95338b5e54ce0a1e529df4cd6d91e7097c3ed45f80072f38647a3821fbcd7354e06b9d0c415e48b7fcb7487
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 2018-11-22 (v2.2.0)
2
+ - Support pluck method [#164](https://github.com/zilkey/active_hash/pull/164) Thanks, @ihatov08
3
+ - Support where.not method [#167](https://github.com/zilkey/active_hash/pull/167) Thanks, @DialBird
4
+
1
5
  2018-04-05 (v2.1.0)
2
6
  - Allow to use ERB (embedded ruby) in yml files [#160](https://github.com/zilkey/active_hash/pull/160) Thanks, @UgoMare
3
7
  - Add `ActiveHash::Base.polymorphic_name` [#162](https://github.com/zilkey/active_hash/pull/162)
data/README.md CHANGED
@@ -207,7 +207,7 @@ Country#save!
207
207
  Country.create
208
208
  Country.create!
209
209
  ```
210
- As such, ActiveHash::Base and its descendants should work with Fixjour or FactoryGirl, so you can treat ActiveHash records the same way you would any other ActiveRecord model in tests.
210
+ As such, ActiveHash::Base and its descendants should work with Fixjour or FactoryBot, so you can treat ActiveHash records the same way you would any other ActiveRecord model in tests.
211
211
 
212
212
  To clear all records from the in-memory array, call delete_all:
213
213
  ```ruby
@@ -3,11 +3,7 @@ module ActiveFile
3
3
  class Base < ActiveHash::Base
4
4
  extend ActiveFile::MultipleFiles
5
5
 
6
- if respond_to?(:class_attribute)
7
- class_attribute :filename, :root_path, :data_loaded, instance_reader: false, instance_writer: false
8
- else
9
- class_inheritable_accessor :filename, :root_path, :data_loaded
10
- end
6
+ class_attribute :filename, :root_path, :data_loaded, instance_reader: false, instance_writer: false
11
7
 
12
8
  class << self
13
9
 
@@ -5,11 +5,7 @@ module ActiveFile
5
5
  end
6
6
 
7
7
  def use_multiple_files
8
- if respond_to?(:class_attribute)
9
- class_attribute :filenames, instance_reader: false, instance_writer: false
10
- else
11
- class_inheritable_accessor :filenames
12
- end
8
+ class_attribute :filenames, instance_reader: false, instance_writer: false
13
9
 
14
10
  def self.set_filenames(*filenames)
15
11
  self.filenames = filenames
@@ -14,10 +14,46 @@ module ActiveHash
14
14
 
15
15
  class Base
16
16
 
17
- if respond_to?(:class_attribute)
18
- class_attribute :_data, :dirty, :default_attributes
19
- else
20
- class_inheritable_accessor :_data, :dirty, :default_attributes
17
+ class_attribute :_data, :dirty, :default_attributes
18
+
19
+ class WhereChain
20
+ def initialize(scope)
21
+ @scope = scope
22
+ @records = @scope.all
23
+ end
24
+
25
+ def not(options)
26
+ return @records if options.blank?
27
+
28
+ # use index if searching by id
29
+ if options.key?(:id) || options.key?("id")
30
+ ids = @scope.pluck(:id) - Array.wrap(options.delete(:id) || options.delete("id"))
31
+ candidates = ids.map { |id| @scope.find_by_id(id) }.compact
32
+ end
33
+ return candidates if options.blank?
34
+
35
+ (candidates || @records || []).reject do |record|
36
+ match_options?(record, options)
37
+ end
38
+ end
39
+
40
+ def match_options?(record, options)
41
+ options.all? do |col, match|
42
+ if match.kind_of?(Array)
43
+ match.any? { |v| normalize(v) == normalize(record[col]) }
44
+ else
45
+ normalize(record[col]) == normalize(match)
46
+ end
47
+ end
48
+ end
49
+
50
+ private :match_options?
51
+
52
+ def normalize(v)
53
+ v.respond_to?(:to_sym) ? v.to_sym : v
54
+ end
55
+
56
+ private :normalize
21
57
  end
22
58
 
23
59
  if Object.const_defined?(:ActiveModel)
@@ -154,8 +190,12 @@ module ActiveHash
154
190
  end
155
191
  end
156
192
 
157
- def where(options)
158
- return @records if options.blank?
193
+ def where(options = :chain)
194
+ if options == :chain
195
+ return WhereChain.new(self)
196
+ elsif options.blank?
197
+ return @records
198
+ end
159
199
 
160
200
  # use index if searching by id
161
201
  if options.key?(:id) || options.key?("id")
@@ -199,6 +239,10 @@ module ActiveHash
199
239
  all.length
200
240
  end
201
241
 
242
+ def pluck(*column_names)
243
+ column_names.map { |column_name| all.map(&column_name.to_sym) }.inject(&:zip)
244
+ end
245
+
202
246
  def transaction
203
247
  yield
204
248
  rescue LocalJumpError => err
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "2.1.0"
3
+ VERSION = "2.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dean
@@ -29,7 +29,7 @@ authors:
29
29
  autorequire:
30
30
  bindir: bin
31
31
  cert_chain: []
32
- date: 2018-04-05 00:00:00.000000000 Z
32
+ date: 2018-11-22 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: activesupport
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 2.7.3
90
+ rubygems_version: 2.7.6
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: An ActiveRecord-like model that uses a hash or file as a datasource