active_hash 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +3 -0
- data/README.md +26 -10
- data/active_hash.gemspec +3 -3
- data/lib/active_hash/base.rb +39 -24
- data/lib/active_hash/version.rb +1 -1
- data/lib/active_yaml/aliases.rb +3 -3
- data/lib/associations/associations.rb +8 -1
- data/lib/enum/enum.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8402bf0998ccf7ae1e02e14a7f8d751d223fb2f
|
4
|
+
data.tar.gz: cebb5ad579c8330dd32de73c0c3e30b2ab9bb7a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4d6a5528d93310bb3d84ba38e92074df4a9717fd22b36ccf6133b6bd038ccd8a47b111d5e6c89255c64bbfb6cc86e000fc184a9d92880a41621a44ccb5d4218
|
7
|
+
data.tar.gz: 294a765759e4310f83de96efcc43d670c099196e070a470483fd1123b9b52bca01fbf76b87a9a93fec746993e0470a4c00c54950f48d1576e68b735dc734b8f8
|
data/CHANGELOG
CHANGED
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
|
145
|
-
Country.count
|
146
|
-
Country.first
|
147
|
-
Country.last
|
148
|
-
Country.find 1
|
149
|
-
Country.find [1,2]
|
150
|
-
Country.find :all
|
151
|
-
Country.find :all, args
|
152
|
-
Country.find_by_id 1
|
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 `
|
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 =
|
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
|
data/lib/active_hash/base.rb
CHANGED
@@ -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.
|
159
|
-
|
160
|
-
|
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
|
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
|
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
|
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
|
342
|
+
unless singleton_methods.include?(method_name)
|
318
343
|
the_meta_class.instance_eval do
|
319
344
|
define_method(method_name) do |*args|
|
320
|
-
|
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
|
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
|
-
|
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
|
data/lib/active_hash/version.rb
CHANGED
data/lib/active_yaml/aliases.rb
CHANGED
@@ -12,15 +12,15 @@ module ActiveYaml
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def raw_data
|
15
|
-
|
16
|
-
v.kind_of? Hash and k.match
|
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
|
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 =
|
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
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
|
+
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:
|
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:
|
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.
|
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
|