famili 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,86 @@
1
+ == Famili
2
+
3
+ Yet Another ObjectMother pattern implementation for rails testing
4
+
5
+ == Why
6
+
7
+ We meet some problems with factory-girl:
8
+
9
+ * We require quite complex logic for creation of test models
10
+ * We require use factories with running rails application for integration tests
11
+ * So why don't make factory just another class in rails lib ?
12
+
13
+ == Example
14
+
15
+ To define factory/mother for model User just create class in your lib directory:
16
+
17
+ `lib/famili/user.rb`
18
+
19
+ module Famili
20
+ class User < Mother
21
+ fist_name { 'nicola' }
22
+ last_name { 'nicola' }
23
+ email { "#{last_name}@mail.lv" }
24
+
25
+ def before_save(user)
26
+ #...
27
+ end
28
+
29
+ def after_create(user)
30
+ #...
31
+ end
32
+ end
33
+ end
34
+
35
+
36
+ And u can use it anywhere in tests or controllers:
37
+
38
+ Famili::User.create(:fist_name=>'Override') # create model
39
+ Famili::User.build(:fist_name=>'Override') # build model not saving
40
+ Famili::User.hash(:fist_name=>'Override') #get attributes hash
41
+
42
+ == Install
43
+
44
+ in config/environment.rb:
45
+
46
+ config.gem 'famili'
47
+
48
+ and then
49
+
50
+ sudo rake gems:install
51
+
52
+ == FEATURES
53
+
54
+
55
+ == CHANGE LOG:
56
+
57
+ 0.0.1 - created
58
+
59
+ == TODO
60
+
61
+ * generators
62
+
63
+ == LICENSE:
64
+
65
+ (The MIT License)
66
+
67
+ Copyright (c) 2010 niquola
68
+
69
+ Permission is hereby granted, free of charge, to any person obtaining
70
+ a copy of this software and associated documentation files (the
71
+ 'Software'), to deal in the Software without restriction, including
72
+ without limitation the rights to use, copy, modify, merge, publish,
73
+ distribute, sublicense, and/or sell copies of the Software, and to
74
+ permit persons to whom the Software is furnished to do so, subject to
75
+ the following conditions:
76
+
77
+ The above copyright notice and this permission notice shall be
78
+ included in all copies or substantial portions of the Software.
79
+
80
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
81
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
82
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
83
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
84
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
85
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
86
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the pg_gnostic plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'spec'
13
+ t.pattern = 'spec/**/*_spec.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the pg_gnostic plugin.'
18
+ Rake::RDocTask.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'Famili'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README.rdoc')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+
27
+ PKG_FILES = FileList[ '[a-zA-Z]*', 'generators/**/*', 'lib/**/*', 'spec/**/*' ]
28
+
29
+ require 'lib/famili'
30
+ spec = Gem::Specification.new do |s|
31
+ s.name = "famili"
32
+ s.version = Famili::VERSION
33
+ s.author = "niquola"
34
+ s.email = "niquola@gmail.com"
35
+ s.homepage = "http://github.com/niquola/famili"
36
+ s.platform = Gem::Platform::RUBY
37
+ s.summary = "Rails plugin for postgres"
38
+ s.files = PKG_FILES.to_a
39
+ s.require_path = "lib"
40
+ s.has_rdoc = false
41
+ s.extra_rdoc_files = ["README.rdoc"]
42
+ end
43
+
44
+ desc 'Turn this plugin into a gem.'
45
+ Rake::GemPackageTask.new(spec) do |pkg|
46
+ pkg.gem_spec = spec
47
+ end
48
+
@@ -0,0 +1,107 @@
1
+ module Famili
2
+ def before_save(model)
3
+ end
4
+
5
+ def after_create(model)
6
+ end
7
+
8
+ class Mother
9
+ class << self
10
+ alias :class_name :name
11
+
12
+ def parent_class=(klass)
13
+ @parent_class = klass
14
+ end
15
+
16
+ def parent_class
17
+ @parent_class
18
+ end
19
+
20
+ def inherited(child)
21
+ child.parent_class = self
22
+ end
23
+
24
+ def name(&block)
25
+ field(:name,&block)
26
+ end
27
+
28
+ def method_missing(method,&block)
29
+ field(method,&block)
30
+ end
31
+
32
+ def attribures
33
+ @attribures||=parent_class && parent_class.attribures
34
+ @attribures||=[]
35
+ @attribures.uniq!
36
+ @attribures
37
+ end
38
+
39
+ def field(method,&block)
40
+ attribures<< method
41
+ #puts "define_method #{method} #{self}"
42
+ define_method(method,&block) if block_given?
43
+ end
44
+
45
+ def create(opts={})
46
+ mother,model = _build(opts)
47
+ model.save
48
+ mother.after_create(model)
49
+ model
50
+ end
51
+
52
+ def build(opts={})
53
+ mother,model = _build(opts)
54
+ model
55
+ end
56
+
57
+ def hash
58
+ mother,model = _build
59
+ model.attribures
60
+ end
61
+
62
+
63
+ def _build(opts)
64
+ mother = new
65
+ model = model_class.new
66
+ opts.symbolize_keys!
67
+ passed_attrs = opts.keys || []
68
+
69
+ mother.instance_eval do
70
+ singleton = class <<self;self;end;
71
+ passed_attrs.each do |attr|
72
+ value = opts[attr]
73
+ if value.class == Proc
74
+ singleton.send(:define_method,attr,&value)
75
+ else
76
+ singleton.send(:define_method,attr) do
77
+ opts[attr]
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ fields = ( passed_attrs + attribures).uniq
84
+ fields.each do |attr|
85
+ attr = attr.to_sym
86
+ value = opts.key?(attr) ? opts[attr] : mother.send(attr)
87
+ model.send(:"#{attr}=",value)
88
+ end
89
+ mother.before_save(model)
90
+ [mother,model]
91
+ end
92
+
93
+ private
94
+
95
+ def model_class(klass=nil)
96
+ if klass
97
+ @model_class = klass
98
+ return
99
+ end
100
+
101
+ @model_class||= class_name.to_s.split('::')[1..-1].inject(Object) do |mod,const|
102
+ mod.const_get(const)
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
data/lib/famili.rb ADDED
@@ -0,0 +1,5 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+ module Famili
3
+ VERSION='0.0.1'
4
+ autoload :Mother,'famili/mother'
5
+ end
data/spec/database.yml ADDED
@@ -0,0 +1,6 @@
1
+ adapter: postgresql
2
+ host: localhost
3
+ database: famili
4
+ username: postgres
5
+ password: postgres
6
+ encoding: utf8
data/spec/db/schema.rb ADDED
@@ -0,0 +1,15 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :users do |t|
3
+ t.string :last_name, :limit => 25
4
+ t.string :first_name, :limit => 25
5
+ t.string :middle_name, :limit => 25
6
+ t.string :name, :limit => 25
7
+ t.string :login, :limit => 40
8
+ t.string :email, :limit => 100
9
+ t.string :crypted_password, :limit => 40
10
+ t.string :salt, :limit => 40
11
+ t.datetime :last_login_datetime
12
+ t.datetime :deleted_at
13
+ t.timestamps
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe Famili do
4
+ before :all do
5
+ TestDbUtils.ensure_schema
6
+ end
7
+
8
+ after :all do
9
+ #TestDbUtils.drop_database
10
+ end
11
+
12
+ class User < ActiveRecord::Base
13
+ end
14
+
15
+ module Famili
16
+ class User < Mother
17
+ last_name { 'nicola' }
18
+
19
+ def before_save(user)
20
+ user.first_name = 'first_name'
21
+ end
22
+
23
+ def after_create(model)
24
+ end
25
+ end
26
+ end
27
+
28
+ it "should create model" do
29
+ nicola = Famili::User.create
30
+ nicola.class.should == User
31
+ nicola.last_name.should == 'nicola'
32
+ nicola.first_name.should == 'first_name'
33
+
34
+ ivan = Famili::User.create(:name=>'ivan')
35
+ ivan.name.should == 'ivan'
36
+ end
37
+ end
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+
4
+
5
+ def path(path)
6
+ File.join(File.dirname(__FILE__),path)
7
+ end
8
+
9
+ $:.unshift(path('../lib'))
10
+ require 'famili'
11
+
12
+ class TestDbUtils
13
+ class<< self
14
+ def config
15
+ @config ||= YAML::load(IO.read(path('/database.yml'))).symbolize_keys
16
+ end
17
+
18
+ #create test database
19
+ def ensure_test_database
20
+ connect_to_test_db
21
+ rescue
22
+ create_database
23
+ end
24
+
25
+ def load_schema
26
+ ensure_test_database
27
+ load(path('db/schema.rb'))
28
+ end
29
+
30
+ def ensure_schema
31
+ load_schema
32
+ rescue
33
+ puts "tests database exists: skip schema loading"
34
+ end
35
+
36
+ def create_database
37
+ connect_to_postgres_db
38
+ ActiveRecord::Base.connection.create_database(config[:database], config)
39
+ connect_to_test_db
40
+ rescue
41
+ $stderr.puts $!, *($!.backtrace)
42
+ $stderr.puts "Couldn't create database for #{config.inspect}"
43
+ end
44
+
45
+ def connect_to_test_db
46
+ ActiveRecord::Base.establish_connection(config)
47
+ ActiveRecord::Base.connection
48
+ end
49
+
50
+ def connect_to_postgres_db
51
+ ActiveRecord::Base.establish_connection(config.merge(:database => 'postgres', :schema_search_path => 'public'))
52
+ end
53
+
54
+ def drop_database
55
+ connect_to_postgres_db
56
+ ActiveRecord::Base.connection.drop_database config[:database]
57
+ end
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: famili
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - niquola
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-29 00:00:00 +04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: niquola@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README.rdoc
30
+ files:
31
+ - Rakefile
32
+ - README.rdoc
33
+ - lib/famili.rb
34
+ - lib/famili/mother.rb
35
+ - spec/famili_spec.rb
36
+ - spec/spec_helper.rb
37
+ - spec/database.yml
38
+ - spec/db/schema.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/niquola/famili
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Rails plugin for postgres
73
+ test_files: []
74
+