valor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +1 -0
- data/lib/valor/version.rb +3 -0
- data/lib/valor.rb +74 -0
- data/valor.gemspec +25 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e9e0afe17feb8e5f438c6f28d4809b3f21a3f380
|
4
|
+
data.tar.gz: f6f6dc2757bee433b931fbceab72bc12f9e91b3f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6bb392bf6e1cafec72738df234509178eeebdfedf23105e0440683c8afbc1f16060808b096f791ab36ad60fa47c7a4625695734e2078a974168238f75a5ba952
|
7
|
+
data.tar.gz: 14da2eddc33660d1348650b6b2054bab2a506db2a180d1cbe5cf90dcfb2e55f11fbea62e7e144c2b1bafef1ec21001f0b639079c14de6dfd059041c9346d48a2
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Brandon Weaver
|
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,85 @@
|
|
1
|
+
# Valor
|
2
|
+
|
3
|
+
Take a Ruby Hash and transform it into the makings of a Virtus Model class.
|
4
|
+
|
5
|
+
## How to
|
6
|
+
|
7
|
+
So how do we go about this then? First we get Valor a hash to use:
|
8
|
+
```ruby
|
9
|
+
hash = {a: {b: 1, c: [{d: 1}, {d: 2}]}}
|
10
|
+
```
|
11
|
+
|
12
|
+
Then we make a Valor Generator. Send a file directory if you want it to auto save:
|
13
|
+
```ruby
|
14
|
+
valor = Valor::Generator.new('Test', hash)
|
15
|
+
valor = Valor::Generator.new('Test', hash, '/tmp') # For autosaving
|
16
|
+
```
|
17
|
+
|
18
|
+
Which will generate the following models and attributes:
|
19
|
+
```ruby
|
20
|
+
{
|
21
|
+
"D" => ["attribute :d, Integer"],
|
22
|
+
"A" => ["attribute :b, Integer", "attribute :c, Array[D]"],
|
23
|
+
"Test" => ["attribute :a, A"]
|
24
|
+
}
|
25
|
+
```
|
26
|
+
|
27
|
+
It even makes sure to generate new models for every sub hash you happen to pass it.
|
28
|
+
|
29
|
+
Now we can save the files. Pass in a directory, or it will default to your current working directory.
|
30
|
+
```ruby
|
31
|
+
valor.save_files('path_to_save_in')
|
32
|
+
```
|
33
|
+
|
34
|
+
...and you'll notice the following files were created:
|
35
|
+
```ruby
|
36
|
+
# A.rb
|
37
|
+
class A
|
38
|
+
include Virtus.model
|
39
|
+
|
40
|
+
attribute :b, Integer
|
41
|
+
attribute :c, Array[D]
|
42
|
+
end
|
43
|
+
|
44
|
+
# D.rb
|
45
|
+
class D
|
46
|
+
include Virtus.model
|
47
|
+
|
48
|
+
attribute :d, Integer
|
49
|
+
end
|
50
|
+
|
51
|
+
# Test.rb
|
52
|
+
class Test
|
53
|
+
include Virtus.model
|
54
|
+
|
55
|
+
attribute :a, A
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
Automation, such a wondrous time saver some times.
|
60
|
+
|
61
|
+
## Installation
|
62
|
+
|
63
|
+
Add this line to your application's Gemfile:
|
64
|
+
|
65
|
+
gem 'valor'
|
66
|
+
|
67
|
+
And then execute:
|
68
|
+
|
69
|
+
$ bundle
|
70
|
+
|
71
|
+
Or install it yourself as:
|
72
|
+
|
73
|
+
$ gem install valor
|
74
|
+
|
75
|
+
## Usage
|
76
|
+
|
77
|
+
TODO: Write usage instructions here
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
1. Fork it ( http://github.com/<my-github-username>/valor/fork )
|
82
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
83
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
84
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
85
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/valor.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'valor/version'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Valor
|
5
|
+
class Generator
|
6
|
+
|
7
|
+
# name - Name of the base class
|
8
|
+
# raw_data - Either JSON or a Ruby Hash
|
9
|
+
# save_directory - If specified, will attempt to auto-save generated classes here.
|
10
|
+
def initialize(name, raw_data, save_directory = nil)
|
11
|
+
@models = {}
|
12
|
+
data = raw_data.is_a?(Hash) ? raw_data : JSON.parse(raw_data)
|
13
|
+
@models[name] = reduce_hash(data)
|
14
|
+
|
15
|
+
save_files(save_directory) if save_directory
|
16
|
+
end
|
17
|
+
|
18
|
+
# Saves classes and attributes to individual file classes
|
19
|
+
def save_files(save_directory = Dir.pwd)
|
20
|
+
raise "Invalid Directory!" unless File.directory?(save_directory)
|
21
|
+
|
22
|
+
@models.each_pair { |klass_name, attributes|
|
23
|
+
file_name = "#{save_directory}/#{klass_name}.rb"
|
24
|
+
|
25
|
+
unless File.exist?(file_name) # Make sure we're not double writing here
|
26
|
+
File.open(file_name, 'w') do |file|
|
27
|
+
file.puts "class #{klass_name}"
|
28
|
+
file.puts ' include Virtus.model'
|
29
|
+
file.puts ''
|
30
|
+
attributes.each { |attribute| file.puts " #{attribute}" }
|
31
|
+
file.puts 'end'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def reduce_hash(data)
|
40
|
+
data.each_pair.reduce([]) { |data, (key, value)|
|
41
|
+
if value.is_a?(Hash)
|
42
|
+
name = key.to_s.capitalize
|
43
|
+
@models[name] = interpret(value)
|
44
|
+
data << "attribute :#{key}, #{name}"
|
45
|
+
else
|
46
|
+
data << "attribute :#{key}, #{interpret(value)}"
|
47
|
+
end
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
def reduce_array(data)
|
52
|
+
if data.first.is_a?(Hash)
|
53
|
+
type = data.first.keys.first.to_s.capitalize
|
54
|
+
@models[type] = reduce_hash(data.first)
|
55
|
+
else
|
56
|
+
type = data.first.class
|
57
|
+
end
|
58
|
+
|
59
|
+
"Array[#{type}]"
|
60
|
+
end
|
61
|
+
|
62
|
+
def interpret(value)
|
63
|
+
case
|
64
|
+
when value.is_a?(Hash)
|
65
|
+
reduce_hash(value)
|
66
|
+
when value.is_a?(Array)
|
67
|
+
reduce_array(value)
|
68
|
+
else
|
69
|
+
klass = value.class.to_s
|
70
|
+
klass == 'Fixnum' ? 'Integer' : klass # Compensating for the difference of types
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/valor.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'valor/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "valor"
|
8
|
+
spec.version = Valor::VERSION
|
9
|
+
spec.authors = ["Brandon Weaver"]
|
10
|
+
spec.email = ["keystonelemur@gmail.com"]
|
11
|
+
spec.summary = %q{Generate Virtus Models from a Ruby hash}
|
12
|
+
spec.description = %q{A framework for automating the creation of Virtus Models from a hash}
|
13
|
+
spec.homepage = "https://www.github.com/baweaver/valor"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'json'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: valor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brandon Weaver
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-14 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A framework for automating the creation of Virtus Models from a hash
|
56
|
+
email:
|
57
|
+
- keystonelemur@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/valor.rb
|
68
|
+
- lib/valor/version.rb
|
69
|
+
- valor.gemspec
|
70
|
+
homepage: https://www.github.com/baweaver/valor
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.2.1
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Generate Virtus Models from a Ruby hash
|
94
|
+
test_files: []
|