active_hash 3.3.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f03cf8270c4fd278e024200679342d2063170eb886caacc29eec8b1577ff5d3
4
- data.tar.gz: d1b0cf6ed32af1f82471e22a593d5929be18f47c729e514376485a9a34a8a74d
3
+ metadata.gz: dbf4b348b5da82f53662271a13a45c1994da941db9c825c68eaa30ad38a45ed1
4
+ data.tar.gz: 9cad29ee714bd667b973423771c6a6df988c0c38665a6bd12583980bf41b57da
5
5
  SHA512:
6
- metadata.gz: 33f26034c33d2e1b5533007a10cd63672478e0c421c3e0e1819392bfae33c6815af58ec8605492790ec3bbbbdfe642839647b296902008c0c92f525bed311d06
7
- data.tar.gz: fb495624034f265a1503f5f493fff74097653f3b00a703b1f9406d397a70eb744a6322e0758bfe9e4b6f7bd4a0ea86b2e5bcadb55adaf2fb1be87525330271dd
6
+ metadata.gz: 2487660659ae746870005b8c6cda566ace4aa7119afe15a4b441a5396708970c8d4e5a904592337fadad9acedfe10976434fdf70829633ed7f297edaffa7872d
7
+ data.tar.gz: ecff618c93428de090964526091d43a6ebe17abb4b7822968c1812f326e3a7a08f06092fd0d658713eb2d7db909be5316897ef42adc8cf6f22f73dedf93b4999
data/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # active_hash Changelog
2
2
 
