active_hash 1.4.1 → 1.5.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
  SHA1:
3
- metadata.gz: 652802c90d3e70506b8997f124021284e8b073be
4
- data.tar.gz: a4b876f9bc3c7316c6d12f3945e2b74b20b8a11f
3
+ metadata.gz: b8402bf0998ccf7ae1e02e14a7f8d751d223fb2f
4
+ data.tar.gz: cebb5ad579c8330dd32de73c0c3e30b2ab9bb7a5
5
5
  SHA512:
6
- metadata.gz: 53e0ad1f1e45a7adb6cd51ea367102ec959a21c19245d52ec9534c6976754fdf888efe3da463818ff1c14c9c0048fa2c0e51a9b4164857ee811076eb6af11612
7
- data.tar.gz: cb66a911b00f3b9b4c25de6485fc0e7b9fff3b66efeabd5e70a5dcf52134afba7d990951cc232beaec45dc4ed12b1760c3fe79e1a818b7d317bd8f6a15692d67
6
+ metadata.gz: d4d6a5528d93310bb3d84ba38e92074df4a9717fd22b36ccf6133b6bd038ccd8a47b111d5e6c89255c64bbfb6cc86e000fc184a9d92880a41621a44ccb5d4218
7
+ data.tar.gz: 294a765759e4310f83de96efcc43d670c099196e070a470483fd1123b9b52bca01fbf76b87a9a93fec746993e0470a4c00c54950f48d1576e68b735dc734b8f8
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ 2017-03-24 (v1.5.0)
2
+ - add support for `.find_by!`(@syguer)
3
+
1
4
  2015-09-13 (v1.4.1)
2
5
  - fix bug where `#attributes` didn't contain default values #107
3
6
  - add support for `.find_by` and `#_read_attribute`. Thanks, @andrewfader!
data/README.md CHANGED
@@ -141,15 +141,17 @@ If you prefer to store your data in YAML, see below.
141
141
 
142
142
  ActiveHash gives you ActiveRecord-esque methods like:
143
143
  ```ruby
144
- Country.all # => returns all Country objects
145
- Country.count # => returns the length of the .data array
146
- Country.first # => returns the first country object
147
- Country.last # => returns the last country object
148
- Country.find 1 # => returns the first country object with that id
149
- Country.find [1,2] # => returns all Country objects with ids in the array
150
- Country.find :all # => same as .all
151
- Country.find :all, args # => the second argument is totally ignored, but allows it to play nicely with AR
152
- Country.find_by_id 1 # => find the first object that matches the id
144
+ Country.all # => returns all Country objects
145
+ Country.count # => returns the length of the .data array
146
+ Country.first # => returns the first country object
147
+ Country.last # => returns the last country object
148
+ Country.find 1 # => returns the first country object with that id
149
+ Country.find [1,2] # => returns all Country objects with ids in the array
150
+ Country.find :all # => same as .all
151
+ Country.find :all, args # => the second argument is totally ignored, but allows it to play nicely with AR
152
+ Country.find_by_id 1 # => find the first object that matches the id
153
+ Country.find_by(name: 'US') # => returns the first country object with specified argument
154
+ Country.find_by!(name: 'US') # => same as find_by, but raise exception when not found
153
155
  ```
154
156
  It also gives you a few dynamic finder methods. For example, if you defined :name as a field, you'd get:
155
157
  ```ruby
@@ -217,7 +219,7 @@ class Person < ActiveRecord::Base
217
219
  belongs_to :country
218
220
  end
219
221
  ```
