hashed 0.0.3 → 0.0.4

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.
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 field in dataset (default: "id").
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 simple use it!
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(:id)
15
- # you may want to have hash not by id
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(field: :created_at, only_field: :name)
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 objects collection}
12
- s.description = %q{Makes hash from AR objects collection}
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
 
@@ -1,3 +1,3 @@
1
1
  module Hashed
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
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 = { field: :id, only_field: false })
6
- opts = { field: opts } if opts.is_a? Symbol
7
- opts[:field] = :id if opts[:field].nil?
8
- result = {}
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
- self.all.each do |row|
11
- result[row.send(opts[:field])] = (opts[:only_field] ? row.send(opts[:only_field]) : row)
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
@@ -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(field: :id, only_field: :created_at).should eq({ 1 => Post.find(1).created_at, 2 => Post.find(2).created_at })
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.3
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: &70182324532520 !ruby/object:Gem::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: *70182324532520
24
+ version_requirements: *70307642435400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3-ruby
27
- requirement: &70182329336180 !ruby/object:Gem::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: *70182329336180
35
+ version_requirements: *70307642352860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activerecord
38
- requirement: &70182329335680 !ruby/object:Gem::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: *70182329335680
47
- description: Makes hash from AR objects collection
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 objects collection
92
+ summary: Makes hash from AR dataset
92
93
  test_files:
93
94
  - spec/hashed/hashed_spec.rb
94
95
  - spec/spec_helper.rb