lazy-attribute 3.0.1 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: dcebd941632c3e7f542524b136d7f23517beef58
4
- data.tar.gz: 606bbd17c3b5b198a1608e81f82c9980343f0a21
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjRlZTFmNGMxMmYyYzdlOTEwMjYxYzQyZmQ4YzI2NDNlOGRhMjI0Mg==
5
+ data.tar.gz: !binary |-
6
+ MTY4MjdkMTFjOWIzNTcwZmUzY2ZiMTU3N2Q4NDIyYzY4NjZlN2EzMw==
5
7
  SHA512:
6
- metadata.gz: da0408b255f671f58f35da26ccde95b0af8ad611309d31a53d81fcbfc150b122a4113ab2328dcce1cb8fcf70743b355a47e5a52dee6d3bd81bf0c0079f1acbe5
7
- data.tar.gz: cb1ccc754ad6f17376611df164b367101930700755e8d984772410a05c460e034cf175b95589d4912afd302f5b3ca24dc35b481a8a12222e7caeafb52775165c
8
+ metadata.gz: !binary |-
9
+ OTI3ZWUwYzE2ZGEyNmM0NThjMzc3NzhmNDM5MGZkYzA1MmZmYTIxODE1MzRj
10
+ NTg1NWFlNTNmMDE2NDE1YzFhZTU5Y2E5NGJjYTAwZGI0ZmRlN2VmNzU5OTNk
11
+ MTQ2ZTYwYmMwMzU4YWU5ZTM0NjQ4MmIzYTA4NjMxMDc2ZWJiZDk=
12
+ data.tar.gz: !binary |-
13
+ YjJiNjYwMzcwNTc2YTk4MmM0YzFhMmE4NTZiYWM5MDYzMWNlYzJjYTdkODA3
14
+ MTcwODkxNmU4Y2RkZTliODYzMDQ2ZGQ4NDJjNzJiMWI4NzRhNWFhYjFiYjA3
15
+ YThmZGU1MTU5OGIwYzU2Y2M2OGM4MzhiMjYxNTg0MDQxNjMxMDc=
data/README.md CHANGED
@@ -7,7 +7,7 @@ You can minimize find_by query of a particular attribute in a model using lazy a
7
7
 
8
8
  Add this line to your application's Gemfile:
9
9
 
10
- gem 'lazy-attribute'
10
+ gem 'lazy-attribute', '~> 3.0.1'
11
11
 
12
12
  And then execute:
13
13
 
@@ -51,6 +51,21 @@ lazy_attribute :first_name, raise_error: true, key: :fn
51
51
  User.fn('first name')
