meta_mongo 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b596bb68d962e79d517aec9d9adf53024c3bb7d0
4
- data.tar.gz: 8a16a8b8d85da3c357c6172d39d561529f34d2a1
3
+ metadata.gz: 5af28da55c2b0b7fc58f963c6fc5f155ee8777ff
4
+ data.tar.gz: cf3b897bf7e7c0319b67f0747cc44cf2e3c0ae9b
5
5
  SHA512:
6
- metadata.gz: 38a3b34ad4c444a1434f3c1e6dee625d260cba6d765e574bc1bbeab760f14b74bd7ef358b7b4aa39b7478d460d78a50107f4da75f5069c8c7476df58f0a4f813
7
- data.tar.gz: 10c154af0026362054fbdeadb57c5f252409eb1dd5a3edea6caf4bb194421339845d5356678010de2c5f369c3bff7f7dc4f2be85a6b34cc60ab794c1c01cac81
6
+ metadata.gz: faa6822ffc3295b9fdf1c644ac0fb2272002a4e2091b8f6b45dbd1cc9f324a32f1eb91e992fa24f5e6180d6fc22c1fbddabf79beee1af93c881f44afc99dd60b
7
+ data.tar.gz: be9585a77dd7eb347cf9651c33e1e1b4292d46a37d4b48e42138dc26dead8241521c110da140e0b80c36d1f44ba0927746cad47afd3c0ec66fa9a58f39f83d3c
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --color
2
2
  --require spec_helper
3
+ --format documentation
data/Guardfile CHANGED
@@ -11,5 +11,5 @@ guard :rspec, cmd: "bundle exec rspec" do
11
11
  # Ruby files
12
12
  ruby = dsl.ruby
13
13
  dsl.watch_spec_files_for(ruby.lib_files)
14
- watch(%r{^lib/p15id/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
14
+ watch(%r{^lib/meta_mongo/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
15
15
  end
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # MetaMongo
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/meta_mongo`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Generates Mongoid model and validations dinamically.
6
4
 
7
5
  ## Installation
8
6
 
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :test => :spec
7
+ task :default => :test
@@ -0,0 +1,27 @@
1
+ module MetaMongo
2
+ module Utils
3
+ class << self;
4
+ def to_class(string)
5
+ begin
6
+ return Object.const_get(string.to_s)
7
+ rescue NameError
8
+ end
9
+ end
10
+
11
+ def remove_class(string)
12
+ return nil unless to_class(string)
13
+
14
+ Object.send(:remove_const, string.to_sym)
15
+ end
16
+
17
+ def create_class(string)
18
+ return nil if to_class(string)
19
+
20
+ begin
21
+ Object.const_set(string.to_s, Class.new)
22
+ rescue NameError
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module MetaMongo
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/meta_mongo.rb CHANGED
@@ -1,5 +1,90 @@
1
1
  require "meta_mongo/version"
2
+ require "meta_mongo/utils"
3
+ require "mongoid"
4
+ require "pry"
2
5
 
3
6
  module MetaMongo
4
- # Your code goes here...
7
+ class BadHash < Exception; end
8
+
9
+ class << self;
10
+ def create_model(name)
11
+ Utils.create_class(name)
12
+ end
13
+
14
+ def run_method(klass, method, *args)
15
+ klass.send(method, *args)
16
+ end
17
+
18
+ def create_method(klass, method, block)
19
+ klass.singleton_class.send(:define_method, :foo, &block)
20
+ end
21
+
22
+ def include_module(klass, mod)
23
+ klass.class_eval do
24
+ include mod
25
+ end
26
+ end
27
+
28
+ def check_missing_keys(obj, *keys)
29
+ keys.each do |key|
30
+ missing_key(key) if !obj.key?(key)
31
+ end
32
+ end
33
+
34
+ def missing_key(key)
35
+ raise BadHash.new("Missing '#{key}' key")
36
+ end
37
+
38
+ def wrong_type(key)
39
+ raise BadHash.new("Invalid '#{key}' type")
40
+ end
41
+
42
+ def parse_attributes(attrs)
43
+ out = []
44
+
45
+ attrs.each do |my_attr|
46
+ check_missing_keys my_attr, "field", "name", "type", "validations"
47
+
48
+ out << {
49
+ name: my_attr["name"],
50
+ field: my_attr["field"],
51
+ type: Utils.to_class(my_attr["type"]),
52
+ vals: my_attr["validations"]
53
+ }
54
+
55
+ wrong_type("validations") if !out.last[:vals].is_a?(Array)
56
+
57
+ end
58
+
59
+ out
60
+ end
61
+
62
+ def init(hash)
63
+ models = hash["models"]
64
+
65
+ missing_key("models") unless models
66
+ wrong_type("models") unless models.is_a?(Array)
67
+
68
+ models.each do |model|
69
+ check_missing_keys(model, "name", "attributes")
70
+
71
+ if !model["attributes"].is_a?(Array)
72
+ wrong_type("attributes")
73
+ end
74
+
75
+ attrs = parse_attributes(model["attributes"])
76
+
77
+ klass = MetaMongo.create_model(model["name"])
78
+ MetaMongo.include_module(klass, Mongoid::Document)
79
+
80
+ attrs.each do |att|
81
+ MetaMongo.run_method(klass, :field, att[:field], type: att[:type])
82
+
83
+ att[:vals].each do |val|
84
+ MetaMongo.run_method(klass, :validates, att[:field], val)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
5
90
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meta_mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Boaventura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-22 00:00:00.000000000 Z
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -113,6 +113,7 @@ files:
113
113
  - bin/console
114
114
  - bin/setup
115
115
  - lib/meta_mongo.rb
116
+ - lib/meta_mongo/utils.rb
116
117
  - lib/meta_mongo/version.rb
117
118
  - meta_mongo.gemspec
118
119
  homepage: https://github.com/michelboaventura/meta_mongo