static_collection 0.3.2 → 0.4.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: 98017f3d39c6af6afaaaeb5ddd30dc586c09c9a5a1ad3ccb5c894e123959947c
4
- data.tar.gz: df0fbf27a32cde5538df55003b59f9320926ffbe0e323e050e52ce3f09b9ba4c
3
+ metadata.gz: 4bdcf2327cbd67bd3a580c9be6a96c96d104e04462970d8abf97139fd05f252d
4
+ data.tar.gz: b8a83baba3ba4eebc5019ddf6825635f0a95a77c793b02030021fe32ec0e3676
5
5
  SHA512:
6
- metadata.gz: 531a720c36212eae51f9319ae20802d868c68ebc5a9f4345017cf04f2f8eb98b2496f552301793efdb51a5500512b2dac3064f81ce6034673722497715ae2bc0
7
- data.tar.gz: 6b389bb60c43c02215583508ddf1e08037b8971674a996bfbe9b8c1f3addfd45d536aa05b79a1a3eefcadc744a8ececa9a2864333a826b03eb3b9052115b868e
6
+ metadata.gz: 93f9f93d7b45466de470ccafc0b6d30272fbb9bada41598fb7e441b33ac3eb037bbeb2a81367de5f0601e947511247f0290ef2b813d75c75c0c2e5a51bf1b7bf
7
+ data.tar.gz: 07aaaee52c0854e04ce98ca5db1ab7dbbcf8aa81232590262343cd5a3960e85cb6190815f3a64ede2a58a8320278b8f7c5426c7a2a2490f97944ba481db160a0
@@ -11,7 +11,7 @@ jobs:
11
11
  runs-on: ubuntu-22.04
12
12
  strategy:
13
13
  matrix:
14
- ruby-version: [3.0.6, 3.1.4, 3.2.2]
14
+ ruby-version: [3.1.4, 3.2.3, 3.3.0]
15
15
  steps:
16
16
  - uses: actions/checkout@v3
17
17
  - name: Set up Ruby ${{ matrix.ruby-version }}
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## 0.4.0
9
+ ### Breaking Change
10
+ - All Static Collections will be now consist of frozen objects
11
+ - Reduce initialization of objects
data/Gemfile.lock CHANGED
@@ -1,79 +1,99 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- static_collection (0.3.2)
4
+ static_collection (0.4.0)
5
5
  activesupport (>= 4)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (7.0.4.3)
10
+ activesupport (7.1.3)
11
+ base64
12
+ bigdecimal
11
13
  concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ connection_pool (>= 2.2.5)
15
+ drb
12
16
  i18n (>= 1.6, < 2)
13
17
  minitest (>= 5.1)
18
+ mutex_m
14
19
  tzinfo (~> 2.0)
15
20
  ast (2.4.2)
16
- concurrent-ruby (1.2.2)
21
+ base64 (0.2.0)
22
+ bigdecimal (3.1.6)
23
+ concurrent-ruby (1.2.3)
24
+ connection_pool (2.4.1)
17
25
  diff-lcs (1.5.0)
18
- i18n (1.12.0)
26
+ drb (2.2.0)
27
+ ruby2_keywords
28
+ i18n (1.14.1)
19
29
  concurrent-ruby (~> 1.0)
20
- json (2.6.3)
21
- minitest (5.18.0)
22
- parallel (1.22.1)
23
- parser (3.2.0.0)
30
+ json (2.7.1)
31
+ language_server-protocol (3.17.0.3)
32
+ minitest (5.21.2)
33
+ mutex_m (0.2.0)
34
+ parallel (1.24.0)
35
+ parser (3.3.0.4)
24
36
  ast (~> 2.4.1)
25
- rack (3.0.7)
37
+ racc
38
+ racc (1.7.3)
39
+ rack (3.0.8)
26
40
  rainbow (3.1.1)
27
- rake (13.0.6)
28
- regexp_parser (2.6.2)
29
- rexml (3.2.5)
41
+ rake (13.1.0)
42
+ regexp_parser (2.9.0)
43
+ rexml (3.2.6)
30
44
  rspec (3.12.0)
31
45
  rspec-core (~> 3.12.0)