52
52
  ```
53
53
 
54
+ ```ruby
55
+ lazy_attribute :email, create_if_not_found: true
56
+ ```
57
+
58
+ Will try to fetch the user object with the matched email address, if the record is not found in the db the same will be created. If any error like validation protected the record being saved, then the new built object will be returned.
59
+
60
+ ```ruby
61
+ user = User['invalid email']
62
+
63
+ user.errors.message
64
+ => {:email=>["Please enter valid email format"]}
65
+ ```
66
+
67
+ if you want the exception needs to be threw, then use the ```raise_error: true``` option
68
+
54
69
  ## Contributing
55
70
 
56
71
  1. Fork it ( https://github.com/selvachezhian/lazy-attribute/fork )
@@ -6,8 +6,8 @@ require 'lazy/attribute/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'lazy-attribute'
8
8
  spec.version = Lazy::Attribute::VERSION
9
- spec.authors = ['Selva Chezhian', 'Saravanan Hari']
10
- spec.email = %w(selvachezhian.labam@gmail.com sarhari@gmail.com)
9
+ spec.authors = ['Selva Chezhian', 'Saravanan Hari', 'Elavarasu']
10
+ spec.email = %w(selvachezhian.labam@gmail.com sarhari@gmail.com elavarasu.pandiyan@gmail.com)
11
11
  spec.summary = %q{Minimizing find_by query for a single repeated attribute}
12
12
  spec.description = %q{This gem provides a simple and extremely flexible way to use most frequent find_by query in a model. You can minimize find_by query of a particular attribute in a model using lazy attribute gem.}
13
13
  spec.homepage = 'https://github.com/selvachezhian/lazy-attribute'
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
+ spec.post_install_message = 'Yey! completed. Thanks for installing!'
20
21
 
21
22
  spec.add_development_dependency 'bundler', '~> 1.6'
22
23
  spec.add_development_dependency 'rake', '~> 0'
@@ -6,12 +6,15 @@ module Lazy
6
6
  #
7
7
  # @param attribute [Symbol]
8
8
  # @param [Hash] options
9
- # @option options [Boolean] :raise_error (false) will raise ActiveRecord::RecordNotFoundException if the value set as true and the record not present
10
9
  # @option options [Symbol] :key (:default) if the key parameter is not set, it will take the default as the key
10
+ # @option options [Boolean] :create_if_not_found (false) if the parameter is set as true, will create the record if the record is not available
11
+ # @option options [Boolean] :raise_error (false) will raise exceptions
12
+ # ActiveRecord::RecordNotFoundException if the value set as true and the record not present <br>
13
+ # ActiveRecord::RecordInvalid: Validation failed exception if any validation error thrown at the time of creation missing record
11
14
  #
12
15
  # @return [none]
13
16
  def lazy_attribute(attribute, options = {})
14
- default_options = { raise_error: false, key: :default }
17
+ default_options = { raise_error: false, key: :default, create_if_not_found: false }
15
18
  options.reverse_merge!(default_options)
16
19
 
17
20
  singleton_class.instance_eval do
@@ -35,12 +38,20 @@ module Lazy
35
38
 
36
39
  private
37
40
 
38
- def dynamic_find_method(raise_error = false)
39
- raise_error ? 'find_by!' : 'find_by'
41
+ def dynamic_find_method(options)
42
+ if options[:raise_error] && options[:create_if_not_found]
43
+ 'find_or_create_by!'
44
+ elsif options[:raise_error] && !options[:create_if_not_found]
45
+ 'find_by!'
46
+ elsif !options[:raise_error] && options[:create_if_not_found]
47
+ 'find_or_create_by'
48
+ elsif !options[:raise_error] && !options[:create_if_not_found]
49
+ 'find_by'
50
+ end
40
51
  end
41
52
 
42
53
  def send_dynamic_method(options, attr, identifier)
43
- send(dynamic_find_method(options[:raise_error]), { attr.to_sym => identifier })
54
+ send(dynamic_find_method(options), { attr.to_sym => identifier })
44
55
  end
45
56
 
46
57
  end
@@ -1,5 +1,5 @@
1
1
  module Lazy
2
2
  module Attribute
3
- VERSION = '3.0.1'
3
+ VERSION = '3.1.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,70 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy-attribute
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Selva Chezhian
8
8
  - Saravanan Hari
9
+ - Elavarasu
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2015-01-28 00:00:00.000000000 Z
13
+ date: 2015-03-31 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: bundler
16
17
  requirement: !ruby/object:Gem::Requirement
17
18
  requirements:
18
- - - "~>"
19
+ - - ~>
19
20
  - !ruby/object:Gem::Version
20
21
  version: '1.6'
21
22
  type: :development
22
23
  prerelease: false
23
24
  version_requirements: !ruby/object:Gem::Requirement
24
25
  requirements:
25
- - - "~>"
26
+ - - ~>
26
27
  - !ruby/object:Gem::Version
27
28
  version: '1.6'
28
29
  - !ruby/object:Gem::Dependency
29
30
  name: rake
30
31
  requirement: !ruby/object:Gem::Requirement
31
32
  requirements:
32
- - - "~>"
33
+ - - ~>
33
34
  - !ruby/object:Gem::Version
34
35
  version: '0'
35
36
  type: :development
36
37
  prerelease: false
37
38
  version_requirements: !ruby/object:Gem::Requirement
38
39
  requirements:
39
- - - "~>"
40
+ - - ~>
40
41
  - !ruby/object:Gem::Version
41
42
  version: '0'
42
43
  - !ruby/object:Gem::Dependency
43
44
  name: yard
44
45
  requirement: !ruby/object:Gem::Requirement
45
46
  requirements:
46
- - - ">="
47
+ - - ! '>='
47
48
  - !ruby/object:Gem::Version
48
49
  version: '0'
49
50
  type: :development
50
51
  prerelease: false
51
52
  version_requirements: !ruby/object:Gem::Requirement
52
53
  requirements:
53
- - - ">="
54
+ - - ! '>='
54
55
  - !ruby/object:Gem::Version
55
56
  version: '0'
56
57
  - !ruby/object:Gem::Dependency
57
58
  name: activerecord
58
59
  requirement: !ruby/object:Gem::Requirement
59
60
  requirements:
60
- - - "~>"
61
+ - - ~>
61
62
  - !ruby/object:Gem::Version
62
63
  version: '4.1'
63
64
  type: :runtime
64
65
  prerelease: false
65
66
  version_requirements: !ruby/object:Gem::Requirement
66
67
  requirements:
67
- - - "~>"
68
+ - - ~>
68
69
  - !ruby/object:Gem::Version
69
70
  version: '4.1'
70
71
  description: This gem provides a simple and extremely flexible way to use most frequent
@@ -73,11 +74,12 @@ description: This gem provides a simple and extremely flexible way to use most f
73
74
  email:
74
75
  - selvachezhian.labam@gmail.com
75
76
  - sarhari@gmail.com
77
+ - elavarasu.pandiyan@gmail.com
76
78
  executables: []
77
79
  extensions: []
78
80
  extra_rdoc_files: []
79
81
  files:
80
- - ".gitignore"
82
+ - .gitignore
81
83
  - Gemfile
82
84
  - LICENSE.txt
83
85
  - README.md
@@ -90,18 +92,18 @@ homepage: https://github.com/selvachezhian/lazy-attribute
90
92
  licenses:
91
93
  - MIT
92
94
  metadata: {}
93
- post_install_message:
95
+ post_install_message: Yey! completed. Thanks for installing!
94
96
  rdoc_options: []
95
97
  require_paths:
96
98
  - lib
97
99
  required_ruby_version: !ruby/object:Gem::Requirement
98
100
  requirements:
99
- - - ">="
101
+ - - ! '>='
100
102
  - !ruby/object:Gem::Version
101
103
  version: '0'
102
104
  required_rubygems_version: !ruby/object:Gem::Requirement
103
105
  requirements:
104
- - - ">="
106
+ - - ! '>='
105
107
  - !ruby/object:Gem::Version
106
108
  version: '0'
107
109
  requirements: []
@@ -111,4 +113,3 @@ signing_key:
111
113
  specification_version: 4
112
114
  summary: Minimizing find_by query for a single repeated attribute
113
115
  test_files: []
114
- has_rdoc: yard