falcor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzhjNWU1YTY3NzA0OWRiZDk2NDZmZTRlZTQ2NGQ1OTE2YTc3NDE4YQ==
5
+ data.tar.gz: !binary |-
6
+ MmU5ZWUzMTYxM2Y1MzgxMDI2ZDc1NjM4NzJmMWEzY2RkYjc5MjY5MQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YzNkYzkxZjdjYjhlODVmNzc0Y2E0ZTYzNGE3NmZiNWQ4NDliZjUwNDkzNjBm
10
+ NzZiZjQxOGJhYWZlMWNjYTdmNjI4OGEzYmRmNDcxOTQ3OTIzNWFkMTMwODZj
11
+ YTBjYTkzOGU4MjdhOWJiMWFhNWI4Mzk4NjllMGZkOTUyYjgwNDM=
12
+ data.tar.gz: !binary |-
13
+ MTRhNTMwMjYyZDdmYTcxNjZlNDIxNDgwNzg2NTY3Yzk3NjdkNDAyZGQwMjU5
14
+ ZmNlMGUzN2YyMzQzNTk1YTQ5NGQ3Njk3MDJiYWZhN2JmYTg4Yzc1ZmM2MmZi
15
+ MmRiNmJjMjg2YWNiNjRjZWI3NDM5YzU3ODMwYzljNTgyODg5MDc=
data/.gitignore ADDED
@@ -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 falcor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Matt Gauger
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.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Falcor
2
+
3
+ ![Falcor YEAH](http://epicyearproject.files.wordpress.com/2011/05/bastian-falcor-whoop-490.gif)
4
+
5
+ Falcor is intended to solve several problems, namely, to be able to quickly define fixture data and use it for things like example JSON web services, mocking out tests, and seeding test data into document stores. The DSL should be very familiar to users of tools like FactoryGirl, but it doesn't require the class to exist before you define the factory.
6
+
7
+ ### TODO:
8
+
9
+ * Traits
10
+ * Port over tests
11
+ * Examples of usage for README
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'falcor'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install falcor
26
+
27
+ ## Usage
28
+
29
+ Here's how you'd define some factories, create an instance of the new `user` factory, and then turn it into JSON:
30
+
31
+ ```ruby
32
+ Falcor::Fabricator.define :blog_post do
33
+ field :title, "My post"
34
+ field :body, "Lots of text"
35
+ end
36
+
37
+ Falcor::Fabricator.define :user do
38
+ field :email, "user@example.com"
39
+ field :full_name, "Matt Gauger"
40
+
41
+ create_list :blog_posts, 2, Factory(:blog_post)
42
+ end
43
+
44
+ user = Factory :user
45
+
46
+ user.to_json
47
+ #=> {:email=>"user@example.com",
48
+ :full_name=>"Matt Gauger",
49
+ :blog_posts=>
50
+ [{:title=>"My post", :body=>"Lots of text"},
51
+ {:title=>"My post", :body=>"Lots of text"}]}
52
+ ```
53
+
54
+ You can also override defaults:
55
+
56
+ ```ruby
57
+ post = Factory :blog_post, title: "My really cool post"
58
+
59
+ post.title
60
+ #=> "My really cool blog post"
61
+ ```
62
+
63
+ And much more! (To be continued...)
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/falcor.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'falcor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "falcor"
8
+ spec.version = Falcor::VERSION
9
+ spec.authors = ["Matt Gauger"]
10
+ spec.email = ["matt.gauger@gmail.com"]
11
+ spec.description = "Quickly build example data"
12
+ spec.summary = "Quickly build example data"
13
+ spec.homepage = "https://github.com/mathias/falcor"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport', '~> 4.0.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "pry-debugger"
27
+ end
data/lib/falcor.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "falcor/version"
2
+
3
+ require 'falcor/class_attrs'
4
+ require 'falcor/define_dsl'
5
+ require 'falcor/fabricator'
6
+ require 'falcor/factory'
7
+
8
+ module Falcor
9
+ end
10
+
11
+ def Factory(name, overrides={}, &block)
12
+ Falcor::Fabricator.create(name, overrides, block)
13
+ end
@@ -0,0 +1,24 @@
1
+ module Falcor
2
+ module ClassAttrs
3
+ class << self
4
+ def included(base)
5
+ base.extend ClassMethods
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+ def class_attr(attr_name)
11
+ (class << self; self; end).instance_eval {
12
+ define_method attr_name.intern do
13
+ instance_variable_get("@#{attr_name}")
14
+ end
15
+
16
+ define_method "#{attr_name}=".intern do |val|
17
+ instance_variable_set("@#{attr_name}", val)
18
+ end
19
+ }
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,82 @@
1
+ module Falcor
2
+ module DefineDsl
3
+ class << self
4
+ def included(base)
5
+ base.extend ClassMethods
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+ def allow(attr)
11
+ self.fields = (self.fields || []).push(attr)
12
+
13
+ default_blk = Proc.new { return self.instance_variable_get("@#{attr}".to_sym) }
14
+ self.send(:define_method, attr, default_blk)
15
+
16
+ default_assignment_blk = Proc.new do |val|
17
+ self.instance_variable_set("@#{attr}".to_sym, val)
18
+ end
19
+
20
+ self.send(:define_method, "#{attr}=", default_assignment_blk)
21
+ end
22
+
23
+ def field(attr, default)
24
+ self.fields = (self.fields || []).push(attr)
25
+
26
+ default_blk = Proc.new { return self.instance_variable_get("@#{attr}".to_sym) || default }
27
+ self.send(:define_method, attr, default_blk)
28
+
29
+ default_assignment_blk = Proc.new do |val|
30
+ self.instance_variable_set("@#{attr}".to_sym, val)
31
+ end
32
+
33
+ self.send(:define_method, "#{attr}=", default_assignment_blk)
34
+ end
35
+
36
+ def association(attr, model)
37
+ self.associations = (self.associations || []).push(attr)
38
+
39
+ default_blk = Proc.new { return self.instance_variable_get("@#{attr}".to_sym) || model }
40
+ self.send(:define_method, attr, default_blk)
41
+
42
+ default_assignment_blk = Proc.new do |val|
43
+ if val.class == Hash
44
+ association = self.send(attr)
45
+ val.each do |k,v|
46
+ association.send(:"#{k}=", v)
47
+ end
48
+ else
49
+ self.instance_variable_set("@#{attr}".to_sym, val)
50
+ end
51
+ end
52
+ self.send(:define_method, "#{attr}=", default_assignment_blk)
53
+ end
54
+
55
+ def create_list(attr, count, model)
56
+ self.lists = (self.lists || []).push(attr)
57
+
58
+ models = []
59
+ count.times do
60
+ models << model
61
+ end
62
+
63
+ default_blk = Proc.new { return self.instance_variable_get("@#{attr}".to_sym) || models }
64
+ self.send(:define_method, attr, default_blk)
65
+
66
+ default_assignment_blk = Proc.new do |val|
67
+ raise "Invalid association list" unless val.class == Array
68
+
69
+ factory_name = ActiveSupport::Inflector.singularize(attr).to_sym
70
+
71
+ overrides = val.map do |override|
72
+ Factory(factory_name, override)
73
+ end
74
+
75
+ self.instance_variable_set("@#{attr}".to_sym, overrides)
76
+ end
77
+
78
+ self.send(:define_method, "#{attr}=", default_assignment_blk)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,47 @@
1
+ require 'active_support/core_ext'
2
+
3
+ module Falcor
4
+ class Fabricator
5
+ class << self
6
+ attr_accessor :factories
7
+
8
+ def define(name, options = {}, &block)
9
+ factories[name] = self.new({ :name => name, :block => block }.merge(options))
10
+ end
11
+
12
+ def create(name, overrides, block)
13
+ raise "No factory defined: [#{name}]" unless factories.has_key?(name)
14
+
15
+ factories[name].create(overrides)
16
+ end
17
+ end
18
+
19
+ def create(overrides)
20
+ @options = @definition.merge(overrides)
21
+ @options[:super] = Factory if @options[:super].nil?
22
+ klass = create_class
23
+ klass.create_methods(@options[:block])
24
+
25
+ klass.new(overrides)
26
+ end
27
+
28
+ private
29
+
30
+ def initialize(options)
31
+ @definition = options
32
+ end
33
+
34
+ def create_class
35
+ Object.send(:remove_const, class_name) rescue nil
36
+ klass = Object.const_set class_name, Class.new(@options[:super])
37
+
38
+ klass
39
+ end
40
+
41
+ def class_name
42
+ (@options[:class] || @options[:name]).to_s.camelize
43
+ end
44
+
45
+ self.factories = {}
46
+ end
47
+ end
@@ -0,0 +1,58 @@
1
+ module Falcor
2
+ class Factory
3
+ include ClassAttrs
4
+ include DefineDsl
5
+
6
+ class_attr :fields
7
+ class_attr :lists
8
+ class_attr :associations
9
+
10
+ class << self
11
+ def create_methods(blk)
12
+ instance_eval(&blk)
13
+ end
14
+ end
15
+
16
+ def initialize(overrides)
17
+ unless overrides.empty?
18
+ overrides.each do |k,v|
19
+ self.send(:"#{k}=", v)
20
+ end
21
+ end
22
+ end
23
+
24
+ def fields
25
+ self.class.fields || []
26
+ end
27
+
28
+ def associations
29
+ self.class.associations || []
30
+ end
31
+
32
+ def lists
33
+ self.class.lists || []
34
+ end
35
+
36
+ def to_json
37
+ json = {}
38
+
39
+ fields.each do |field|
40
+ value = self.send(field)
41
+ unless value.nil?
42
+ json[field] = self.send(field)
43
+ end
44
+ end
45
+
46
+ associations.each do |field|
47
+ json[field] = self.send(field).send(:to_json)
48
+ end
49
+
50
+ lists.each do |field|
51
+ json[field] = self.send(field).map { |m| m.to_json }
52
+ end
53
+
54
+ json
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,3 @@
1
+ module Falcor
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: falcor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Gauger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-debugger
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Quickly build example data
84
+ email:
85
+ - matt.gauger@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - falcor.gemspec
96
+ - lib/falcor.rb
97
+ - lib/falcor/class_attrs.rb
98
+ - lib/falcor/define_dsl.rb
99
+ - lib/falcor/fabricator.rb
100
+ - lib/falcor/factory.rb
101
+ - lib/falcor/version.rb
102
+ homepage: https://github.com/mathias/falcor
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.1.9
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Quickly build example data
126
+ test_files: []