cobble 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 markbates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,76 @@
1
+ =Cobble
2
+
3
+ A very simple and easy library to help facilitate the building and creating of objects. They can be any type of objects, although if you want to 'persist' them they need to respond to <code>.save!</code>. Cobble is there to put YOU in charge of how your objects get created. There's very little magic, it's just there to help make your life a little bit easier.
4
+
5
+ ==Examples:
6
+
7
+ ===Defining Attributes:
8
+
9
+ Attributes can be defined to be used later in your cobblers, or they can be used on their own for other use.
10
+
11
+ Cobble.define_attributes(:user, :name => 'Mark Bates')
12
+ Cobble.define_attributes(:another_user, :attributes_for => :user, :email => lambda{fake(:email)})
13
+ Cobble.define_attributes(:yet_another_user, :attributes_for => :user, :email => lambda{rand})
14
+
15
+ # Test:
16
+ Cobble.attributes_for(:user).should == {:name => 'Mark Bates'}
17
+ Cobble.attributes_for(:another_user).should == {:name => 'Mark Bates', :email => 'bob@example.com'}
18
+ Cobble.attributes_for(:yet_another_user).should == {:name => 'Mark Bates', :email => 0.123456789}
19
+
20
+ ===Defining a Cobbler:
21
+
22
+ A cobbler is what actually builds your object(s). A cobble is fully defined by you, Cobble just helps to give you the proper attributes and options for your object.
23
+
24
+ Cobble.define(:user) do
25
+ User.new(options)
26
+ end
27
+ Cobble.define(:another_user) do
28
+ user = User.new(options)
29
+ user.url = 'http://www.metabates.com'
30
+ user
31
+ end
32
+ Cobble.define(:yet_another_user) do
33
+ User.new(options)
34
+ end
35
+
36
+ # Test
37
+
38
+ # build and save:
39
+ user = Cobble.user # or Cobble.create(:user)
40
+ user.name.should == 'Mark Bates'
41
+ user.should_not be_new_record
42
+
43
+ # just build:
44
+ user = Cobble.build_user # or Cobble.build(:user)
45
+ user.name.should == 'Mark Bates'
46
+ user.should be_new_record
47
+
48
+ # build and save:
49
+ user = Cobble.another_user # or Cobble.create(:another_user)
50
+ user.name.should == 'Mark Bates'
51
+ user.email.should == 'bob@example.com'
52
+ user.url.should == 'http://www.metabates.com'
53
+ user.should_not be_new_record
54
+
55
+ # just build:
56
+ user = Cobble.build_another_user # or Cobble.build(:another_user)
57
+ user.name.should == 'Mark Bates'
58
+ user.email.should == 'bob@example.com'
59
+ user.url.should == 'http://www.metabates.com'
60
+ user.should be_new_record
61
+
62
+ # build and save:
63
+ user = Cobble.another_user(:name => 'Bob Smith') # or Cobble.create(:another_user)
64
+ user.name.should == 'Bob Smith'
65
+ user.email.should == 'bob@example.com'
66
+ user.url.should == 'http://www.metabates.com'
67
+ user.should_not be_new_record
68
+
69
+ # just build:
70
+ user = Cobble.build_another_user(:name => 'Bob Smith') # or Cobble.build(:another_user)
71
+ user.name.should == 'Bob Smith'
72
+ user.email.should == 'bob@example.com'
73
+ user.url.should == 'http://www.metabates.com'
74
+ user.should be_new_record
75
+
76
+ Cobblers will try and use a matching set of attributes, if defined, as the basis for the options available to your cobbler. You can, of course, pass in different options at runtime to override these defaults.
data/lib/cobble.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'singleton'
2
+ require 'faker'
3
+
4
+ Dir.glob(File.join(File.dirname(__FILE__), 'cobble', '**/*.rb')).each do |f|
5
+ require File.expand_path(f)
6
+ end
7
+
8
+ def Cobble(name, *args, &block)
9
+ Cobble.create(name, *args, &block)
10
+ end
11
+
12
+ def Factory(name, *args, &block)
13
+ puts "*** WARNING *** You should migrate over to using the proper Cobble methods as this won't be here forever!"
14
+ Cobble.create(name, *args, &block)
15
+ end
@@ -0,0 +1,72 @@
1
+ class Cobble
2
+ class Binding
3
+ attr_accessor :name
4
+ attr_accessor :type
5
+ attr_accessor :options
6
+
7
+ def initialize(name, options = {}) # :nodoc:
8
+ self.name = name
9
+ self.type = :build
10
+ self.options = {}
11
+ parse_options(options)
12
+
13
+ self.attributes_for(name)
14
+
15
+ attr_for = self.options.delete(:attributes_for)
16
+ if attr_for
17
+ self.attributes_for(attr_for)
18
+ end
19
+ end
20
+
21
+ def fake(name, *args)
22
+ Cobble::Fakes.execute(name.to_sym, *args)
23
+ end
24
+
25
+ def execute(&block)
26
+ eval_options
27
+ instance_eval(&block)
28
+ end
29
+
30
+ def attributes_for(names)
31
+ [names].flatten.each do |name|
32
+ self.options = Cobble.attribs(name).dup.merge(self.options)
33
+ end
34
+ end
35
+
36
+ def method_missing(sym, *args, &block)
37
+ unless self.options[sym]
38
+ if block_given?
39
+ self.options[sym] = yield
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+ def parse_options(options)
46
+ case options
47
+ when Hash
48
+ self.options = options
49
+ when Array
50
+ if options.empty?
51
+ self.options = {}
52
+ else
53
+ options.each do |val|
54
+ if val.is_a?(Hash)
55
+ self.options = val
56
+ break
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ def eval_options
64
+ self.options.each do |key, val|
65
+ if val.is_a?(Proc)
66
+ self.options[key] = instance_eval(&val)
67
+ end
68
+ end
69
+ end
70
+
71
+ end # Binding
72
+ end # Cobble
@@ -0,0 +1,87 @@
1
+ class Cobble
2
+ include Singleton
3
+
4
+ attr_accessor :factories
5
+ attr_accessor :attributes
6
+
7
+ def initialize # :nodoc:
8
+ self.reset!
9
+ end
10
+
11
+ def reset!
12
+ self.factories = {}
13
+ self.attributes = {}
14
+ end
15
+
16
+ def define(factory_name, options = {}, &block)
17
+ self.factories[factory_name.to_sym] = [options, block]
18
+ end
19
+
20
+ def define_attributes(name, options = {})
21
+ self.attributes[name.to_sym] = options
22
+ end
23
+
24
+ def attribs(name)
25
+ self.attributes[name.to_sym] || {}
26
+ end
27
+
28
+ def build(factory, *args, &block)
29
+ return execute(factory.to_sym, :build, *args)
30
+ end
31
+
32
+ def create(factory, *args, &block)
33
+ res = self.execute(factory.to_sym, :create, *args)
34
+ res.save!
35
+ return res
36
+ end
37
+
38
+ def attributes_for(factory, *args, &block)
39
+ fb = self.cobble_binding(factory.to_sym, :build, *args)
40
+ fb.execute {}
41
+ return fb.options
42
+ end
43
+
44
+ def method_missing(sym, *args, &block)
45
+ meth = sym.to_s
46
+ case meth
47
+ when /^build_(.+)$/
48
+ return self.build($1, *args, &block)
49
+ when /^attributes_for_(.+)$/
50
+ return self.attributes_for($1, *args, &block)
51
+ when /^create_(.+)$/
52
+ return self.create($1, *args, &block)
53
+ else
54
+ return self.create(sym, *args, &block)
55
+ end
56
+ end
57
+
58
+ class << self
59
+
60
+ def method_missing(sym, *args, &block)
61
+ Cobble.instance.send(sym, *args, &block)
62
+ end
63
+
64
+ end
65
+
66
+ protected
67
+ def cobble_binding(name, type, *args)
68
+ factory = self.factories[name.to_sym]
69
+ raise ArgumentError.new("No Factory defined for '#{name}'!!!") if factory.nil?
70
+
71
+ fb = Cobble::Binding.new(name.to_sym, *args)
72
+ fb.type = type
73
+ options = (factory[0] || {}).dup
74
+ attr_for = options.delete(:attributes_for)
75
+ if attr_for
76
+ fb.attributes_for(attr_for)
77
+ end
78
+ return fb
79
+ end
80
+
81
+ def execute(name, type, *args)
82
+ factory = self.factories[name.to_sym]
83
+ fb = cobble_binding(name, type, *args)
84
+ return fb.execute(&factory[1])
85
+ end
86
+
87
+ end
@@ -0,0 +1,11 @@
1
+ class Cobble
2
+ module Errors # :nodoc:
3
+ class NoFakeRegistered < StandardError
4
+
5
+ def initialize(name) # :nodoc:
6
+ super("No fake has been registered for '#{name}'!")
7
+ end
8
+
9
+ end # NoFakeRegistered
10
+ end # Errors
11
+ end # Cobble
@@ -0,0 +1,82 @@
1
+ class Cobble
2
+ class Fakes
3
+ include Singleton
4
+
5
+ attr_accessor :list
6
+
7
+ def initialize
8
+ self.reset!
9
+ end
10
+
11
+ def reset!
12
+ self.list = {}
13
+ end
14
+
15
+ def add(name, &block)
16
+ self.list[name.to_sym] = block
17
+ end
18
+
19
+ def alias(from, to)
20
+ self.list[from.to_sym] = Cobble::Fakes::Alias.new(to)
21
+ end
22
+
23
+ def execute(name, *args)
24
+ block = self.list[name.to_sym]
25
+ if block.is_a?(Cobble::Fakes::Alias)
26
+ return execute(block.to, *args)
27
+ end
28
+ if block
29
+ return block.call(*args)
30
+ end
31
+ raise Cobble::Errors::NoFakeRegistered.new(name)
32
+ end
33
+
34
+ class << self
35
+ def method_missing(sym, *args, &block)
36
+ Cobble::Fakes.instance.send(sym, *args, &block)
37
+ end
38
+ end # class << self
39
+
40
+ private
41
+ class Alias
42
+ attr_accessor :to
43
+ def initialize(to)
44
+ self.to = to
45
+ end
46
+ end
47
+
48
+ end # Fakes
49
+ end # Cobble
50
+
51
+ %w{first_name last_name name prefix suffix}.each do |m|
52
+ eval("Cobble::Fakes.add(:#{m}) {|*args| Faker::Name.#{m}(*args)}")
53
+ end
54
+
55
+ %w{city city_prefix city_suffix secondary_address street_address street_name
56
+ street_suffix uk_country uk_county uk_postcode us_state us_state_abbr zip_code}.each do |m|
57
+ eval("Cobble::Fakes.add(:#{m}) {|*args| Faker::Address.#{m}(*args)}")
58
+ end
59
+
60
+ %w{bs catch_phrase name suffix}.each do |m|
61
+ eval("Cobble::Fakes.add(:#{m}) {|*args| Faker::Company.#{m}(*args)}")
62
+ end
63
+
64
+ %w{domain_name domain_suffix domain_word email free_email user_name}.each do |m|
65
+ eval("Cobble::Fakes.add(:#{m}) {|*args| Faker::Internet.#{m}(*args)}")
66
+ end
67
+
68
+ %w{paragraph paragraphs sentence sentences words}.each do |m|
69
+ eval("Cobble::Fakes.add(:#{m}) {|*args| Faker::Lorem.#{m}(*args)}")
70
+ end
71
+
72
+ %w{phone_number}.each do |m|
73
+ eval("Cobble::Fakes.add(:#{m}) {|*args| Faker::PhoneNumber.#{m}(*args)}")
74
+ end
75
+
76
+ Cobble::Fakes.add(:url) {|*args| 'http://' + Faker::Internet.domain_name(*args)}
77
+ Cobble::Fakes.add(:lorem) {|*args| Faker::Lorem.paragraphs(*args).join("\n")}
78
+ Cobble::Fakes.add(:name) {|*args| Faker::Name.first_name + ' ' + Faker::Name.last_name}
79
+ Cobble::Fakes.add(:birth_date) {|*args| (rand(80) + 13).years.ago}
80
+ Cobble::Fakes.add(:title) {|*args| Faker::Company.catch_phrase.slice(0..250)}
81
+ Cobble::Fakes.alias(:body, :lorem)
82
+ Cobble::Fakes.alias(:full_name, :name)
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cobble
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - markbates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-15 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: faker
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: "cobble was developed by: markbates"
26
+ email: development@metabates.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - LICENSE
34
+ files:
35
+ - lib/cobble/binding.rb
36
+ - lib/cobble/cobble.rb
37
+ - lib/cobble/errors.rb
38
+ - lib/cobble/fakes.rb
39
+ - lib/cobble.rb
40
+ - README
41
+ - LICENSE
42
+ has_rdoc: true
43
+ homepage: http://www.metabates.com
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: magrathea
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: cobble
70
+ test_files: []
71
+