codegen 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 427e6b563f7c6af1dba8f3d51825679f6eab80fe
4
+ data.tar.gz: 0432327a3055bb40dbf6c83fe13d3ff240ceb8f3
5
+ SHA512:
6
+ metadata.gz: f2c6a20a0cbc3353e19d5ea04a955750ab9a8a74b11c377879abcd9b27cc703a2bebd93a250ec71aa395d7760581397312603179e23e1a6c6454b3c23e692a82
7
+ data.tar.gz: 5bc6f1d05d0ecd0dad85ade8908d107fddc90befbda76ea8400c81cd96e7f0393b9fcd52be3af1ac15c5e54933702cb5b0e469843f2963682e9e502b3db0ad36
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .DS_Store
4
+ /**/.DS_Store
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use --create 2.0.0@artopojo
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Cristian Pereyra
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,29 @@
1
+ # ArToPojo
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ar_to_pojo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ar_to_pojo
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/codegen.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 'codegen/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "codegen"
8
+ spec.version = Codegen::VERSION
9
+ spec.authors = ["Cristian Pereyra"]
10
+ spec.email = ["criis.pereyra@gmail.com"]
11
+ spec.description = %q{A gem for making Java POJOs out of ActiveRecord models}
12
+ spec.summary = %q{Made with Android in mind, but it will maybe be extended to other platforms}
13
+ spec.homepage = ""
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "simplecov"
25
+
26
+ spec.add_dependency 'active_support', "~> 3.2"
27
+ end
@@ -0,0 +1,11 @@
1
+ require 'json'
2
+
3
+ module Codegen
4
+ module Generators
5
+ module JavaPojo
6
+ def generate
7
+
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'generators/java_pojo'
2
+ module Codegen
3
+ module Generators
4
+ end
5
+ end
@@ -0,0 +1,100 @@
1
+ module Codegen
2
+ module Sources
3
+ class ActiveRecord < Base
4
+
5
+ converts_to :entity
6
+
7
+ def convert options = {}
8
+ if options[:type] == :entity
9
+ convert_entity options
10
+ end
11
+ end
12
+
13
+ private
14
+ def convert_entity options
15
+ models = options[:models] || collect_all_models
16
+ models_and_methods = models.map { |model|
17
+ {
18
+ model => {
19
+ methods: model_methods(model),
20
+ relations: model_relations(model)
21
+ }
22
+ }
23
+ }
24
+
25
+ models_and_methods.map { |mm|
26
+ model_to_entity(mm)
27
+ }
28
+ end
29
+
30
+ def model_to_entity mm
31
+ model = mm.keys.first
32
+ methods = mm[model][:methods]
33
+ relations = mm[model][:relations]
34
+
35
+ normalized_relations = relations.map { |relation|
36
+ case relation[:type]
37
+ when :has_many, :has_and_belongs_to_many
38
+ ref = :many
39
+ when :belongs_to, :has_one
40
+ ref = :one
41
+ else
42
+ puts "WARNING: Unexpected relation type for relation: #{relation[:name]} on class #{model}"
43
+ ref = :one
44
+ end
45
+
46
+ {
47
+ cardinality: ref,
48
+ name: relation[:name],
49
+ type: relation[:klass]
50
+ }
51
+ }
52
+
53
+ normalized_methods = methods.map { |method|
54
+ {
55
+ cardinality: :one,
56
+ name: method[:name],
57
+ type: method[:type]
58
+ }
59
+ }
60
+
61
+ begin
62
+ mname = model.class_name
63
+ rescue
64
+ mname = model.to_s
65
+ end
66
+
67
+ Codegen::Types::Entity.new name: mname, methods: normalized_methods, relations: normalized_relations
68
+ end
69
+
70
+
71
+ def collect_all_models
72
+ begin
73
+ Rails.application.eager_load! if defined?(Rails)
74
+ ActiveRecord::Base.subclasses
75
+ rescue
76
+ []
77
+ end
78
+ end
79
+
80
+ def model_methods model
81
+ model.columns.map { |column|
82
+ {
83
+ type: column.type.to_s,
84
+ name: column.name
85
+ }
86
+ }
87
+ end
88
+
89
+ def model_relations model
90
+ model.reflect_on_all_associations.map { |assoc|
91
+ {
92
+ type: assoc.macro,
93
+ name: assoc.name.to_s,
94
+ klass: assoc.class_name
95
+ }
96
+ }
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,33 @@
1
+ require 'active_support'
2
+
3
+ module Codegen
4
+ module Sources
5
+ class Base
6
+ include ActiveSupport::Callbacks
7
+ class << self
8
+ attr_accessor :convert_to
9
+ end
10
+
11
+ def self.converts_to type
12
+ if type.is_a? Symbol
13
+ self.convert_to = type
14
+ else
15
+ self.convert_to = type.map { |t| t.to_s.underscore.to_sym }
16
+ end
17
+ end
18
+
19
+ define_callbacks :check_valid
20
+ set_callback :check_valid, :before, :convert!
21
+
22
+ def check_valid params
23
+ self.convert_to.include? params[:type]
24
+ end
25
+
26
+ def convert!
27
+ run_callbacks :convert! do
28
+ convert
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,6 @@
1
+ require_relative 'sources/base'
2
+ require_relative 'sources/active_record'
3
+ module Codegen
4
+ module Sources
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ module Codegen
2
+ module Types
3
+ class Entity
4
+ attr_accessor :name, :relations, :methods
5
+
6
+ def initialize params
7
+ @name = params[:name]
8
+ @relations = params[:relations]
9
+ @methods = params[:methods]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ require_relative 'types/entity'
2
+ module Codegen
3
+ module Types
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Codegen
2
+ VERSION = "0.0.2"
3
+ end
data/lib/codegen.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'codegen/version'
2
+ require_relative 'codegen/sources'
3
+ require_relative 'codegen/generators'
4
+ require_relative 'codegen/types'
5
+
6
+ module Codegen
7
+ class Gen
8
+ attr_accessor :source
9
+ attr_accessor :source_options
10
+ attr_accessor :generator
11
+ attr_accessor :generator_options
12
+ end
13
+ end
@@ -0,0 +1,57 @@
1
+ require_relative '../lib/codegen'
2
+ require 'rspec_helper'
3
+ require 'pry'
4
+
5
+ describe Codegen do
6
+ context "Analyzing ActiveRecord" do
7
+ before :each do
8
+ @ar_class = mock("Entity")
9
+
10
+ columns = [
11
+ ["name", :string],
12
+ ["birth_date", :date],
13
+ ["register_date",:datetime],
14
+ ["precision",:float],
15
+ ["score",:integer],
16
+ ["description",:text]
17
+ ].map { |item|
18
+ column_mock = mock("Object")
19
+ column_mock.stub!(:name).and_return(item[0])
20
+ column_mock.stub!(:type).and_return(item[1])
21
+ column_mock
22
+ }
23
+
24
+ associations = [
25
+ ["Project", :has_and_belongs_to_many, :projects],
26
+ ["Comment", :has_many, :comments],
27
+ ["Picture", :has_many, :pictures],
28
+ ["Profile", :has_one, :profile],
29
+ ["Company", :belongs_to, :organization]
30
+ ].map { |item|
31
+ assoc_mock = mock("Object")
32
+ assoc_mock.stub!(:class_name).and_return(item[0])
33
+ assoc_mock.stub!(:macro).and_return(item[1])
34
+ assoc_mock.stub!(:name).and_return(item[2])
35
+ assoc_mock
36
+ }
37
+
38
+ @ar_class.stub!(:reflect_on_all_associations).and_return(associations)
39
+ @ar_class.stub!(:columns).and_return(columns)
40
+ @ar_class.stub!(:class_name).and_return("Entity")
41
+ @ar_class.stub!(:to_s).and_return("Entity")
42
+ @ar_class.stub!(:ancestors).and_return(["ActiveRecord::Base"])
43
+ end
44
+
45
+ it "Should be able to open an ActiveRecord object and detect columns and relations" do
46
+ @ar_class.columns.size.should_not == 0
47
+ @ar_class.reflect_on_all_associations.size.should_not == 0
48
+
49
+ source = Codegen::Sources::ActiveRecord.new
50
+ entities = source.convert type: :entity, models: 5.times.map { @ar_class }
51
+ entities.size.should == 5
52
+ entities.first.name.should == "Entity"
53
+ entities.first.relations.count.should == 5
54
+ entities.first.methods.count.should == 6
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,18 @@
1
+ if ENV["COVERAGE"]
2
+ require "simplecov"
3
+ SimpleCov.start
4
+ end
5
+
6
+ require 'rspec/autorun'
7
+
8
+ RSpec.configure do |config|
9
+ # Run specs in random order to surface order dependencies. If you find an
10
+ # order dependency and want to debug it, you can fix the order by providing
11
+ # the seed, which is printed after each run.
12
+ # --seed 1234
13
+ config.order = "random"
14
+
15
+ config.formatter = :documentation
16
+
17
+ config.color_enabled = true
18
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codegen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Cristian Pereyra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: simplecov
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: active_support
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ description: A gem for making Java POJOs out of ActiveRecord models
84
+ email:
85
+ - criis.pereyra@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rvmrc
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - codegen.gemspec
97
+ - lib/codegen.rb
98
+ - lib/codegen/generators.rb
99
+ - lib/codegen/generators/java_pojo.rb
100
+ - lib/codegen/sources.rb
101
+ - lib/codegen/sources/active_record.rb
102
+ - lib/codegen/sources/base.rb
103
+ - lib/codegen/types.rb
104
+ - lib/codegen/types/entity.rb
105
+ - lib/codegen/version.rb
106
+ - spec/codegen_spec.rb
107
+ - spec/rspec_helper.rb
108
+ homepage: ''
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.0.0
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Made with Android in mind, but it will maybe be extended to other platforms
133
+ test_files:
134
+ - spec/codegen_spec.rb
135
+ - spec/rspec_helper.rb