3
+ ## Version [4.0.0] - <sub><sup>2025-07-30</sup></sub>
4
+
5
+ ### Added
6
+
7
+ - Add i18n support [#230](https://github.com/active-hash/active_hash/pull/230) @ryu-sato @Yuki-Inoue
8
+ - Add `column_names` method [#311](https://github.com/active-hash/active_hash/pull/311) @hatsu38
9
+ - Add block support to `count` [#317](https://github.com/active-hash/active_hash/pull/317) @ashleyHutton
10
+ - Support ruby 3.4 [#328](https://github.com/active-hash/active_hash/pull/328) @flavorjones
11
+ - Add `:alias` to `has_many :through` [#329](https://github.com/active-hash/active_hash/pull/329) @alexgriff
12
+ - Add Active Record 8.0 [#324](https://github.com/active-hash/active_hash/pull/324) @flavorjones
13
+
14
+ ### Fixed
15
+
16
+ - Fix Do not suppress load errors[#309](https://github.com/active-hash/active_hash/pull/309) @andreynering
17
+ - Ensure `field_names` are all strings [#312](https://github.com/active-hash/active_hash/pull/312) @flavorjones
18
+ - Hide private `add_default_value` [#314](https://github.com/active-hash/active_hash/pull/314) @kbrock
19
+ - Fix `exists?(nil)` [#320](https://github.com/active-hash/active_hash/pull/320) @y-yagi
20
+ - Enance Enum support [#321](https://github.com/active-hash/active_hash/pull/321) @hatsu38
21
+ - Updated docs [#326](https://github.com/active-hash/active_hash/pull/326) @y-yagi
22
+
23
+ ### Removed
24
+
25
+ - Drop Active Record < 6.1. Ruby < 3.0 [#324](https://github.com/active-hash/active_hash/pull/324) @flavorjones
26
+
3
27
  ## Version [3.3.1] - <sub><sup>2024-05-03</sup></sub>
4
28
 
5
29
  ### Fixed
@@ -329,7 +353,9 @@
329
353
  - Setting data to nil correctly causes .all to return an empty array
330
354
  - Added reload(force) method, so that you can force a reload from files in ActiveFile, useful for tests
331
355
 
332
- [HEAD]: https://github.com/active-hash/active_hash/compare/v3.3.0...HEAD
356
+ [HEAD]: https://github.com/active-hash/active_hash/compare/v3.4.0...HEAD
357
+ [3.3.2]: https://github.com/active-hash/active_hash/compare/v3.3.1...v3.4.0
358
+ [3.3.1]: https://github.com/active-hash/active_hash/compare/v3.3.0...v3.3.1
333
359
  [3.3.0]: https://github.com/active-hash/active_hash/compare/v3.2.1...v3.3.0
334
360
  [3.2.1]: https://github.com/active-hash/active_hash/compare/v3.2.0...v3.2.1
335
361
  [3.2.0]: https://github.com/active-hash/active_hash/compare/v3.1.1...v3.2.0
data/README.md CHANGED
@@ -34,7 +34,11 @@ Other:
34
34
  gem install active_hash
35
35
  ```
36
36
 
37
- **Currently version 2.x doesn't support Ruby < 2.4 and Rails < 5**. If you use these versions, please use 1.x.
37
+ Requirements:
38
+
39
+ - v1.x: Ruby >= 1.9.3, Rails >= 2.2.2
40
+ - v2.x, v3.x: Ruby >= 2.4, Rails >= 5
41
+ - upcoming: Ruby >= 3.0, Rails >= 6.1
38
42
 
39
43
  ```ruby
40
44
  gem 'active_hash', '~> 1.5.3'
@@ -558,7 +562,7 @@ Call `Model.reload(true)` to force reload the data from disk.
558
562
  In Rails, you can use this snippet. Please just note it resets the state every request, which may not always be desired.
559
563
 
560
564
  ```ruby
561
- before_filter do
565
+ before_action do
562
566
  [Model1, Model2, Model3].each { |m| m.reload(true) }
563
567
  end
564
568
  ```
@@ -606,6 +610,20 @@ Constants are formed by first stripping all non-word characters and then upcasin
606
610
 
607
611
  The field specified as the _enum_accessor_ must contain unique data values.
608
612
 
613
+ ## I18n
614
+
615
+ ActiveHash supports i18n as ActiveModel.
616
+ Put following code in one of your locale file (e.g. `config/locales/LANGUAGE_CODE.yml`)
617
+
618
+ ```yaml
619
+ # for example, inside config/locales/ja.yml
620
+ ja:
621
+ activemodel:
622
+ models:
623
+ # `Country.model_name.human` will evaluates to "国"
624
+ country: "国"
625
+ ```
626
+
609
627
  ## Contributing
610
628
 
611
629
  If you'd like to become an ActiveHash contributor, the easiest way it to fork this repo, make your changes, run the specs and submit a pull request once they pass.
@@ -619,10 +637,10 @@ If your changes seem reasonable and the specs pass I'll give you commit rights t
619
637
 
620
638
  ## Releasing a new version
621
639
 
622
- To make users' lives easier, please maintain support for:
640
+ To make users' lives easier, CI will exercise tests for:
623
641
 
624
- * Ruby 2.4
625
- * ActiveRecord/ActiveSupport from 5.0 through edge
642
+ * Ruby 3.0 through current
643
+ * ActiveRecord/ActiveSupport from 6.1 through edge
626
644
 
627
645
  Once appraisal passes in all supported rubies, follow these steps to release a new version of active_hash:
628
646
 
data/active_hash.gemspec CHANGED
@@ -51,7 +51,7 @@ Gem::Specification.new do |s|
51
51
  Dir.glob("lib/**/*")
52
52
  ].flatten
53
53
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
54
- s.add_runtime_dependency('activesupport', '>= 5.0.0')
54
+ s.add_runtime_dependency('activesupport', '>= 6.1.0')
55
55
  s.add_development_dependency "pry"
56
- s.required_ruby_version = '>= 2.4.0'
56
+ s.required_ruby_version = '>= 3.0.0'
57
57
  end
@@ -24,7 +24,7 @@ module ActiveHash
24
24
  class_attribute :_data, :dirty, :default_attributes, :scopes
25
25
 
26
26
  if Object.const_defined?(:ActiveModel)
27
- extend ActiveModel::Naming
27
+ extend ActiveModel::Translation
28
28
  include ActiveModel::Conversion
29
29
  else
30
30
  def to_param
@@ -50,6 +50,23 @@ module ActiveHash
50
50
  @field_names ||= []
51
51
  end
52
52
 
53
+ #
54
+ # Useful for CSV integration needing column names as strings.
55
+ #
56
+ # @return [Array<String>] An array of column names as strings.
57
+ #
58
+ # @example Usage
59
+ # class Country < ActiveHash::Base
60
+ # fields :name, :code
61
+ # end
62
+ #
63
+ # Country.column_names
64
+ # # => ["id", "name", "code"]
65
+ #
66
+ def column_names
67
+ field_names.map(&:name)
68
+ end
69
+
53
70
  def the_meta_class
54
71
  class << self
55
72
  self
@@ -85,12 +102,12 @@ module ActiveHash
85
102
  end
86
103
  end
87
104
 
88
- def exists?(args = nil)
105
+ def exists?(args = :none)
89
106
  if args.respond_to?(:id)
90
107
  record_index[args.id.to_s].present?
91
- elsif args == false
108
+ elsif !args
92
109
  false
93
- elsif args.nil?
110
+ elsif args == :none
94
111
  all.present?
95
112
  elsif args.is_a?(Hash)
96
113
  all.where(args).present?
@@ -198,7 +215,9 @@ module ActiveHash
198
215
  end
199
216
 
200
217
  def field(field_name, options = {})
218
+ field_name = field_name.to_sym
201
219
  validate_field(field_name)
220
+
202
221
  field_names << field_name
203
222
 
204
223
  add_default_value(field_name, options[:default]) if options.key?(:default)
@@ -210,7 +229,8 @@ module ActiveHash
210
229
  end
211
230
 
212
231
  def validate_field(field_name)
213
- if [:attributes].include?(field_name.to_sym)
232
+ field_name = field_name.to_sym
233
+ if [:attributes].include?(field_name)
214
234
  raise ReservedFieldError.new("#{field_name} is a reserved field in ActiveHash. Please use another name.")
215
235
  end
216
236
  end
@@ -263,8 +283,10 @@ module ActiveHash
263
283
  self.default_attributes[field_name] = default_value
264
284
  end
265
285
 
286
+ private :add_default_value
287
+
266
288
  def define_getter_method(field, default_value)
267
- unless instance_methods.include?(field.to_sym)
289
+ unless instance_methods.include?(field)
268
290
  define_method(field) do
269
291
  attributes[field].nil? ? default_value : attributes[field]
270
292
  end
@@ -135,6 +135,7 @@ module ActiveHash
135
135
  end
136
136
 
137
137
  def count
138
+ return super if block_given?
138
139
  length
139
140
  end
140
141
 
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "3.3.1"
3
+ VERSION = "4.0.0"
4
4
  end
5
5
  end
@@ -8,19 +8,16 @@ module ActiveHash
8
8
 
9
9
  def has_many(association_id, scope = nil, **options, &extension)
10
10
  if options[:through]
11
- klass_name = association_id.to_s.classify
12
- klass =
13
- begin
14
- klass_name.safe_constantize
15
- rescue StandardError, LoadError
16
- nil
17
- end
11
+ source_association_name = options[:source]&.to_s || association_id.to_s.singularize
12
+
13
+ through_klass = reflect_on_association(options[:through])&.klass
14
+ klass = through_klass&.reflect_on_association(source_association_name)&.klass
18
15
 
19
16
  if klass && klass < ActiveHash::Base
20
17
  define_method(association_id) do
21
18
  join_models = send(options[:through])
22
19
  join_models.flat_map do |join_model|
23
- join_model.send(association_id.to_s.singularize)
20
+ join_model.send(source_association_name)
24
21
  end.uniq
25
22
  end
26
23
 
@@ -33,12 +30,7 @@ module ActiveHash
33
30
 
34
31
  def belongs_to(name, scope = nil, **options)
35
32
  klass_name = options.key?(:class_name) ? options[:class_name] : name.to_s.camelize
36
- klass =
37
- begin
38
- klass_name.safe_constantize
39
- rescue StandardError, LoadError
40
- nil
41
- end
33
+ klass = klass_name.safe_constantize
42
34
 
43
35
  if klass && klass < ActiveHash::Base
44
36
  options = { class_name: klass_name }.merge(options)
data/lib/enum/enum.rb CHANGED
@@ -14,6 +14,20 @@ module ActiveHash
14
14
  reload
15
15
  end
16
16
 
17
+ def enum(columns)
18
+ columns.each do |column, values|
19
+ values = values.zip(values.map(&:to_s)).to_h if values.is_a?(Array)
20
+ values.each do |method, value|
21
+ class_eval <<~METHOD, __FILE__, __LINE__ + 1
22
+ # frozen_string_literal: true
23
+ def #{method}?
24
+ #{column} == #{value.inspect}
25
+ end
26
+ METHOD
27
+ end
28
+ end
29
+ end
30
+
17
31
  def insert(record)
18
32
  super
19
33
  set_constant(record) if defined?(@enum_accessors)
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: 3.3.1
4
+ version: 4.0.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: 2024-05-04 00:00:00.000000000 Z
32
+ date: 2025-07-29 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: activesupport
@@ -37,14 +37,14 @@ dependencies:
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 5.0.0
40
+ version: 6.1.0
41
41
  type: :runtime
42
42
  prerelease: false
43
43
  version_requirements: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: 5.0.0
47
+ version: 6.1.0
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: pry
50
50
  requirement: !ruby/object:Gem::Requirement
@@ -101,14 +101,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
101
  requirements:
102
102
  - - ">="
103
103
  - !ruby/object:Gem::Version
104
- version: 2.4.0
104
+ version: 3.0.0
105
105
  required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  requirements:
107
107
  - - ">="
108
108
  - !ruby/object:Gem::Version
109
109
  version: '0'
110
110
  requirements: []
111
- rubygems_version: 3.5.9
111
+ rubygems_version: 3.5.22
112
112
  signing_key:
113
113
  specification_version: 4
114
114
  summary: An ActiveRecord-like model that uses a hash or file as a datasource