activerecord-attributes 0.1.0

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-attributes.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Charles Lowell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # Activerecord::Attributes
2
+
3
+ Just looking at an ActiveRecord class can be cognitively cumbersome. Not only do you have to comprehend all the
4
+ scopes, relations, validations, etc, present in the file, but at the same time you have mentally
5
+ overlay the database table that it represents in order to get the full picture.
6
+
7
+ Why should you required to reconcile two related mental models in realtime in order to comprehend your own, or somebody
8
+ else's code?
9
+
10
+ `ActiveRecord::Attributes` is a simple way to annotate and sanity check your record classess so that they can be both
11
+ understood and known to be correct.
12
+
13
+ ## Usage
14
+
15
+ Declare attributes in your record class
16
+
17
+ class Artist < ActiveRecord::Base
18
+ attribute :name
19
+ attribute :discipline
20
+ end
21
+
22
+ Upon loading this class, the attribute declaration will assert that the columns corresponding to `:name` and
23
+ `:discipline` do indeed exist and fail fast otherwise.
24
+
25
+ That's it!
26
+
27
+ ## Migrations
28
+
29
+ Existential column assertions are all well and good, but in the context of migrations, the columns that back an
30
+ active record's attributes could be in flux. For this reason, there is the facility to disable sanity checking:
31
+
32
+ ActiveRecord::Attributes.without_assertions do
33
+ Artist.find_by_passion('painting')
34
+ end
35
+
36
+
37
+ ## Installation
38
+
39
+ Add this line to your application's Gemfile:
40
+
41
+ gem 'activerecord-attributes'
42
+
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.rspec_opts = '--color --format documentation'
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/activerecord-attributes/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["The Frontside"]
6
+ gem.email = ["info@thefrontside.net"]
7
+ gem.description = %q{Add simple attribute annotations to your active records}
8
+ gem.summary = %q{Because not being able to see your actual class definition is a dev papercut}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "activerecord-attributes"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Activerecord::Attributes::VERSION
17
+
18
+ gem.add_dependency 'activerecord'
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'sqlite3'
23
+ end
@@ -0,0 +1,32 @@
1
+ require "activerecord-attributes/version"
2
+ require 'active_support'
3
+
4
+ module ActiveRecord
5
+ module Attributes
6
+
7
+ def attribute(name)
8
+ return unless ActiveRecord::Attributes.with_assertions?
9
+ unless attribute_names.include? name.to_s
10
+ fail AttributeError, "#{self} does not actually have an attribute named #{name}"
11
+ end
12
+ end
13
+
14
+ def self.without_assertions
15
+ current = @disable_assertions
16
+ @disable_assertions = true
17
+ yield
18
+ ensure
19
+ @disable_assertions = current
20
+ end
21
+
22
+ def self.with_assertions?
23
+ !@disable_assertions
24
+ end
25
+
26
+ class AttributeError < StandardError; end
27
+ end
28
+
29
+ ActiveSupport.on_load(:active_record) do
30
+ extend Attributes
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ module Activerecord
2
+ module Attributes
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ require 'active_record'
2
+ require 'activerecord-attributes'
3
+
4
+ describe "An active record" do
5
+ before do
6
+ ActiveRecord::Base.establish_connection({
7
+ :adapter => "sqlite3",
8
+ :database => File.expand_path('../artists.sqlite3', __FILE__)
9
+ })
10
+ @class = Class.new(ActiveRecord::Base)
11
+ @class.table_name = 'artists'
12
+ end
13
+ it "is ok with columns that exists" do
14
+ lambda {
15
+ @class.attribute :talent
16
+ @class.attribute :passion
17
+ }.should_not raise_error
18
+ end
19
+
20
+ it "complains when you specify an attribute with no corresponding column" do
21
+ lambda {
22
+ @class.attribute 'common_sense'
23
+ }.should raise_error(ActiveRecord::Attributes::AttributeError)
24
+ end
25
+
26
+ it "allows for disabling sanity checks when running migrations" do
27
+ ActiveRecord::Attributes.without_assertions do
28
+ lambda {
29
+ @class.attribute :starving
30
+ }.should_not raise_error
31
+ end
32
+ end
33
+ end
Binary file
@@ -0,0 +1 @@
1
+ $:.unshift File.expand_path '../../lib', __FILE_
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - The Frontside
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: sqlite3
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Add simple attribute annotations to your active records
79
+ email:
80
+ - info@thefrontside.net
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE
88
+ - README.md
89
+ - Rakefile
90
+ - activerecord-attributes.gemspec
91
+ - lib/activerecord-attributes.rb
92
+ - lib/activerecord-attributes/version.rb
93
+ - spec/activerecord-attributes_spec.rb
94
+ - spec/artists.sqlite3
95
+ - spec/spec_helper.rb
96
+ homepage: ''
97
+ licenses: []
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ segments:
109
+ - 0
110
+ hash: 3845241495246877097
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ segments:
118
+ - 0
119
+ hash: 3845241495246877097
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 1.8.24
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Because not being able to see your actual class definition is a dev papercut
126
+ test_files:
127
+ - spec/activerecord-attributes_spec.rb
128
+ - spec/artists.sqlite3
129
+ - spec/spec_helper.rb