rails_clafer 0.1.2
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +4 -0
- data/lib/rails_clafer/association.rb +16 -0
- data/lib/rails_clafer/card.rb +17 -0
- data/lib/rails_clafer/clafer.rake +7 -0
- data/lib/rails_clafer/clafer_element.rb +92 -0
- data/lib/rails_clafer/clafer_model.rb +60 -0
- data/lib/rails_clafer/clafer_printer.rb +92 -0
- data/lib/rails_clafer/gcard.rb +9 -0
- data/lib/rails_clafer/railtie.rb +7 -0
- data/lib/rails_clafer/ref_clafer.rb +18 -0
- data/lib/rails_clafer/sub_clafer.rb +28 -0
- data/lib/rails_clafer/version.rb +3 -0
- data/lib/rails_clafer.rb +20 -0
- data/lib/rails_clafer.rb~ +5 -0
- metadata +79 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
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,29 @@
|
|
1
|
+
# RailsClafer
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rails_clafer'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rails_clafer
|
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,16 @@
|
|
1
|
+
module RailsClafer
|
2
|
+
class Association
|
3
|
+
class << self
|
4
|
+
def from_ar_assoc(src_class, ar_assoc)
|
5
|
+
card = Card.from_ar_assoc ar_assoc
|
6
|
+
assoc = new src_class.name, ar_assoc.klass.name, card
|
7
|
+
assoc.ref_name = ar_assoc.name
|
8
|
+
assoc
|
9
|
+
end
|
10
|
+
end
|
11
|
+
attr_accessor :src, :dst, :dst_card, :ref_name
|
12
|
+
def initialize(src, dst, dst_card)
|
13
|
+
@src, @dst, @dst_card = src, dst, dst_card
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module RailsClafer
|
2
|
+
class Card
|
3
|
+
class << self
|
4
|
+
def from_ar_assoc(assoc)
|
5
|
+
if assoc.collection?
|
6
|
+
new 0, "*"
|
7
|
+
else
|
8
|
+
new 1, 1
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
attr_accessor :min, :max
|
13
|
+
def initialize(min=1,max=1)
|
14
|
+
@min, @max = min, max
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module RailsClafer
|
2
|
+
class ClaferElement
|
3
|
+
class << self
|
4
|
+
def model_belong_to_mult_models?(class_name)
|
5
|
+
if ClaferModel.assoc_dst.has_key? class_name
|
6
|
+
if ClaferModel.assoc_dst[class_name].size>1
|
7
|
+
true
|
8
|
+
else
|
9
|
+
false
|
10
|
+
end
|
11
|
+
else
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def dfs (assoc_map, class_name, path, target, abs_model_map)
|
17
|
+
if assoc_map.has_key? class_name
|
18
|
+
assoc_map[class_name].each do |assoc|
|
19
|
+
if assoc.dst == target
|
20
|
+
abs_model_map.merge! path
|
21
|
+
else
|
22
|
+
if !path.has_key? assoc.dst
|
23
|
+
dfs(assoc_map, assoc.dst, path.merge({assoc.dst => true}), target, abs_model_map)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def calc_abstract_clafer_list()
|
31
|
+
abs_model_map = Hash[ClaferModel.rails_models.collect {|klass| [klass.name, false] }]
|
32
|
+
#first pass to check if ruby model belongs to more than one model
|
33
|
+
abs_model_map.each do |class_name, is_abstract|
|
34
|
+
if model_belong_to_mult_models? class_name
|
35
|
+
abs_model_map[class_name] = true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
# second pass to look for circular associations
|
39
|
+
abs_model_map.each do |class_name, is_abstract|
|
40
|
+
if !is_abstract
|
41
|
+
dfs(ClaferModel.assoc_src, class_name, {class_name => true}, class_name, abs_model_map )
|
42
|
+
end
|
43
|
+
end
|
44
|
+
abs_model_map
|
45
|
+
end
|
46
|
+
|
47
|
+
def from_rails_models(rails_models)
|
48
|
+
# create clafer for each metaclafer and assoc dst if not already exists
|
49
|
+
class_mapping = Hash[rails_models.collect {|model_class| [model_class.name, model_class] }]
|
50
|
+
abs_model_map = calc_abstract_clafer_list
|
51
|
+
|
52
|
+
class_mapping.collect do |name, klass|
|
53
|
+
new name, klass, abs_model_map[name]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
attr_accessor :is_abstract
|
60
|
+
alias_method :is_abstract?, :is_abstract
|
61
|
+
attr_accessor :name, :card, :gcard, :constraint
|
62
|
+
def initialize(name, ruby_class, is_abstract)
|
63
|
+
@is_abstract = is_abstract
|
64
|
+
@name = name
|
65
|
+
@klass = ruby_class
|
66
|
+
@gcard = GCard.new
|
67
|
+
@card = Card.new
|
68
|
+
@constraint = ""
|
69
|
+
end
|
70
|
+
|
71
|
+
def children
|
72
|
+
@children ||= children_list
|
73
|
+
end
|
74
|
+
private
|
75
|
+
def children_list
|
76
|
+
if ClaferModel.assoc_src.has_key?(@name)
|
77
|
+
ClaferModel.assoc_src[@name].collect do |assoc|
|
78
|
+
dst_clafer_name = assoc.dst
|
79
|
+
clafer = ClaferModel.clafer_by_name(dst_clafer_name)
|
80
|
+
if clafer.is_abstract?
|
81
|
+
RefClafer.from_assoc(assoc, clafer)
|
82
|
+
else
|
83
|
+
SubClafer.from_assoc(assoc, clafer)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
else
|
87
|
+
[]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'rails_clafer/gcard'
|
2
|
+
require 'rails_clafer/card'
|
3
|
+
require 'rails_clafer/clafer_element'
|
4
|
+
require 'rails_clafer/association'
|
5
|
+
require 'rails_clafer/ref_clafer'
|
6
|
+
require 'rails_clafer/sub_clafer'
|
7
|
+
|
8
|
+
|
9
|
+
module RailsClafer
|
10
|
+
class ClaferModel
|
11
|
+
class << self
|
12
|
+
def init(settings)
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def assoc_src
|
17
|
+
@assoc_src ||= associations.inject({}) do |result, assoc|
|
18
|
+
(result[assoc.src] ||= []) << assoc
|
19
|
+
result
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def assoc_dst
|
24
|
+
@assoc_dst ||= associations.inject({}) do |result, assoc|
|
25
|
+
(result[assoc.dst] ||= []) << assoc
|
26
|
+
result
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def abstract_clafers
|
31
|
+
@abstract_clafers ||= clafers.select{|clafer| clafer.is_abstract?}
|
32
|
+
end
|
33
|
+
|
34
|
+
def clafer_by_name(name)
|
35
|
+
@clafer_map ||= Hash[clafers.collect {|clafer| [clafer.name, clafer]}]
|
36
|
+
@clafer_map[name]
|
37
|
+
end
|
38
|
+
|
39
|
+
def associations
|
40
|
+
@associations ||= rails_models.collect { |model|
|
41
|
+
model.reflect_on_all_associations.select { |ar_assoc|
|
42
|
+
!ar_assoc.belongs_to?
|
43
|
+
}.collect{ |ar_assoc|
|
44
|
+
Association.from_ar_assoc(model, ar_assoc)
|
45
|
+
}
|
46
|
+
}.flatten
|
47
|
+
end
|
48
|
+
|
49
|
+
def clafers
|
50
|
+
@clafers ||= ClaferElement.from_rails_models rails_models
|
51
|
+
end
|
52
|
+
|
53
|
+
def rails_models
|
54
|
+
@rails_models ||= ActiveRecord::Base.descendants
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module RailsClafer
|
2
|
+
class ClaferPrinter
|
3
|
+
class << self
|
4
|
+
def print_clafers(list)
|
5
|
+
out = ""
|
6
|
+
list.each do |clafer|
|
7
|
+
out << print_clafer(clafer, 0)
|
8
|
+
end
|
9
|
+
out
|
10
|
+
end
|
11
|
+
|
12
|
+
def print_clafer(clafer, indent_level)
|
13
|
+
whitespace = build_whitespace(indent_level)
|
14
|
+
out = whitespace
|
15
|
+
if clafer.respond_to?(:ref_name)
|
16
|
+
out << print_ref_clafer(clafer)
|
17
|
+
elsif clafer.respond_to?(:name)
|
18
|
+
out << print_reg_clafer(clafer)
|
19
|
+
end
|
20
|
+
out << "\n"
|
21
|
+
if clafer.respond_to?(:children)
|
22
|
+
subclafers = clafer.children
|
23
|
+
subclafers.each do |subclafer|
|
24
|
+
out << print_clafer(subclafer, indent_level + 1)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
out << print_constraint(indent_level + 1, clafer.constraint) unless clafer.constraint.blank?
|
28
|
+
out
|
29
|
+
|
30
|
+
end
|
31
|
+
def print_abstract(clafer)
|
32
|
+
if clafer.is_abstract?
|
33
|
+
"abstract "
|
34
|
+
else
|
35
|
+
""
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def build_whitespace(indent_level)
|
40
|
+
' ' * indent_level * 4
|
41
|
+
end
|
42
|
+
|
43
|
+
def print_constraint(indent_level, constraint)
|
44
|
+
out = build_whitespace(indent_level)
|
45
|
+
out << constraint
|
46
|
+
out << "\n"
|
47
|
+
out
|
48
|
+
end
|
49
|
+
|
50
|
+
def print_reg_clafer(clafer)
|
51
|
+
"#{print_gcard(clafer.gcard)}#{print_abstract(clafer)}#{clafer.name} #{print_card(clafer.card)}"
|
52
|
+
end
|
53
|
+
|
54
|
+
def print_ref_clafer(ref_clafer)
|
55
|
+
"#{ref_clafer.ref_name} -> #{ref_clafer.clafer_name} #{print_card(ref_clafer.card)}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def print_card(card)
|
59
|
+
map = {[1, 1]=>"",
|
60
|
+
[0, 1] => "?",
|
61
|
+
[1, "*"] => "+",
|
62
|
+
[0, "*"] => "*" }
|
63
|
+
out = map[[card.min, card.max]]
|
64
|
+
out = "#{card.min}..#{card.max}" unless out != nil
|
65
|
+
out
|
66
|
+
#if card.min == 1 && card.max == 1
|
67
|
+
# ""
|
68
|
+
#elsif card.min == 0 && card.max = 1
|
69
|
+
# "?"
|
70
|
+
#elsif card.min == 1 && card.max =="*"
|
71
|
+
# "+"
|
72
|
+
#elsif card.min == 0 && card.max == "*"
|
73
|
+
# "*"
|
74
|
+
#else
|
75
|
+
# "#{card.min}..#{card.max}"
|
76
|
+
#end
|
77
|
+
end
|
78
|
+
|
79
|
+
def print_gcard(gcard)
|
80
|
+
map = {[0,"*"]=>"",
|
81
|
+
[1, 1] => "xor",
|
82
|
+
[1, "*"] => "or",
|
83
|
+
[0, 1] => "mux" }
|
84
|
+
#[0, "*"] => "opt" }
|
85
|
+
out = map[[gcard.min, gcard.max]]
|
86
|
+
out = "#{gcard.min}-#{gcard.max}" unless out != nil
|
87
|
+
out << " " unless out.empty?
|
88
|
+
out
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module RailsClafer
|
2
|
+
class RefClafer
|
3
|
+
class << self
|
4
|
+
def from_assoc(assoc, type_clafer)
|
5
|
+
new assoc.ref_name, type_clafer, assoc.dst_card
|
6
|
+
end
|
7
|
+
end
|
8
|
+
attr_accessor :ref_name, :clafer, :card, :gcard, :constraint
|
9
|
+
def initialize(ref_name, type_clafer, card)
|
10
|
+
@ref_name, @clafer= ref_name, type_clafer
|
11
|
+
@card = card
|
12
|
+
@gcard = GCard.new
|
13
|
+
end
|
14
|
+
def clafer_name
|
15
|
+
@clafer.name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RailsClafer
|
2
|
+
class SubClafer
|
3
|
+
class << self
|
4
|
+
def from_assoc(assoc, clafer)
|
5
|
+
new clafer, assoc.dst_card
|
6
|
+
end
|
7
|
+
end
|
8
|
+
attr_accessor :name, :clafer, :card, :gcard
|
9
|
+
def initialize(clafer, card)
|
10
|
+
@name = clafer.name
|
11
|
+
@clafer = clafer
|
12
|
+
@card = card
|
13
|
+
@gcard = GCard.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(name, *args)
|
17
|
+
if @clafer.respond_to?(name)
|
18
|
+
@clafer.send(name, *args)
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def children
|
25
|
+
@clafer.children
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/rails_clafer.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "rails_clafer/version"
|
2
|
+
require "rails_clafer/railtie" if defined? Rails
|
3
|
+
|
4
|
+
module RailsClafer
|
5
|
+
class << self
|
6
|
+
def print
|
7
|
+
init
|
8
|
+
require 'rails_clafer/clafer_printer'
|
9
|
+
require 'rails_clafer/clafer_model'
|
10
|
+
claferCode = ClaferPrinter.print_clafers ClaferModel.abstract_clafers
|
11
|
+
puts claferCode
|
12
|
+
claferCode
|
13
|
+
end
|
14
|
+
|
15
|
+
def init
|
16
|
+
Rake::Task[:environment].invoke
|
17
|
+
Rails.application.eager_load!
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_clafer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paulius Juodisius
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
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: 3.0.0
|
30
|
+
description: Rails to Clafer model transformation tool
|
31
|
+
email:
|
32
|
+
- pjuo@itu.dk
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/rails_clafer.rb
|
43
|
+
- lib/rails_clafer.rb~
|
44
|
+
- lib/rails_clafer/association.rb
|
45
|
+
- lib/rails_clafer/card.rb
|
46
|
+
- lib/rails_clafer/clafer.rake
|
47
|
+
- lib/rails_clafer/clafer_element.rb
|
48
|
+
- lib/rails_clafer/clafer_model.rb
|
49
|
+
- lib/rails_clafer/clafer_printer.rb
|
50
|
+
- lib/rails_clafer/gcard.rb
|
51
|
+
- lib/rails_clafer/railtie.rb
|
52
|
+
- lib/rails_clafer/ref_clafer.rb
|
53
|
+
- lib/rails_clafer/sub_clafer.rb
|
54
|
+
- lib/rails_clafer/version.rb
|
55
|
+
homepage: http://www.juodisius.com
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.23
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Rails to Clafer model transformation tool
|
79
|
+
test_files: []
|