hashed 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -5
- data/hashed.gemspec +2 -2
- data/lib/hashed/version.rb +1 -1
- data/lib/hashed.rb +8 -6
- data/spec/hashed/hashed_spec.rb +1 -1
- metadata +10 -9
data/README.md
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
# Hashed
|
2
2
|
|
3
|
-
Provides `hashed` method to ActiveRecord objects-set that return hash where keys is value of some
|
3
|
+
Provides `hashed` method to ActiveRecord objects-set that return hash where keys is value of some attribute of object (default – primary key of used table).
|
4
|
+
|
5
|
+
Put this line to your Gemfile
|
4
6
|
|
5
7
|
```ruby
|
6
8
|
gem "hashed"
|
7
9
|
```
|
8
10
|
|
9
|
-
Then run `bundle install` and
|
11
|
+
Then run `bundle install` and let's go!
|
10
12
|
|
11
13
|
```ruby
|
12
14
|
Category.active.hashed
|
13
15
|
# equal to
|
14
|
-
Category.active.hashed(
|
15
|
-
# you may want to have hash not by
|
16
|
+
Category.active.hashed(Category.primary_key)
|
17
|
+
# you may want to have hash not by primary_key
|
16
18
|
Category.active.hashed(:created_at)
|
17
19
|
# you may want to select only one field ({key: value})
|
18
|
-
Category.active.hashed(
|
20
|
+
Category.active.hashed(by: :created_at, only: :name)
|
19
21
|
```
|
data/hashed.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["Rinat Shaykhutdinov"]
|
9
9
|
s.email = ["rinat.crone@gmail.com"]
|
10
10
|
s.homepage = "https://github.com/cronych/hashed"
|
11
|
-
s.summary = %q{Makes hash from AR
|
12
|
-
s.description = %q{
|
11
|
+
s.summary = %q{Makes hash from AR dataset}
|
12
|
+
s.description = %q{Provides `hashed` method to ActiveRecord dataset, that return hash where keys is value of some attribute of object (default – primary key of used table).}
|
13
13
|
|
14
14
|
s.rubyforge_project = "hashed"
|
15
15
|
|
data/lib/hashed/version.rb
CHANGED
data/lib/hashed.rb
CHANGED
@@ -2,13 +2,15 @@ require "hashed/version"
|
|
2
2
|
require "active_record"
|
3
3
|
|
4
4
|
module Hashed
|
5
|
-
def hashed(opts = {
|
6
|
-
|
7
|
-
opts
|
8
|
-
|
5
|
+
def hashed(opts = {})
|
6
|
+
result = {}
|
7
|
+
opts = { by: opts } if opts.is_a? Symbol
|
8
|
+
options = { by: primary_key.to_sym, only: false }
|
9
|
+
options.merge! opts
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
dataset = options[:only] ? select([options[:by], options[:only]]) : self
|
12
|
+
dataset.all.each do |row|
|
13
|
+
result[row.send(options[:by])] = (options[:only] ? row.send(options[:only]) : row)
|
12
14
|
end
|
13
15
|
|
14
16
|
result
|
data/spec/hashed/hashed_spec.rb
CHANGED
@@ -11,7 +11,7 @@ describe Hashed do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "return id-keyed hash with one value" do
|
14
|
-
Post.hashed(
|
14
|
+
Post.hashed(by: :id, only: :created_at).should eq({ 1 => Post.find(1).created_at, 2 => Post.find(2).created_at })
|
15
15
|
end
|
16
16
|
|
17
17
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-12-01 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70307642435400 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70307642435400
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sqlite3-ruby
|
27
|
-
requirement: &
|
27
|
+
requirement: &70307642352860 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70307642352860
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activerecord
|
38
|
-
requirement: &
|
38
|
+
requirement: &70307642295240 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,8 +43,9 @@ dependencies:
|
|
43
43
|
version: '3.0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
47
|
-
description:
|
46
|
+
version_requirements: *70307642295240
|
47
|
+
description: Provides `hashed` method to ActiveRecord dataset, that return hash where
|
48
|
+
keys is value of some attribute of object (default – primary key of used table).
|
48
49
|
email:
|
49
50
|
- rinat.crone@gmail.com
|
50
51
|
executables: []
|
@@ -88,7 +89,7 @@ rubyforge_project: hashed
|
|
88
89
|
rubygems_version: 1.8.11
|
89
90
|
signing_key:
|
90
91
|
specification_version: 3
|
91
|
-
summary: Makes hash from AR
|
92
|
+
summary: Makes hash from AR dataset
|
92
93
|
test_files:
|
93
94
|
- spec/hashed/hashed_spec.rb
|
94
95
|
- spec/spec_helper.rb
|