json_to_ruby_class 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f1100e68418bc5e77bda78f5a3dd0b0d3d7d8d3
4
- data.tar.gz: a377fde3a0c6c67af718370079ed5b14880d8aa8
3
+ metadata.gz: 5313ce74144a44ca9effc173b01de5c88a3666d2
4
+ data.tar.gz: a0e3fcbd3fb98ae3307fce2efe2ec3f090200f0d
5
5
  SHA512:
6
- metadata.gz: 189216748734a6c23831cf3b58028c826902a9d80db7b1d41929f8159a77fc0218366ffba36431ef7293245ec4213eb192428dbc4d08f759f3bc56d35c528c76
7
- data.tar.gz: 056c1ada2acb95e6af673463a2bbc5940c16d85d0a1426bea21b9dfb56cad160ca827c80a21388067a0d29228a8e1f5a430ed73651b5b1e1ed57e69b736aaf01
6
+ metadata.gz: 72504284558c27ca722c3720d81047b7cd960823bf83bc5e3eac76825611da87fe0e92f355a84d050d698fe492632846e83579d24f810d72781e4c69ab4653a8
7
+ data.tar.gz: 7e2fd4f86226c557c45932490459bc0edc03bb04ca04eaa996552d49ddce6c2a9919e57f11e5214b70054fa51f743b74c0e6a1e1d931ac93c959d83b12099f61
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.2.0 (2016-08-02)
2
+
3
+ Features
4
+ * C# classes are now supported
5
+ * VB.Net classes are now supported
6
+
1
7
  ## 0.1.3 (2016-07-14)
2
8
 
3
9
  Bug fixes
data/README.md CHANGED
@@ -58,6 +58,28 @@ class Example
58
58
  end
