mongoid_model_builder 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.project +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +84 -0
- data/Rakefile +2 -0
- data/lib/mongoid_model_builder/mongoid_model_builder.rb +110 -0
- data/lib/mongoid_model_builder/version.rb +5 -0
- data/lib/mongoid_model_builder.rb +3 -0
- data/mongoid_model_builder.gemspec +19 -0
- metadata +72 -0
data/.gitignore
ADDED
data/.project
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<projectDescription>
|
3
|
+
<name>model_builder</name>
|
4
|
+
<comment></comment>
|
5
|
+
<projects>
|
6
|
+
</projects>
|
7
|
+
<buildSpec>
|
8
|
+
<buildCommand>
|
9
|
+
<name>com.aptana.ide.core.unifiedBuilder</name>
|
10
|
+
<arguments>
|
11
|
+
</arguments>
|
12
|
+
</buildCommand>
|
13
|
+
</buildSpec>
|
14
|
+
<natures>
|
15
|
+
<nature>com.aptana.ruby.core.rubynature</nature>
|
16
|
+
<nature>com.aptana.projects.webnature</nature>
|
17
|
+
</natures>
|
18
|
+
</projectDescription>
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 TODO: Write your name
|
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,84 @@
|
|
1
|
+
# Mongoid::ModelBuilder
|
2
|
+
|
3
|
+
mongoid_model_builder dynamically creates Mongoid model classes following configuration hash specifications
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mongoid_model_builder'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mongoid_model_builder
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Create a config file for your models :
|
22
|
+
|
23
|
+
# config/models.rb
|
24
|
+
[
|
25
|
+
{
|
26
|
+
:name => 'Person',
|
27
|
+
:fields => [
|
28
|
+
{
|
29
|
+
:name => 'name',
|
30
|
+
:type => String,
|
31
|
+
:length => 128,
|
32
|
+
:validators => {
|
33
|
+
:presence => true
|
34
|
+
}
|
35
|
+
}, {
|
36
|
+
:name => 'email',
|
37
|
+
:validators => {
|
38
|
+
:presence => true,
|
39
|
+
:format => {
|
40
|
+
:with => /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}, {
|
44
|
+
:name => 'birthdate',
|
45
|
+
:type => Date
|
46
|
+
}
|
47
|
+
]
|
48
|
+
}, {
|
49
|
+
:name => 'Employee',
|
50
|
+
:extends => 'Person',
|
51
|
+
:includes => %w(Mongoid::Timestamps),
|
52
|
+
:fields => [
|
53
|
+
{
|
54
|
+
:name => 'birthdate',
|
55
|
+
:validators => {
|
56
|
+
:presence => true
|
57
|
+
}
|
58
|
+
}, {
|
59
|
+
:name => 'salary',
|
60
|
+
:type => Float,
|
61
|
+
:default => 1000.00
|
62
|
+
}
|
63
|
+
]
|
64
|
+
}
|
65
|
+
]
|
66
|
+
|
67
|
+
Build your models :
|
68
|
+
|
69
|
+
Mongoid::ModelBuilder.load('config/models.rb')
|
70
|
+
=> [Person, Employee]
|
71
|
+
|
72
|
+
Person.new
|
73
|
+
=> #<Person _id: ..., _type: "Person", name: nil, email: nil, birthdate: nil>
|
74
|
+
|
75
|
+
Employee.new
|
76
|
+
=> #<Employee _id: ..., _type: "Employee", name: nil, email: nil, birthdate: nil, salary: 1000>
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
1. Fork it
|
81
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
82
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
83
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
84
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module ModelBuilder
|
3
|
+
class << self
|
4
|
+
# Load models definitions from Ruby configuration file.
|
5
|
+
def load models, options = {}
|
6
|
+
|
7
|
+
# Try to read file if a String is provided
|
8
|
+
models = eval File.read models if models.is_a? String
|
9
|
+
|
10
|
+
raise "Models list must be an Array or a ruby file containing an Array" unless models.is_a? Array
|
11
|
+
|
12
|
+
result = []
|
13
|
+
@code = []
|
14
|
+
models.each do |model|
|
15
|
+
result << build(model, options[:force])
|
16
|
+
end
|
17
|
+
|
18
|
+
return @code.join("\n") if options[:code]
|
19
|
+
return result
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
# Build a model class
|
25
|
+
def build model, force
|
26
|
+
# Handle existing classes
|
27
|
+
if Object.const_defined? model[:name]
|
28
|
+
raise "Class '#{model[:name]}' already exists." unless force
|
29
|
+
Object.send(:remove_const, model[:name]) rescue nil
|
30
|
+
end
|
31
|
+
|
32
|
+
# Set parent class
|
33
|
+
parent = model[:extends] ? model[:extends].constantize : Object
|
34
|
+
|
35
|
+
# Create model class
|
36
|
+
@model = Object.const_set model[:name], Class.new(parent)
|
37
|
+
@code << "class #{model[:name]} < #{parent}"
|
38
|
+
|
39
|
+
# Include Mongoid::Document by default
|
40
|
+
includes = %w(Mongoid::Document)
|
41
|
+
if model[:includes]
|
42
|
+
raise "Includes list must be an Array (#{model[:includes].class} provided)" unless model[:includes].is_a? Array
|
43
|
+
includes |= model[:includes]
|
44
|
+
end
|
45
|
+
|
46
|
+
add_includes includes
|
47
|
+
add_fields model[:fields]
|
48
|
+
|
49
|
+
@code << 'end'
|
50
|
+
|
51
|
+
return @model
|
52
|
+
end
|
53
|
+
|
54
|
+
# Appends code to current model class
|
55
|
+
def model_append source
|
56
|
+
source = source.join("\n") if source.is_a? Array
|
57
|
+
raise "model_append only accepts String or Array source (#{source.class} provided)" unless source.is_a? String
|
58
|
+
@model.class_eval source
|
59
|
+
@code << source
|
60
|
+
end
|
61
|
+
|
62
|
+
# Handle class includes
|
63
|
+
def add_includes includes
|
64
|
+
a = []
|
65
|
+
includes.uniq.each do |value|
|
66
|
+
a << "include #{value.constantize}"
|
67
|
+
end
|
68
|
+
model_append a
|
69
|
+
end
|
70
|
+
|
71
|
+
# Handle model fields
|
72
|
+
def add_fields fields
|
73
|
+
raise "Fields list must be an Array (#{fields.class} provided)" unless fields.is_a? Array
|
74
|
+
|
75
|
+
fields.each do |field|
|
76
|
+
add_field field
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def add_field field
|
81
|
+
raise "Field must be a Hash (#{field.class} provided)" unless field.is_a? Hash
|
82
|
+
|
83
|
+
# Retrieve parent field options if not overloaded
|
84
|
+
if @model.superclass.respond_to?('fields') && @model.superclass.fields.has_key?(field[:name])
|
85
|
+
parent = @model.superclass.fields[field[:name]]
|
86
|
+
field[:type] = parent.type unless field[:type]
|
87
|
+
field[:default] = parent.default_val unless field[:default]
|
88
|
+
field[:label] = parent.label unless field[:label]
|
89
|
+
field[:localize] = parent.localized? unless field[:localize]
|
90
|
+
end
|
91
|
+
|
92
|
+
# Field definition
|
93
|
+
allowed_options = [:type, :default, :localize, :label]
|
94
|
+
model_append "field :#{field[:name]}, #{field.slice(allowed_options)}"
|
95
|
+
|
96
|
+
# Length option automatically creates a maximum length validator
|
97
|
+
field = {:validators => {:length => {:maximum => field[:length]}}}.deep_merge(field) if field[:length]
|
98
|
+
add_validators field
|
99
|
+
end
|
100
|
+
|
101
|
+
# Handle model validators
|
102
|
+
def add_validators field
|
103
|
+
return unless field[:validators]
|
104
|
+
raise "Field validators list must be a Hash (#{field[:validators].class} provided)" unless field[:validators].is_a? Hash
|
105
|
+
|
106
|
+
model_append "validates :#{field[:name]}, #{field[:validators]}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mongoid_model_builder/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Gauthier Delacroix"]
|
6
|
+
gem.email = ["gauthier.delacroix@gmail.com"]
|
7
|
+
gem.description = "%q{mongoid_model_builder dynamically creates Mongoid model classes following configuration hash specifications}"
|
8
|
+
gem.summary = "%q{Dynamic Mongoid model classes generator}"
|
9
|
+
gem.homepage = "https://github.com/porecreat/mongoid_model_builder"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "mongoid_model_builder"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Mongoid::ModelBuilder::VERSION
|
17
|
+
|
18
|
+
gem.add_runtime_dependency('mongoid', '~> 2.4')
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_model_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gauthier Delacroix
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.4'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.4'
|
30
|
+
description: ! '%q{mongoid_model_builder dynamically creates Mongoid model classes
|
31
|
+
following configuration hash specifications}'
|
32
|
+
email:
|
33
|
+
- gauthier.delacroix@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- .project
|
40
|
+
- Gemfile
|
41
|
+
- LICENSE
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- lib/mongoid_model_builder.rb
|
45
|
+
- lib/mongoid_model_builder/mongoid_model_builder.rb
|
46
|
+
- lib/mongoid_model_builder/version.rb
|
47
|
+
- mongoid_model_builder.gemspec
|
48
|
+
homepage: https://github.com/porecreat/mongoid_model_builder
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.21
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: ! '%q{Dynamic Mongoid model classes generator}'
|
72
|
+
test_files: []
|