220
- However, as of ActiveRecord 3.1 support for ActiveRecord's `belong_to` is broken. Instead, you must extend ActiveHash::Associations::ActiveRecordExtensions method:
222
+ However, as of ActiveRecord 3.1 support for ActiveRecord's `belongs_to` is broken. Instead, you must extend ActiveHash::Associations::ActiveRecordExtensions method:
221
223
  ```ruby
222
224
  class Country < ActiveHash::Base
223
225
  end
@@ -489,6 +491,20 @@ In Rails, in development mode, it reloads the entire class, which reloads the fi
489
491
 
490
492
  NOTE: By default, .full_path refers to the current working directory. In a rails app, this will be RAILS_ROOT.
491
493
 
494
+
495
+ ## Reloading ActiveYaml, ActiveJSON and ActiveFile
496
+
497
+ During the development you may often change your data and want to see your changes immediately.
498
+ Call `Model.reload(true)` to force reload the data from disk.
499
+
500
+ In Rails, you can use this snippet. Please just note it resets the state every request, which may not always be desired.
501
+
502
+ ```ruby
503
+ before_filter do
504
+ [Model1, Model2, Model3].each { |m| m.reload(true) }
505
+ end
506
+ ```
507
+
492
508
  ## Enum
493
509
 
494
510
  ActiveHash can expose its data in an Enumeration by setting constants for each record. This allows records to be accessed in code through a constant set in the ActiveHash class.
data/active_hash.gemspec CHANGED
@@ -4,7 +4,7 @@ $:.push File.expand_path("../lib", __FILE__)
4
4
  require "active_hash/version"
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{active_hash}
7
+ s.name = "active_hash"
8
8
  s.version = ActiveHash::Gem::VERSION
9
9
  s.authors = [
10
10
  "Jeff Dean",
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "Matthew O'Riordan",
29
29
  "Brett Richardson",
30
30
  "Rachel Heaton",
31
+ "Keisuke Izumiya"
31
32
  ]
32
33
  s.email = %q{jeff@zilkey.com}
33
34
  s.summary = %q{An ActiveRecord-like model that uses a hash or file as a datasource}
@@ -43,7 +44,6 @@ Gem::Specification.new do |s|
43
44
  Dir.glob("lib/**/*")
44
45
  ].flatten
45
46
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
46
- s.require_paths = ["lib"]
47
-
48
47
  s.add_runtime_dependency('activesupport', '>= 2.2.2')
48
+ s.required_ruby_version = '>= 1.9.3'
49
49
  end
@@ -150,14 +150,21 @@ module ActiveHash
150
150
  if options.has_key?(:conditions)
151
151
  where(options[:conditions])
152
152
  else
153
- @records || []
153
+ @records ||= []
154
154
  end
155
155
  end
156
156
 
157
157
  def where(options)
158
- return @records if options.nil?
159
- (@records || []).select do |record|
160
- options.all? { |col, match| record[col] == match }
158
+ return @records if options.blank?
159
+
160
+ # use index if searching by id
161
+ if (ids = (options.delete(:id) || options.delete("id")))
162
+ candidates = Array.wrap(ids).map { |id| find_by_id(id) }
163
+ end
164
+ return candidates if options.blank?
165
+
166
+ (candidates || @records || []).select do |record|
167
+ match_options?(record, options)
161
168
  end
162
169
  end
163
170
 
@@ -165,6 +172,22 @@ module ActiveHash
165
172
  where(options).first
166
173
  end
167
174
 
175
+ def find_by!(options)
176
+ find_by(options) || (raise RecordNotFound.new("Couldn't find #{name}"))
177
+ end
178
+
179
+ def match_options?(record, options)
180
+ options.all? do |col, match|
181
+ if match.kind_of?(Array)
182
+ match.include?(record[col])
183
+ else
184
+ record[col] == match
185
+ end
186
+ end
187
+ end
188
+
189
+ private :match_options?
190
+
168
191
  def count
169
192
  all.length
170
193
  end
@@ -191,6 +214,8 @@ module ActiveHash
191
214
  nil
192
215
  when :all
193
216
  all
217
+ when :first
218
+ all(*args).first
194
219
  when Array
195
220
  id.map { |i| find(i) }
196
221
  else
@@ -281,7 +306,7 @@ module ActiveHash
281
306
  end
282
307
 
283
308
  def define_getter_method(field, default_value)
284
- unless has_instance_method?(field)
309
+ unless instance_methods.include?(field.to_sym)
285
310
  define_method(field) do
286
311
  attributes[field].nil? ? default_value : attributes[field]
287
312
  end
@@ -291,8 +316,8 @@ module ActiveHash
291
316
  private :define_getter_method
292
317
 
293
318
  def define_setter_method(field)
294
- method_name = "#{field}="
295
- unless has_instance_method?(method_name)
319
+ method_name = :"#{field}="
320
+ unless instance_methods.include?(method_name)
296
321
  define_method(method_name) do |new_val|
297
322
  attributes[field] = new_val
298
323
  end
@@ -303,7 +328,7 @@ module ActiveHash
303
328
 
304
329
  def define_interrogator_method(field)
305
330
  method_name = :"#{field}?"
306
- unless has_instance_method?(method_name)
331
+ unless instance_methods.include?(method_name)
307
332
  define_method(method_name) do
308
333
  send(field).present?
309
334
  end
@@ -314,10 +339,10 @@ module ActiveHash
314
339
 
315
340
  def define_custom_find_method(field_name)
316
341
  method_name = :"find_by_#{field_name}"
317
- unless has_singleton_method?(method_name)
342
+ unless singleton_methods.include?(method_name)
318
343
  the_meta_class.instance_eval do
319
344
  define_method(method_name) do |*args|
320
- options = args.extract_options!
345
+ args.extract_options!
321
346
  identifier = args[0]
322
347
  all.detect { |record| record.send(field_name) == identifier }
323
348
  end
@@ -329,11 +354,11 @@ module ActiveHash
329
354
 
330
355
  def define_custom_find_all_method(field_name)
331
356
  method_name = :"find_all_by_#{field_name}"
332
- unless has_singleton_method?(method_name)
357
+ unless singleton_methods.include?(method_name)
333
358
  the_meta_class.instance_eval do
334
359
  unless singleton_methods.include?(method_name)
335
360
  define_method(method_name) do |*args|
336
- options = args.extract_options!
361
+ args.extract_options!
337
362
  identifier = args[0]
338
363
  all.select { |record| record.send(field_name) == identifier }
339
364
  end
@@ -385,18 +410,6 @@ module ActiveHash
385
410
 
386
411
  private :mark_clean
387
412
 
388
- def has_instance_method?(name)
389
- instance_methods.map { |method| method.to_sym }.include?(name)
390
- end
391
-
392
- private :has_instance_method?
393
-
394
- def has_singleton_method?(name)
395
- singleton_methods.map { |method| method.to_sym }.include?(name)
396
- end
397
-
398
- private :has_singleton_method?
399
-
400
413
  end
401
414
 
402
415
  def initialize(attributes = {})
@@ -405,6 +418,7 @@ module ActiveHash
405
418
  attributes.dup.each do |key, value|
406
419
  send "#{key}=", value
407
420
  end
421
+ yield self if block_given?
408
422
  end
409
423
 
410
424
  def attributes
@@ -422,6 +436,7 @@ module ActiveHash
422
436
  def _read_attribute(key)
423
437
  attributes[key]
424
438
  end
439
+ alias_method :read_attribute, :_read_attribute
425
440
 
426
441
  def []=(key, val)
427
442
  attributes[key] = val
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "1.4.1"
3
+ VERSION = "1.5.0"
4
4
  end
5
5
  end
@@ -12,15 +12,15 @@ module ActiveYaml
12
12
  end
13
13
 
14
14
  def raw_data
15
- YAML.load_file(full_path).reject do |k, v|
16
- v.kind_of? Hash and k.match /^\//i
15
+ super.reject do |k, v|
16
+ v.kind_of? Hash and k.match(/^\//i)
17
17
  end
18
18
  end
19
19
 
20
20
  end
21
21
 
22
22
  def initialize(attributes={})
23
- super unless attributes.keys.index { |k| k.to_s.match /^\//i }
23
+ super unless attributes.keys.index { |k| k.to_s.match(/^\//i) }
24
24
  end
25
25
  end
26
26
 
@@ -8,7 +8,14 @@ module ActiveHash
8
8
  options = our_args.extract_options!
9
9
  name = our_args.shift
10
10
  options = {:class_name => name.to_s.camelize }.merge(options)
11
- klass = options[:class_name].constantize rescue nil
11
+ klass =
12
+ begin
13
+ options[:class_name].constantize
14
+ rescue
15
+ nil
16
+ rescue LoadError
17
+ nil
18
+ end
12
19
  if klass && klass < ActiveHash::Base
13
20
  belongs_to_active_hash(name, options)
14
21
  else
data/lib/enum/enum.rb CHANGED
@@ -16,7 +16,7 @@ module ActiveHash
16
16
 
17
17
  def insert(record)
18
18
  super
19
- set_constant(record) if @enum_accessors.present?
19
+ set_constant(record) if defined?(@enum_accessors)
20
20
  end
21
21
 
22
22
  def delete_all
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: 1.4.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Dean
@@ -25,10 +25,11 @@ authors:
25
25
  - Matthew O'Riordan
26
26
  - Brett Richardson
27
27
  - Rachel Heaton
28
+ - Keisuke Izumiya
28
29
  autorequire:
29
30
  bindir: bin
30
31
  cert_chain: []
31
- date: 2015-09-13 00:00:00.000000000 Z
32
+ date: 2017-04-04 00:00:00.000000000 Z
32
33
  dependencies:
33
34
  - !ruby/object:Gem::Dependency
34
35
  name: activesupport
@@ -78,7 +79,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
79
  requirements:
79
80
  - - ">="
80
81
  - !ruby/object:Gem::Version
81
- version: '0'
82
+ version: 1.9.3
82
83
  required_rubygems_version: !ruby/object:Gem::Requirement
83
84
  requirements:
84
85
  - - ">="
@@ -86,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
87
  version: '0'
87
88
  requirements: []
88
89
  rubyforge_project:
89
- rubygems_version: 2.4.8
90
+ rubygems_version: 2.5.1
90
91
  signing_key:
91
92
  specification_version: 4
92
93
  summary: An ActiveRecord-like model that uses a hash or file as a datasource