32
46
  rspec-expectations (~> 3.12.0)
33
47
  rspec-mocks (~> 3.12.0)
34
- rspec-core (3.12.0)
48
+ rspec-core (3.12.2)
35
49
  rspec-support (~> 3.12.0)
36
- rspec-expectations (3.12.2)
50
+ rspec-expectations (3.12.3)
37
51
  diff-lcs (>= 1.2.0, < 2.0)
38
52
  rspec-support (~> 3.12.0)
39
53
  rspec-its (1.3.0)
40
54
  rspec-core (>= 3.0.0)
41
55
  rspec-expectations (>= 3.0.0)
42
- rspec-mocks (3.12.3)
56
+ rspec-mocks (3.12.6)
43
57
  diff-lcs (>= 1.2.0, < 2.0)
44
58
  rspec-support (~> 3.12.0)
45
- rspec-support (3.12.0)
46
- rubocop (1.44.1)
59
+ rspec-support (3.12.1)
60
+ rubocop (1.60.1)
47
61
  json (~> 2.3)
62
+ language_server-protocol (>= 3.17.0)
48
63
  parallel (~> 1.10)
49
- parser (>= 3.2.0.0)
64
+ parser (>= 3.3.0.2)
50
65
  rainbow (>= 2.2.2, < 4.0)
51
66
  regexp_parser (>= 1.8, < 3.0)
52
67
  rexml (>= 3.2.5, < 4.0)
53
- rubocop-ast (>= 1.24.1, < 2.0)
68
+ rubocop-ast (>= 1.30.0, < 2.0)
54
69
  ruby-progressbar (~> 1.7)
55
70
  unicode-display_width (>= 2.4.0, < 3.0)
56
- rubocop-ast (1.24.1)
57
- parser (>= 3.1.1.0)
58
- rubocop-capybara (2.17.0)
71
+ rubocop-ast (1.30.0)
72
+ parser (>= 3.2.1.0)
73
+ rubocop-capybara (2.20.0)
59
74
  rubocop (~> 1.41)
60
- rubocop-performance (1.15.2)
61
- rubocop (>= 1.7.0, < 2.0)
62
- rubocop-ast (>= 0.4.0)
63
- rubocop-rails (2.17.4)
75
+ rubocop-factory_bot (2.25.1)
76
+ rubocop (~> 1.41)
77
+ rubocop-performance (1.20.2)
78
+ rubocop (>= 1.48.1, < 2.0)
79
+ rubocop-ast (>= 1.30.0, < 2.0)
80
+ rubocop-rails (2.23.1)
64
81
  activesupport (>= 4.2.0)
65
82
  rack (>= 1.1)
66
83
  rubocop (>= 1.33.0, < 2.0)
67
- rubocop-rspec (2.18.1)
68
- rubocop (~> 1.33)
84
+ rubocop-ast (>= 1.30.0, < 2.0)
85
+ rubocop-rspec (2.26.1)
86
+ rubocop (~> 1.40)
69
87
  rubocop-capybara (~> 2.17)
70
- rubocop-vendor (0.8.10)
71
- rubocop (>= 0.53.0)
72
- ruby-progressbar (1.11.0)
88
+ rubocop-factory_bot (~> 2.22)
89
+ rubocop-vendor (0.13.0)
90
+ rubocop
91
+ ruby-progressbar (1.13.0)
92
+ ruby2_keywords (0.0.5)
73
93
  tzinfo (2.0.6)
74
94
  concurrent-ruby (~> 1.0)
75
- unicode-display_width (2.4.2)
76
- ws-style (6.14.4)
95
+ unicode-display_width (2.5.0)
96
+ ws-style (6.14.7)
77
97
  rubocop (>= 1.36)
78
98
  rubocop-performance (>= 1.10.2)
79
99
  rubocop-rails (>= 2.9.1)
data/Rakefile CHANGED
@@ -1,6 +1,14 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'bundler/gem_tasks'
8
+ require 'rspec/core/rake_task'
9
+ require 'rubocop/rake_task'
3
10
 
4
11
  RSpec::Core::RakeTask.new(:spec)
12
+ RuboCop::RakeTask.new
5
13
 