59
59
  ```
60
60
 
61
+ Furthermore, the following types can be used to retrieve the proper classes in other languages:
62
+ * 'c#', for C# classes
63
+ * 'vb', for VB.net classes
64
+
65
+ Example use:
66
+
67
+ ```ruby
68
+ puts JsonToRubyClass.produce_models(json, 'c#')
69
+
70
+ public class Student
71
+ {
72
+ public string firstName { get; set; }
73
+ public string lastName { get; set; }
74
+ public int age { get; set; }
75
+ }
76
+
77
+ public class Example
78
+ {
79
+ public Student[] students { get; set; }
80
+ }
81
+ ```
82
+
61
83
  ## Development
62
84
 
63
85
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -66,7 +88,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
66
88
 
67
89
  ## Contributing
68
90
 
69
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json_to_ruby_class. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arcanoid/json_to_ruby_class. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
70
92
 
71
93
 
72
94
  ## License
@@ -18,7 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
-
22
21
  spec.add_dependency 'activesupport'
23
22
  spec.add_development_dependency "bundler", "~> 1.10"
24
23
  spec.add_development_dependency "rake", "~> 10.0"
@@ -1,11 +1,23 @@
1
1
  require "json_to_ruby_class/version"
2
+ require "json_to_ruby_class/ruby_converter"
3
+ require "json_to_ruby_class/c_sharp_converter"
4
+ require "json_to_ruby_class/vb_dot_net_converter"
2
5
  require 'active_support'
3
6
  require 'active_support/core_ext'
4
7
 
5
8
  module JsonToRubyClass
6
- def self.produce_models(hash)
9
+ VB_DOT_NET_LANGUAGE_TYPE = 'vb'
10
+ C_SHARP_LANGUAGE_TYPE = 'c#'
11
+ RUBY_LANGUAGE_TYPE = 'ruby'
12
+
13
+ def self.produce_models(hash, language = 'ruby')
7
14
  models_array = collect_info_from_json(hash, nil)
8
- prepare_models_from_hash models_array
15
+
16
+ case language
17
+ when C_SHARP_LANGUAGE_TYPE then CSharpConverter.prepare_c_sharp_models_from_hash models_array
18
+ when VB_DOT_NET_LANGUAGE_TYPE then VBDotNetConverter.prepare_vb_dot_net_models_from_hash models_array
19
+ else RubyConverter.prepare_ruby_models_from_hash models_array
20
+ end
9
21
  end
10
22
 
11
23
  #######
@@ -32,34 +44,20 @@ module JsonToRubyClass
32
44
  end
33
45
  end
34
46
 
35
- accessors << key
47
+ accessors << { :key => "#{key.to_s}", :type => value.class }
36
48
  end
37
49
 
38
50
  model_name_to_be_used = (model_name.nil? ? 'Example' : model_name.to_s.camelcase)
39
- accessors_to_be_used = accessors.map { |s| ":#{s.to_s.underscore}" }
40
51
 
41
52
  if (hash = existing_models_array.find { |model| model[:name] == model_name_to_be_used })
42
- hash[:accessors].push(accessors_to_be_used).flatten!.uniq!
53
+ hash[:accessors].push(accessors).flatten!.uniq!
43
54
  else
44
55
  existing_models_array << {
45
56
  :name => (model_name.nil? ? 'Example' : model_name.to_s.camelcase),
46
- :accessors => accessors_to_be_used
57
+ :accessors => accessors
47
58
  }
48
59
  end
49
60
 
50
61
  existing_models_array
51
62
  end
52
-
53
- def self.prepare_models_from_hash(models_array)
54
- model_string = ''
55
-
56
- models_array.each do |model|
57
- model_string << "class #{model[:name].singularize}\n"
58
- model_string << ' attr_accessor '
59
- model_string << model[:accessors].join(",\n ")
60
- model_string << "\nend\n\n"
61
- end
62
-
63
- model_string
64
- end
65
63
  end
@@ -0,0 +1,35 @@
1
+ module JsonToRubyClass
2
+ class CSharpConverter
3
+ #######
4
+ private
5
+ #######
6
+
7
+ def self.prepare_c_sharp_models_from_hash(models_array)
8
+ model_string = ''
9
+
10
+ models_array.each do |model|
11
+ model_string << "public class #{model[:name].singularize}\n"
12
+ model_string << "{\n"
13
+
14
+ model[:accessors].each do |accessor|
15
+ type = case accessor[:type].to_s
16
+ when 'String' then 'string'
17
+ when 'Fixnum' then 'int'
18
+ when 'Float' then 'decimal'
19
+ when 'Array' then "#{accessor[:key].singularize.camelcase}[]"
20
+ when 'TrueClass' then 'bool'
21
+ when 'FalseClass' then 'bool'
22
+ when 'Hash' then "#{accessor[:key].singularize.camelcase}"
23
+ # TODO: How could you cover an array of integers?
24
+ else accessor[:type].to_s
25
+ end
26
+
27
+ model_string << " public #{type} #{accessor[:key]} { get; set; }\n"
28
+ end
29
+ model_string << "}\n\n"
30
+ end
31
+
32
+ model_string
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ module JsonToRubyClass
2
+ class RubyConverter
3
+ #######
4
+ private
5
+ #######
6
+
7
+ def self.prepare_ruby_models_from_hash(models_array)
8
+ model_string = ''
9
+
10
+ models_array.each do |model|
11
+ model_string << "class #{model[:name].singularize}\n"
12
+ model_string << ' attr_accessor '
13
+ model_string << model[:accessors].map { |accessor| ":#{accessor[:key].underscore}" }.join(",\n ")
14
+ model_string << "\nend\n\n"
15
+ end
16
+
17
+ model_string
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ module JsonToRubyClass
2
+ class VBDotNetConverter
3
+ #######
4
+ private
5
+ #######
6
+
7
+ def self.prepare_vb_dot_net_models_from_hash(models_array)
8
+ model_string = ''
9
+
10
+ models_array.each do |model|
11
+ model_string << "Public Class #{model[:name].singularize}\n"
12
+
13
+ model[:accessors].each do |accessor|
14
+ type = case accessor[:type].to_s
15
+ when 'String' then 'String'
16
+ when 'Fixnum' then 'Integer'
17
+ when 'Float' then 'Decimal'
18
+ when 'Array' then "#{accessor[:key].singularize.camelcase}()"
19
+ when 'TrueClass' then 'Boolean'
20
+ when 'FalseClass' then 'Boolean'
21
+ when 'Hash' then "#{accessor[:key].singularize.camelcase}"
22
+ # TODO: How could you cover an array of integers?
23
+ else accessor[:type].to_s
24
+ end
25
+
26
+ model_string << " Public Property #{accessor[:key]} As #{type}\n"
27
+ end
28
+ model_string << "End Class\n\n"
29
+ end
30
+
31
+ model_string
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module JsonToRubyClass
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_to_ruby_class
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasilis Kalligas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-14 00:00:00.000000000 Z
11
+ date: 2016-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -86,6 +86,9 @@ files:
86
86
  - bin/setup
87
87
  - json_to_ruby_class.gemspec
88
88
  - lib/json_to_ruby_class.rb
89
+ - lib/json_to_ruby_class/c_sharp_converter.rb
90
+ - lib/json_to_ruby_class/ruby_converter.rb
91
+ - lib/json_to_ruby_class/vb_dot_net_converter.rb
89
92
  - lib/json_to_ruby_class/version.rb
90
93
  homepage: https://rubygems.org/gems/json_to_ruby_class
91
94
  licenses: