toy-attributes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = ToyAttributes
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ToyAttributes'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :toy-attributes do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,12 @@
1
+ module ToyAttributes::Methods
2
+
3
+ ToyAttributes::COLUMN_TYPES.each do |column_type|
4
+ define_method column_type do |*args|
5
+ options = args.extract_options!
6
+ args.each { |column_name| ToyAttributes::Observer.watch! column_name, column_type, self, options }
7
+ end
8
+ end
9
+
10
+ end
11
+
12
+ ActiveRecord::Base.extend ToyAttributes::Methods
@@ -0,0 +1,35 @@
1
+ module ToyAttributes::Observer
2
+
3
+ def self.watch! column_name, column_type, model, options={}
4
+ assert_existence_of_table(model)
5
+ assert_existence_of_column(column_name, column_type, model)
6
+ model.reset_column_information
7
+ model.attr_accessible column_name
8
+ end
9
+
10
+ private
11
+
12
+ def self.assert_existence_of_table model
13
+ unless ActiveRecord::Base.connection.table_exists? model.table_name
14
+ Class.new(ActiveRecord::Migration).create_table(model.table_name.to_sym) { |t| t.timestamps }
15
+ end
16
+ end
17
+
18
+ def self.assert_existence_of_column column_name, column_type, model
19
+ add_column_to_table(column_name, column_type, model)
20
+ update_table_column(column_name, column_type, model)
21
+ end
22
+
23
+ def self.add_column_to_table column_name, column_type, model
24
+ unless model.columns_hash[column_name.to_s]
25
+ Class.new(ActiveRecord::Migration).add_column model.table_name.to_sym, column_name, column_type
26
+ end
27
+ end
28
+
29
+ def self.update_table_column column_name, column_type, model
30
+ if model.columns_hash[column_name.to_s] && model.columns_hash[column_name.to_s].type != column_type
31
+ Class.new(ActiveRecord::Migration).change_column model.table_name, column_name, column_type
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,12 @@
1
+ module ToyAttributes
2
+
3
+ def self.load! dir_path
4
+ ActiveRecord::Base.extend ToyAttributes::Methods
5
+ Dir[dir_path].each { |file| load file }
6
+ end
7
+
8
+ class Engine < Rails::Engine
9
+ config.to_prepare { ToyAttributes.load! "#{Rails.root}/app/models/*.rb" }
10
+ end
11
+
12
+ end
@@ -0,0 +1,35 @@
1
+ module ToyAttributes::Relations
2
+
3
+ def self.included base
4
+ base.instance_eval do
5
+
6
+ alias :_has_many :has_many
7
+ alias :_has_one :has_one
8
+
9
+ def has_many model
10
+ self._has_many model
11
+ enforce_relation_with model
12
+ end
13
+
14
+ def has_one model
15
+ self._has_one model
16
+ enforce_relation_with model
17
+ end
18
+
19
+ private
20
+
21
+ def enforce_relation_with model
22
+ related = model.to_s.singularize.classify.constantize
23
+ symbol = to_s.underscore.to_sym
24
+ related.integer :"#{symbol}_id"
25
+ related.belongs_to symbol
26
+ accepts_nested_attributes_for model
27
+ attr_accessible :"#{model}_attributes"
28
+ end
29
+
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ ActiveRecord::Base.send :include, ToyAttributes::Relations
@@ -0,0 +1,3 @@
1
+ module ToyAttributes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ module ToyAttributes
2
+ COLUMN_TYPES = [:string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean, :references]
3
+ end
4
+
5
+ require "active_record"
6
+
7
+ require "toy-attributes/methods"
8
+ require "toy-attributes/observer"
9
+ require "toy-attributes/relations"
10
+
11
+ require "toy-attributes/railtie" if defined? Rails
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: toy-attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christian Mortaro
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &29994228 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *29994228
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec-rails
27
+ requirement: &29993676 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *29993676
36
+ - !ruby/object:Gem::Dependency
37
+ name: shoulda
38
+ requirement: &29993160 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *29993160
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: &29992668 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *29992668
58
+ description: Allows to assign attributes directly inside ActiveRecord models without
59
+ having to write migrations
60
+ email:
61
+ - christian.mortaro@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - lib/tasks/toy-attributes_tasks.rake
67
+ - lib/toy-attributes/methods.rb
68
+ - lib/toy-attributes/observer.rb
69
+ - lib/toy-attributes/railtie.rb
70
+ - lib/toy-attributes/relations.rb
71
+ - lib/toy-attributes/version.rb
72
+ - lib/toy-attributes.rb
73
+ - MIT-LICENSE
74
+ - Rakefile
75
+ - README.rdoc
76
+ homepage: https://github.com/Mortaro/Toy-Attributes
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
89
+ - 0
90
+ hash: 1030857843
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ segments:
98
+ - 0
99
+ hash: 1030857843
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.16
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Handles the attributes for Toy-Locomotive.
106
+ test_files: []