depot 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
@@ -0,0 +1,6 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
@@ -0,0 +1,3 @@
1
+ = Depot
2
+
3
+ Documentation.
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "depot/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "depot"
7
+ s.version = Depot::VERSION
8
+ s.authors = ["Rainer Borene"]
9
+ s.email = ["me@rainerborene.com"]
10
+ s.homepage = "https://github.com/rainerborene/depot"
11
+ s.summary = %q{Populate your database in an elegant way}
12
+ s.description = %q{A simple DSL for defining database seeds in Rails.}
13
+
14
+ s.rubyforge_project = "depot"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency "active_support" , ">= 3.0.0"
22
+
23
+ s.add_development_dependency "rake" , "~> 0.9"
24
+ s.add_development_dependency "rspec" , "~> 2.8"
25
+ s.add_development_dependency "pry" , "~> 0.9"
26
+ s.add_development_dependency "guard-rspec" , "~> 0.6"
27
+ end
@@ -0,0 +1,11 @@
1
+ require "depot/base"
2
+ require "depot/context"
3
+ require "depot/version"
4
+
5
+ module Depot
6
+ def self.construct(&block)
7
+ Base.new.tap do |depot|
8
+ depot.instance_eval(&block)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,46 @@
1
+ require "set"
2
+ require "logger"
3
+
4
+ module Depot
5
+ class Base
6
+ attr_reader :entries, :models
7
+ attr_accessor :logging
8
+
9
+ def initialize
10
+ @entries = {}
11
+ @logging = false
12
+ @models = Set.new
13
+
14
+ if defined?(ActiveRecord)
15
+ subclasses = ActiveRecord::Base.send(:subclasses).map do |m|
16
+ m.name.downcase.pluralize.gsub(/::/, "_").to_sym
17
+ end
18
+ @models.merge(subclasses)
19
+ end
20
+ end
21
+
22
+ def logging(value = true)
23
+ @logging = value
24
+
25
+ if defined?(ActiveRecord)
26
+ @buffered ||= ActiveRecord::Base.logger
27
+ ActiveRecord::Base.logger = @logging ? Logger.new(STDOUT) : @buffered
28
+ end
29
+ end
30
+
31
+ def inject(*args)
32
+ @models.merge args.map(&:to_sym)
33
+ end
34
+
35
+ def method_missing(name, &block)
36
+ if @models.include? name and block_given?
37
+ klass = name.to_s.gsub("_", "::").singularize.classify.constantize
38
+ Context.new(klass, self).instance_eval(&block)
39
+ elsif entries.has_key? name
40
+ @entries[name]
41
+ else
42
+ super
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,37 @@
1
+ require "active_support/core_ext/hash"
2
+
3
+ module Depot
4
+ class Context
5
+ attr_reader :klass, :base
6
+
7
+ def initialize(klass, base)
8
+ @klass = klass
9
+ @base = base
10
+ end
11
+
12
+ def finder(criteria)
13
+ @finder = criteria
14
+ end
15
+
16
+ def create(attributes = {})
17
+ # remove method reference symbol
18
+ key = attributes[:as]
19
+ attributes.except! :as
20
+
21
+ # alternate syntax to define attributes
22
+ if block_given?
23
+ klass_instance = @klass.new
24
+ yield klass_instance
25
+ attributes = klass_instance.attributes
26
+ end
27
+
28
+ # finder criteria
29
+ criteria = @finder || attributes.keys.first
30
+
31
+ # okey, let's do it
32
+ entry = @klass.send("find_or_create_by_#{criteria}", attributes)
33
+ @base.entries[key] = entry if key
34
+ entry
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Depot
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+
3
+ describe Depot::Base do
4
+
5
+ subject { described_class.new }
6
+
7
+ before do
8
+ User.stub!(:find_or_create_by_name)
9
+ end
10
+
11
+ describe "#initialize" do
12
+ it "should initialize entries hash" do
13
+ subject.entries.should be_empty
14
+ subject.entries.should be_instance_of Hash
15
+ end
16
+
17
+ it "should disable logging by default" do
18
+ subject.instance_variable_get(:@logging).should be_false
19
+ end
20
+ end
21
+
22
+ describe "#inject" do
23
+ it "should append a new symbol to the models set" do
24
+ subject.inject :jobs
25
+ subject.models.should include :jobs
26
+ end
27
+ end
28
+
29
+ describe "#method_missing" do
30
+ before { subject.inject :users }
31
+
32
+ it "should listen to model names" do
33
+ lambda {
34
+ subject.users do
35
+ # something
36
+ end
37
+ }.should_not raise_error
38
+ end
39
+
40
+ it "should listen to entries key name" do
41
+ subject.users do
42
+ create({ name: "Jonh Doe", email: "jonh@doe.com", as: :jonh })
43
+ end
44
+ subject.entries.should have_key :jonh
45
+ lambda { subject.jonh }.should_not raise_error
46
+ end
47
+
48
+ it "should fallback to the default behavior" do
49
+ lambda { subject.say_hello }.should raise_error
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ describe Depot::Context do
4
+
5
+ before do
6
+ User.stub!(:find_or_create_by_name)
7
+ @base = Depot::Base.new
8
+ end
9
+
10
+ subject { described_class.new(User, @base) }
11
+
12
+ describe "#initialize" do
13
+ its(:klass) { should eql User }
14
+ its(:base) { should eql @base }
15
+ end
16
+
17
+ describe "#finder" do
18
+ it "should change the finder criteria" do
19
+ attributes = { email: "jonh@doe.com" }
20
+ User.should_receive(:find_or_create_by_email).with(attributes)
21
+ subject.finder(:email)
22
+ subject.create(attributes)
23
+ end
24
+ end
25
+
26
+ describe "#create" do
27
+ it "should create a new entry given a hash" do
28
+ subject.create({ name: "Jonh Doe", email: "jonh@doe.com", :as => :jonh_doe })
29
+ @base.entries.should_not be_empty
30
+ @base.entries.should have_key :jonh_doe
31
+ end
32
+
33
+ it "should create a new entry given a block" do
34
+ subject.create :as => :jonh_doe do |p|
35
+ p.name = "Jonh Doe"
36
+ p.email = "jonh@doe.com"
37
+ end
38
+
39
+ @base.entries.should_not be_empty
40
+ @base.entries.should have_key :jonh_doe
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe Depot do
4
+
5
+ let(:version) { Depot::VERSION }
6
+
7
+ it "should be a triplet" do
8
+ version.should match(/^\d+\.\d+\.\d+$/)
9
+ end
10
+
11
+ end
@@ -0,0 +1,15 @@
1
+ require "bundler"
2
+ Bundler.setup(:default, :development)
3
+ Bundler.require
4
+
5
+ require "depot"
6
+
7
+ class User
8
+ attr_accessor :name, :email
9
+
10
+ def attributes
11
+ { :name => name, :email => email }
12
+ end
13
+ end
14
+
15
+ Dir["spec/support/**/*.rb"].each {|file| require file}
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: depot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rainer Borene
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_support
16
+ requirement: &2154230020 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2154230020
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2154229220 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '0.9'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2154229220
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &2154222220 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.8'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2154222220
47
+ - !ruby/object:Gem::Dependency
48
+ name: pry
49
+ requirement: &2154221660 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2154221660
58
+ - !ruby/object:Gem::Dependency
59
+ name: guard-rspec
60
+ requirement: &2154221140 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '0.6'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *2154221140
69
+ description: A simple DSL for defining database seeds in Rails.
70
+ email:
71
+ - me@rainerborene.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Guardfile
79
+ - README.rdoc
80
+ - Rakefile
81
+ - depot.gemspec
82
+ - lib/depot.rb
83
+ - lib/depot/base.rb
84
+ - lib/depot/context.rb
85
+ - lib/depot/version.rb
86
+ - spec/depot/base_spec.rb
87
+ - spec/depot/context_spec.rb
88
+ - spec/depot/version_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: https://github.com/rainerborene/depot
91
+ licenses: []
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ segments:
103
+ - 0
104
+ hash: 3417339911530143929
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ segments:
112
+ - 0
113
+ hash: 3417339911530143929
114
+ requirements: []
115
+ rubyforge_project: depot
116
+ rubygems_version: 1.8.10
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: Populate your database in an elegant way
120
+ test_files:
121
+ - spec/depot/base_spec.rb
122
+ - spec/depot/context_spec.rb
123
+ - spec/depot/version_spec.rb
124
+ - spec/spec_helper.rb