6
- task default: :spec
14
+ task default: %i[spec rubocop]
@@ -1,3 +1,3 @@
1
1
  module StaticCollection
2
- VERSION = "0.3.2".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
@@ -3,23 +3,27 @@ require "static_collection/version"
3
3
 
4
4
  module StaticCollection
5
5
  class Base
6
+ # rubocop:disable Metrics/MethodLength
6
7
  def self.set_source(source, defaults: {})
7
8
  raise "Source must be an array" unless source.is_a?(Array)
8
9
 
9
- instance_variable_set(:@defaults, defaults.stringify_keys)
10
- instance_variable_set(:@source, source.map(&:stringify_keys))
11
- raise "Source must have at least one value" unless count > 0
10
+ defaults = defaults.stringify_keys
11
+ raise "Source must have at least one value" if source.count <= 0
12
12
 
13
- all.first.attributes.each do |attribute_name, attribute_value|
13
+ values = source.map(&:stringify_keys).map { |s| new(deep_freeze(defaults.merge(s))) }
14
+ instance_variable_set(:@all, values)
15
+
16
+ all_attribute_names = values.flat_map { |v| v.attributes.keys }.uniq
17
+ all_attribute_names.each do |attribute_name|
14
18
  # Class methods
15
- define_singleton_method("find_by_#{attribute_name}") do |value|
19
+ define_singleton_method(:"find_by_#{attribute_name}") do |value|
16
20
  ActiveSupport::Deprecation.warn(
17
21
  "find_by_#{attribute_name} is deprecated for StaticCollection, " \
18
22
  "use find_by(#{attribute_name}: [value])",
19
23
  )
20
24
  all.find { |instance| instance.send(attribute_name) == value }
21
25
  end
22
- define_singleton_method("find_all_by_#{attribute_name}") do |value|
26
+ define_singleton_method(:"find_all_by_#{attribute_name}") do |value|
23
27
  ActiveSupport::Deprecation.warn(
24
28
  "find_all_by_#{attribute_name} is deprecated for StaticCollection, " \
25
29
  "use where(#{attribute_name}: [value])",
@@ -29,13 +33,33 @@ module StaticCollection
29
33
 
30
34
  # Instance methods
31
35
  send(:define_method, attribute_name) { attributes[attribute_name] }
32
- next unless attribute_value.is_a?(TrueClass) || attribute_value.is_a?(FalseClass)
33
-
34
36
  send(:define_method, "#{attribute_name}?") {
35
- attributes[attribute_name]
37
+ attributes[attribute_name] if attributes.key?(attribute_name)
36
38
  }
37
39
  end
38
40
  end
41
+ # rubocop:enable Metrics/MethodLength
42
+
43
+ class << self
44
+ private
45
+
46
+ def deep_freeze(obj)
47
+ case obj
48
+ when Hash
49
+ obj.each_with_object({}) do |(key, value), acc|
50
+ acc[deep_freeze(key)] = deep_freeze(value)
51
+ end.freeze
52
+ when Array
53
+ obj.map do |value|
54
+ deep_freeze(value)
55
+ end.freeze
56
+ when Symbol, Integer, NilClass
57
+ obj
58
+ else
59
+ obj.freeze
60
+ end
61
+ end
62
+ end
39
63
 
40
64
  def self.find_by(opts)
41
65
  all.find { |instance| opts.all? { |k, v| instance.send(k) == v } }
@@ -46,8 +70,7 @@ module StaticCollection
46
70
  end
47
71
 
48
72
  def self.all
49
- defaults = instance_variable_get(:@defaults)
50
- instance_variable_get(:@source).map { |s| new(defaults.merge(s)) }
73
+ instance_variable_get(:@all)
51
74
  end
52
75
 
53
76
  def self.count
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static_collection
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Graham
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-18 00:00:00.000000000 Z
11
+ date: 2024-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -123,6 +123,7 @@ files:
123
123
  - ".rubocop.yml"
124
124
  - ".rubocop_todo.yml"
125
125
  - ".ruby-version"
126
+ - CHANGELOG.md
126
127
  - Gemfile
127
128
  - Gemfile.lock
128
129
  - LICENSE.txt