static-model 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .yardoc
2
+ doc
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --protected --charset UTF-8 --markup markdown lib/**/*.rb - LICENSE
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ The MIT License
2
+ ===============
3
+
4
+ © 2011 Cody Robbins
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,94 @@
1
+ Static Model
2
+ ============
3
+
4
+ This is a compilation of some boilerplate code for a non–database-backed model based on ActiveModel. You might want to use this kind of static model for anything that needs to be validated but doesn’t persist to the database—such as a login authenticator or an account password reset initiator. It includes the appropriate ActiveModel modules, has a constructor that initializes attributes from a hash, and has some helper methods useful for implementing custom validations.
5
+
6
+ Full documentation is at [RubyDoc.info](http://rubydoc.info/gems/static-model).
7
+
8
+ Example
9
+ -------
10
+
11
+ A user lookup class like the one below could be used as the parent class for a login authenticator or an account password reset initiator. It needs to find a user via their email, which entails validating the presence of the email attribute and providing access to the user that’s consequently looked up.
12
+
13
+ class UserLookup < StaticModel
14
+ # An accessor to allow specifying the email to look up.
15
+ attr_accessor(:email)
16
+
17
+ # A reader to allow access to the user that's looked up.
18
+ attr_reader(:user)
19
+
20
+ # Require the email attribute.
21
+ validates(:email,
22
+ :presence => {:message => 'Email is required.'})
23
+
24
+ # If the email is present, ensure that a user is associated with it.
25
+ validate do
26
+ no_other_errors? && user_exists?
27
+ end
28
+
29
+ # Find the user.
30
+ def user
31
+ User.find_by_email(@email)
32
+ end
33
+
34
+ # Add an error if no user is found.
35
+ def user_exists?
36
+ user ? true : add_email_error_and_return_false
37
+ end
38
+
39
+ protected
40
+
41
+ # Add an error.
42
+ def add_email_error_and_return_false
43
+ add_error_and_return_false('Email not found', :email)
44
+ end
45
+ end
46
+
47
+ To use this class in a Rails controller, you’d do something like
48
+
49
+ class UserLookupController < ApplicationController
50
+ def find
51
+ create_user_lookup
52
+ do_something if email_valid?
53
+ end
54
+
55
+ protected
56
+
57
+ def create_user_lookup
58
+ @user_lookup = UserLookup.new(params[:user_lookup])
59
+ end
60
+
61
+ def do_something
62
+ @user_lookup.user.do_something
63
+ end
64
+
65
+ def email_valid?
66
+ request.post? && @user_lookup.valid?
67
+ end
68
+ end
69
+
70
+ Colophon
71
+ --------
72
+
73
+ ### See also
74
+
75
+ If you like this gem, you may also want to check out [Declarative Find](http://codyrobbins.com/software/declarative-find), [Create New](http://codyrobbins.com/software/create-new), [Save Changes To](http://codyrobbins.com/software/save-changes-to), and [HTTP Error](http://codyrobbins.com/software/http-error).
76
+
77
+ ### Tested with
78
+
79
+ * ActiveModel 3.0.5 — 20 May 2011
80
+
81
+ ### Contributing
82
+
83
+ * [Source](https://github.com/codyrobbins/static-model)
84
+ * [Bug reports](https://github.com/codyrobbins/static-model/issues)
85
+
86
+ To send patches, please fork on GitHub and submit a pull request.
87
+
88
+ ### Credits
89
+
90
+ © 2011 [Cody Robbins](http://codyrobbins.com/). See LICENSE for details.
91
+
92
+ * [Homepage](http://codyrobbins.com/software/static-model)
93
+ * [My other gems](http://codyrobbins.com/software#gems)
94
+ * [Follow me on Twitter](http://twitter.com/codyrobbins)
@@ -0,0 +1,115 @@
1
+ # encoding: UTF-8
2
+
3
+ # A compilation of some boilerplate code for a non–database-backed model based on ActiveModel. You might want to use this kind of static model for anything that needs to be validated but doesn’t persist to the database—such as a login authenticator or an account password reset initiator. It includes the appropriate ActiveModel modules, has a constructor that initializes attributes from a hash, and has some helper methods useful for implementing custom validations.
4
+ #
5
+ # Example
6
+ # -------
7
+ #
8
+ # A user lookup class like the one below could be used as the parent class for a login authenticator or an account password reset initiator. It needs to find a user via their email, which entails validating the presence of the email attribute and providing access to the user that’s consequently looked up.
9
+ #
10
+ # class UserLookup < StaticModel
11
+ # # An accessor to allow specifying the email to look up.
12
+ # attr_accessor(:email)
13
+ #
14
+ # # A reader to allow access to the user that's looked up.
15
+ # attr_reader(:user)
16
+ #
17
+ # # Require the email attribute.
18
+ # validates(:email,
19
+ # :presence => {:message => 'Email is required.'})
20
+ #
21
+ # # If the email is present, ensure that a user is associated with it.
22
+ # validate do
23
+ # no_other_errors? && user_exists?
24
+ # end
25
+ #
26
+ # # Find the user.
27
+ # def user
28
+ # User.find_by_email(@email)
29
+ # end
30
+ #
31
+ # # Add an error if no user is found.
32
+ # def user_exists?
33
+ # user ? true : add_email_error_and_return_false
34
+ # end
35
+ #
36
+ # protected
37
+ #
38
+ # # Add an error.
39
+ # def add_email_error_and_return_false
40
+ # add_error_and_return_false('Email not found', :email)
41
+ # end
42
+ # end
43
+ #
44
+ # To use this class in a Rails controller, you’d do something like
45
+ #
46
+ # class UserLookupController < ApplicationController
47
+ # def find
48
+ # create_user_lookup
49
+ # do_something if email_valid?
50
+ # end
51
+ #
52
+ # protected
53
+ #
54
+ # def create_user_lookup
55
+ # @user_lookup = UserLookup.new(params[:user_lookup])
56
+ # end
57
+ #
58
+ # def do_something
59
+ # @user_lookup.user.do_something
60
+ # end
61
+ #
62
+ # def email_valid?
63
+ # request.post? && @user_lookup.valid?
64
+ # end
65
+ # end
66
+ class StaticModel
67
+ # Includes.
68
+ include(ActiveModel::Naming)
69
+ include(ActiveModel::Conversion)
70
+ include(ActiveModel::Validations)
71
+
72
+ # Methods.
73
+
74
+ # Creates an instance of the model with its attributes initialized from a hash, just like you can do with ActiveRecord models.
75
+ #
76
+ # @param attributes [Hash] The attributes of the model and the respective values to initialize them with.
77
+ #
78
+ # @example
79
+ # class User < StaticModel
80
+ # attr_accessor(:name, :email)
81
+ # end
82
+ #
83
+ # user = User.new(:name => 'Dagny Taggart', :email => 'dagny@taggart.com')
84
+ #
85
+ # user.name #=> Dagny Taggart
86
+ # user.email #=> dagny@taggart.com
87
+ def initialize(attributes = {})
88
+ attributes ||= {}
89
+ attributes.each do |name, value|
90
+ send("#{name}=", value)
91
+ end
92
+ end
93
+
94
+ # This method simply tells ActiveModel that this model is not database-backed.
95
+ def persisted?
96
+ false
97
+ end
98
+
99
+ protected
100
+
101
+ # A helper method to be used internally by subclasses which simply indicates whether there are currently any errors. This is useful for validation methods in subclasses that shouldn’t fire if there are already other errors. For example, the format of an email address might only need to be checked if the email address is present.
102
+ def no_other_errors?
103
+ errors.empty?
104
+ end
105
+
106
+ # A helper method to be used internally by subclasses which adds an error to the `:base` attribute and returns `false` so that it can halt validations chains.
107
+ #
108
+ # @param error [String] The error message.
109
+ # @param attribute [Symbol] The attribute of the model to place the error message on.
110
+ # @return [boolean] Returns `false` in order to halt any validation chain it may be invoked from.
111
+ def add_error_and_return_false(error, attribute = :base)
112
+ errors[attribute] << error
113
+ false
114
+ end
115
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'static-model'
5
+ s.version = '1.0'
6
+ s.summary = 'A base non–database-backed model for Rails.'
7
+ s.homepage = 'http://codyrobbins.com/software/static-model'
8
+ s.author = 'Cody Robbins'
9
+ s.email = 'cody@codyrobbins.com'
10
+
11
+ s.post_install_message = '
12
+ -------------------------------------------------------------
13
+ Follow me on Twitter! http://twitter.com/codyrobbins
14
+ -------------------------------------------------------------
15
+
16
+ '
17
+
18
+ s.files = `git ls-files`.split
19
+
20
+ s.add_dependency('activemodel')
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static-model
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "1.0"
6
+ platform: ruby
7
+ authors:
8
+ - Cody Robbins
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-31 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: activemodel
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description:
28
+ email: cody@codyrobbins.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - .yardopts
38
+ - LICENSE
39
+ - README.markdown
40
+ - lib/static-model.rb
41
+ - static-model.gemspec
42
+ has_rdoc: true
43
+ homepage: http://codyrobbins.com/software/static-model
44
+ licenses: []
45
+
46
+ post_install_message: "\n\
47
+ -------------------------------------------------------------\n\
48
+ Follow me on Twitter! http://twitter.com/codyrobbins\n\
49
+ -------------------------------------------------------------\n\n"
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.6.2
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: "A base non\xE2\x80\x93database-backed model for Rails."
73
+ test_files: []
74
+