kilza 1.0.2 → 1.0.4

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: 19afa4576243d4b81618c7a96e3fcfc171d1e40c
4
- data.tar.gz: 5ebd96dd828845962476cd04e9d9b1c5f2c581e4
3
+ metadata.gz: 0bb3186b37f9e9108276b4e7bb4c95217e32da34
4
+ data.tar.gz: 025ba6e2147e62e5e19dbf6ba8e58be9bbffa361
5
5
  SHA512:
6
- metadata.gz: 7359efe86778e41c19a07fbd9b2e7719d73db4626c4630f1944d9ba4ab4c4576efd9c4155f5d88cdb3f720012617b4bb3a8d0ad9b88f8db6178fc131a931a2e5
7
- data.tar.gz: 08bbb522b1b72075a981a2accd3ea2d4db85629cf417d0c22eb4f871bbb5d14428322f99297e78b0941788ce467d2a8610b894ba42efd7c54bf4f8e67e54c09e
6
+ metadata.gz: 8cde20c73473da2ff456a9ab14ddc5f2e32e60dcdf4218d91a6e62aa33127994c85046904d3318143a6b00a160b22a797c54812839bf42eadfcd24994ab02ba7
7
+ data.tar.gz: c5e2fcca3ba6505713607378e8b2905730c9a296a546984f7632599de692ce604de6a400ab9b519f0dbe67cbf353a0b40ecab2e5a1de0a8ad516f0e4ce9ca41d
data/README.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Kilza
2
2
 
3
+ [![Build Status](https://travis-ci.org/Jaspion/Kilza.svg?branch=master)](https://travis-ci.org/Jaspion/Kilza)
4
+
5
+ [![Coverage Status](https://coveralls.io/repos/Jaspion/Kilza/badge.svg?branch=master&service=github)](https://coveralls.io/github/Jaspion/Kilza?branch=master)
6
+
7
+ [![Inline docs](http://inch-ci.org/github/jaspion/kilza.svg?branch=master)](http://inch-ci.org/github/jaspion/kilza)
8
+
3
9
  Ruby gem that can convert JSON strings in Objects.
4
10
 
5
11
  It supports Objective-C and Java classes. Contribuition would be appreciate.
@@ -150,8 +156,8 @@ public class Mybaseclass implements Serializable
150
156
  }
151
157
  @Override
152
158
  public int hashCode(){
153
- return (((Long)code).hashCode() +
154
- ((String)message).hashCode());
159
+ return (code.hashCode() +
160
+ message.hashCode());
155
161
  }
156
162
 
157
163
  @Override
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "kilza"
3
+ require 'bundler/setup'
4
+ require 'kilza'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
10
+ # require 'pry'
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
data/bin/kilza CHANGED
@@ -16,8 +16,8 @@ end
16
16
  module TTY
17
17
  class Prompt
18
18
  class List
19
- def numpad(value)
20
- @numpad = value
19
+ def enum(value)
20
+ @enum = value
21
21
  end
22
22
  end
23
23
  end
@@ -25,6 +25,9 @@ end
25
25
 
26
26
  class KilzaApplication
27
27
 
28
+ OBJC = 1
29
+ JAVA = 2
30
+
28
31
  def initialize
29
32
  @pastel = Pastel.new
30
33
  @prompt = TTY::Prompt.new
@@ -34,34 +37,34 @@ class KilzaApplication
34
37
  end
35
38
 
36
39
  def get_lang
37
- action = @prompt.select(@pastel.bold("Target programming language?")) do |menu|
40
+ action = @prompt.select(@pastel.bold('Target programming language?')) do |menu|
38
41
  menu.default 1
39
- menu.numpad true
42
+ menu.enum '.'
40
43
 
41
- menu.choice "Objective-C", 1
42
- menu.choice "Java", 2
44
+ menu.choice 'Objective-C', OBJC
45
+ menu.choice 'Java', JAVA
43
46
  end
44
47
  action
45
48
  end
46
49
 
47
50
  def get_json
48
- action = @prompt.select(@pastel.bold("Where is your JSON string content?")) do |menu|
51
+ action = @prompt.select(@pastel.bold('Where is your JSON string content?')) do |menu|
49
52
  menu.default 1
50
- menu.numpad true
53
+ menu.enum '.'
51
54
 
52
- menu.choice "It's online. I have an URL.", 1
53
- menu.choice "It's offline. I have the file path.", 2
55
+ menu.choice 'It\'s online. I have an URL.', 1
56
+ menu.choice 'It\'s offline. I have the file path.', 2
54
57
  end
55
58
 
56
59
  json_string = ''
57
60
  if (action == 1)
58
- url = @prompt.ask(@pastel.bold("Please, what is the URL?")) do |q|
61
+ url = @prompt.ask(@pastel.bold('Please, what is the URL?')) do |q|
59
62
  q.required true
60
63
  end
61
- @prompt.say(@pastel.bold("Ok. I'll try to get the content right now."))
64
+ @prompt.say(@pastel.bold('Ok. I\'ll try to get the content right now.'))
62
65
  json_string = read_url(url)
63
66
  else
64
- file_path = File.expand_path(@prompt.ask(@pastel.bold("Please, what is the file path? "))) do |q|
67
+ file_path = File.expand_path(@prompt.ask(@pastel.bold('Please, what is the file path? '))) do |q|
65
68
  q.required true
66
69
  end
67
70
  json_string = File.read(file_path.chomp)
@@ -69,56 +72,69 @@ class KilzaApplication
69
72
  json_string
70
73
  end
71
74
 
75
+ def generate(lang, json_string, class_basename, target_path, class_package = '')
76
+ if (lang == OBJC)
77
+ objc = Kilza::Objc.new(json_string)
78
+ objc.classes(class_basename).each { |c|
79
+ c.sources.each{ |s|
80
+ File.write(File.join(target_path, s.file_name), s.source)
81
+ }
82
+ }
83
+ elsif (lang == JAVA)
84
+ java = Kilza::Java.new(json_string)
85
+ java.classes(class_basename).each { |c|
86
+ c.package = class_package
87
+ c.sources.each{ |s|
88
+ File.write(File.join(target_path, s.file_name), s.source)
89
+ }
90
+ }
91
+ end
92
+ end
93
+
94
+ def get_target_path
95
+ target_path = @prompt.ask(@pastel.bold('Master, where would you like to save? ')).chomp
96
+ File.expand_path(target_path)
97
+ end
98
+
99
+ def get_basename
100
+ @prompt.ask(@pastel.bold('Master, which name would like to call the first class? ')).chomp
101
+ end
102
+
103
+ def get_package
104
+ @prompt.ask(@pastel.bold('What is the target Java package? ')).chomp
105
+ end
106
+
72
107
  def run
73
108
  while (not @done)
74
- action = @prompt.select(@pastel.bold("Choose your destiny?")) do |menu|
109
+ action = @prompt.select(@pastel.bold('Choose your destiny?')) do |menu|
75
110
  menu.default 1
76
- menu.numpad true
111
+ menu.enum '.'
77
112
 
78
113
  menu.choice 'I want some JSON objects.', 1
79
114
  menu.choice 'Nothing.', 2
80
115
  end
81
116
  if action == 1
82
117
  json_string = get_json
83
- target_path = @prompt.ask(@pastel.bold("Master, where would you like to save? ")).chomp
84
- target_path = File.expand_path(target_path)
85
- class_basename = @prompt.ask(@pastel.bold("Master, which name would like to call the first class? ")).chomp
118
+ target_path = get_target_path
119
+ class_basename = get_basename
86
120
  lang = get_lang
87
121
 
88
- class_package = nil
89
- if (lang == 2)
90
- class_package = @prompt.ask(@pastel.bold("What is the target Java package? ")).chomp
91
- end
122
+ class_package = get_package if (lang == JAVA)
92
123
 
93
- @prompt.say(@pastel.bold("Target: ") + target_path)
94
- @prompt.say(@pastel.bold("Base class name: ") + class_basename)
95
- @prompt.say(@pastel.bold("Target language: ") + (lang == 1 ? 'Objective-C' : 'Java'))
96
- if (not class_package.nil?)
97
- @prompt.say(@pastel.bold("Target package: ") + class_package)
124
+ @prompt.say(@pastel.bold('Target: ') + target_path)
125
+ @prompt.say(@pastel.bold('Base class name: ') + class_basename)
126
+ @prompt.say(@pastel.bold('Target language: ') + (lang == OBJC ? 'Objective-C' : 'Java'))
127
+ if not class_package.nil?
128
+ @prompt.say(@pastel.bold('Target package: ') + class_package)
98
129
  end
99
130
  ok = @prompt.yes?('Is that correct?')
100
- if (ok.nil? or ok)
101
- @prompt.say(@pastel.bold("Yes master. Now I'm going to generate all files."))
102
- @prompt.say(@pastel.bold("Berebekan Katabamba Berebekan Katabamba Berebekan Katabamba"))
103
-
104
- if (lang == 1)
105
- objc = Kilza::Objc.new(json_string)
106
- objc.classes(class_basename).each { |c|
107
- c.sources.each{ |s|
108
- File.write(File.join(target_path, s.file_name), s.source)
109
- }
110
- }
111
- else
112
- java = Kilza::Java.new(json_string)
113
- java.classes(class_basename).each { |c|
114
- c.package = class_package
115
- c.sources.each{ |s|
116
- File.write(File.join(target_path, s.file_name), s.source)
117
- }
118
- }
119
- end
120
-
121
- @prompt.say(@pastel.bold("Kikera!"))
131
+ if ok.nil? or ok
132
+ @prompt.say(@pastel.bold('Yes master. Now I\'m going to generate all files.'))
133
+ @prompt.say(@pastel.bold('Berebekan Katabamba Berebekan Katabamba Berebekan Katabamba'))
134
+
135
+ generate(lang, json_string, class_basename, target_path, class_package)
136
+
137
+ @prompt.say(@pastel.bold('Kikera!'))
122
138
  end
123
139
  else
124
140
  @done = true
data/kilza.gemspec CHANGED
@@ -4,35 +4,42 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'kilza/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "kilza"
7
+ spec.name = 'kilza'
8
8
  spec.version = Kilza::VERSION
9
- spec.authors = ["Toshiro Sugii"]
10
- spec.email = ["rtoshiro@printwtf.com"]
9
+ spec.authors = ['Toshiro Sugii']
10
+ spec.email = ['rtoshiro@printwtf.com']
11
11
 
12
- spec.summary = %q{Parses JSON objects and generates objects into other languages}
13
- spec.description = %q{Parses JSON objects and generates objects into other languages}
14
- spec.homepage = "https://github.com/Jaspion/Kilza"
15
- spec.license = "MIT"
12
+ spec.summary = 'Parses JSON and generates objects into other languages'
13
+ spec.description = %q{
14
+ Ruby gem that can convert JSON strings in Objects.
15
+ It supports Objective-C and Java classes. Contribuition would be appreciate.
16
+ }
17
+ spec.homepage = 'https://github.com/Jaspion/Kilza'
18
+ spec.license = 'MIT'
16
19
 
17
20
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
21
  # delete this section to allow pushing this gem to any host.
19
22
  if spec.respond_to?(:metadata)
20
- spec.metadata['allowed_push_host'] = "rubygems.org"
23
+ spec.metadata['allowed_push_host'] = 'rubygems.org'
21
24
  else
22
- raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
25
+ fail 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
26
  end
24
27
 
25
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
- spec.bindir = "bin"
27
- spec.executables = ["kilza"]
28
- spec.require_paths = ["lib"]
28
+ spec.files= `git ls-files -z`.split("\x0").reject { |f|
29
+ f.match(%r{^(test|spec|features)/})
30
+ }
31
+ spec.bindir = 'bin'
32
+ spec.executables = ['kilza']
33
+ spec.require_paths = ['lib']
29
34
 
30
- spec.add_development_dependency "bundler", "~> 1.10"
31
- spec.add_development_dependency "rake", "~> 10.0"
32
- spec.add_development_dependency "minitest"
35
+ spec.add_development_dependency 'bundler', '~> 1.10'
36
+ spec.add_development_dependency 'rake', '~> 10.0'
37
+ spec.add_development_dependency 'rspec'
38
+ spec.add_development_dependency 'minitest'
39
+ spec.add_development_dependency 'coveralls'
33
40
 
34
- spec.add_dependency "json"
35
- spec.add_dependency "erubis"
36
- spec.add_dependency "tty"
37
- spec.add_dependency "pastel"
41
+ spec.add_dependency 'json'
42
+ spec.add_dependency 'erubis'
43
+ spec.add_dependency 'tty'
44
+ spec.add_dependency 'pastel'
38
45
  end
data/lib/kilza.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "kilza/version"
1
+ require 'kilza/version'
2
2
 
3
3
  require 'kilza/source'
4
4
  require 'kilza/class'
@@ -7,21 +7,36 @@ require 'kilza/language'
7
7
  require 'kilza/language/objc'
8
8
  require 'kilza/language/java'
9
9
 
10
+ # Ruby class
10
11
  class String
11
- def is_number?
12
+ # Test if the string can be a number
13
+ #
14
+ # @param str [String] string to be tested
15
+ #
16
+ # @return [Boolean] true in case of success
17
+ def number?
12
18
  true if Float(self) rescue false
13
19
  end
14
20
  end
15
21
 
22
+ # Tranforms a JSON string into Objects
16
23
  module Kilza
24
+ # Removes everything except numbers and letters.
25
+ #
26
+ # @param str [String] string to be cleaned
27
+ #
28
+ # @return [String] cleaned string
17
29
  def self.clean(str)
18
- if str[0].is_number?
19
- str = '_' + str
20
- end
21
- str = str.gsub(/[^a-zA-Z0-9]/, '_')
30
+ str = '_' + str if str[0].number?
31
+ str.gsub(/[^a-zA-Z0-9]/, '_')
22
32
  end
23
33
 
34
+ # Cleans the string and make it lowercase.
35
+ #
36
+ # @param str [String] string to be cleaned
37
+ #
38
+ # @return [String] cleaned string
24
39
  def self.normalize(str)
25
- str = Kilza::clean(str).downcase
40
+ Kilza.clean(str).downcase
26
41
  end
27
42
  end
data/lib/kilza/class.rb CHANGED
@@ -1,46 +1,65 @@
1
1
  module Kilza
2
+ # Represents one single object class
2
3
  module Class
4
+ # Class name
3
5
  attr_accessor :name
4
- attr_accessor :params
6
+
7
+ # Array with all class dependecies
8
+ # Specific for each language
5
9
  attr_accessor :imports
10
+
11
+ # Array with all class properties
6
12
  attr_accessor :properties
7
13
 
8
14
  def initialize(name)
9
- @name = Kilza::normalize(name).capitalize
15
+ @name = Kilza.normalize(name).capitalize
10
16
  @properties = []
11
17
  @imports = []
12
- @params = []
13
18
  end
14
19
 
20
+ # Search for a property by name
21
+ #
22
+ # @param name [String] Property name
23
+ #
24
+ # @return [Kilza::Property] The property found or nil
15
25
  def find(name)
16
- @properties.each { |p|
17
- return p if (p.name == name)
18
- }
26
+ @properties.each { |p| return p if p.name == name }
19
27
  nil
20
28
  end
21
29
 
30
+ # Adds a new property
31
+ #
32
+ # @param property [Kilza::Property] Property to include
22
33
  def push(property)
23
34
  p = find(property.name)
24
- if p.nil?
25
- @properties.push(property)
26
- end
35
+ @properties.push(property) if p.nil?
27
36
  end
28
37
 
29
38
  def sources
39
+ fail 'It should be implemented'
40
+ end
41
+
42
+ def code(lang, file_name)
43
+ cur_path = File.expand_path(__FILE__)
44
+ erb_path = File.join(File.dirname(cur_path), 'language', lang)
45
+
46
+ path = File.join(erb_path, file_name + '.erb')
47
+ eruby = Erubis::Eruby.new(File.read(path))
48
+
49
+ s = Kilza::Source.new
50
+ s.source = eruby.result(binding)
51
+ s.file_name = @name.capitalize + '.' + file_name
52
+ s
30
53
  end
31
54
 
32
- def to_hash
55
+ def to_s
33
56
  properties = []
34
- @properties.each { |p|
35
- properties.push(p.to_hash)
36
- }
37
- hash = {
38
- :name => @name,
39
- :imports => @imports,
40
- :params => @params,
41
- :properties => properties
42
- }
43
- hash
57
+ @properties.each { |p| properties.push(p.to_s) }
58
+ {
59
+ name: @name,
60
+ imports: @imports,
61
+ properties: properties
62
+ }.to_s
44
63
  end
45
64
  end
46
65
  end
@@ -2,79 +2,134 @@ require 'json'
2
2
  require 'erubis'
3
3
 
4
4
  module Kilza
5
+ # Represents an program language
5
6
  module Language
7
+ # Array with all Class classes
6
8
  attr_accessor :classes
9
+
10
+ # Name used to represent the first generated class
7
11
  attr_accessor :base_name
12
+
13
+ # JSON that will be used to generate objects
8
14
  attr_accessor :json_string
9
15
 
10
- attr_accessor :reserved_words # words that will receive an undescore before property name
11
- attr_accessor :equal_keys # array with all properties that will be used to compare other objects
12
- attr_accessor :types # hash table with all language types mapped to target language
16
+ # String that will be used to prefix reserved words
17
+ attr_accessor :reserved_delimiter
18
+
19
+ # Words that will receive an undescore before property name
20
+ attr_accessor :reserved_words
21
+
22
+ # Array with all properties that will be used to compare other objects
23
+ attr_accessor :equal_keys
24
+
25
+ # Hash table with all language types mapped to target language
26
+ attr_accessor :types
13
27
 
14
28
  def initialize(json_string)
15
29
  @json_string = json_string
16
30
  @classes = []
17
31
  @types = {}
18
32
  @reserved_words = []
33
+ @reserved_delimiter = '_'
19
34
  @equal_keys = []
20
35
  end
21
36
 
22
- def get_class(name)
23
- name = "_" + name if not @reserved_words.index(name).nil?
37
+ # Returns all available classes
38
+ #
39
+ # @param base_name [String] First class name
40
+ #
41
+ # @return [Array] All available classes
42
+ def classes(base_name)
43
+ hash = JSON.parse(json_string)
44
+ hash = { base_name + 'Object' => hash } if hash.is_a?(Array)
45
+ parse_hash(base_name, hash)
46
+ @classes
47
+ end
48
+
49
+ protected
50
+
51
+ # Creates a new Class object and checks for valid name
52
+ #
53
+ # @param name [String] name of the class to be created
54
+ #
55
+ # @return [Kilza::Class] new class
56
+ def clazz(name)
57
+ name = @reserved_delimiter + name unless @reserved_words.index(name).nil?
24
58
  Class.new(name)
25
59
  end
26
60
 
27
- def get_property(name, type, is_array, is_key)
28
- name = "_" + name if not @reserved_words.index(name).nil?
29
- Property.new(name, type, is_array, is_key.nil?)
61
+ # Creates a new Property object and checks for valid name
62
+ #
63
+ # @param name [String] name of the property to be created
64
+ # @param type [String] type of the property based on class name
65
+ # @param array [Boolean] indicates if this property represents an array
66
+ # @param key [Boolean] indicates if this property can be used to compare
67
+ # objects
68
+ #
69
+ # @return [Kilza::Property] new property
70
+ def property(name, type, array, key)
71
+ name = @reserved_delimiter + name unless @reserved_words.index(name).nil?
72
+ Property.new(name, type, array, key.nil?)
30
73
  end
31
74
 
75
+ # Searches for a Kilza::Class inside @classes
76
+ # and creates a new one if it could not be found
77
+ #
78
+ # @param name [String] class name to find
79
+ #
80
+ # @return [Kilza::Class] class with the specified name
32
81
  def find(name)
33
- @classes.each { |cl|
34
- return cl if (cl.name == name)
35
- }
36
- @classes.push(get_class(name))
37
- return @classes.last
82
+ name = Kilza.normalize(name).capitalize
83
+ @classes.each { |cl| return cl if cl.name == name }
84
+ @classes.push(clazz(name))
85
+ @classes.last
38
86
  end
39
87
 
40
- def parse(hash, class_name)
41
- current_class = find(class_name)
42
- hash.each { |property_name, value|
43
- type = value.class.name.split('::').last.downcase
44
-
45
- case type
46
- when "array"
47
- if (value.length == 0)
48
- current_class.push(get_property(property_name, 'object', true, @equal_keys.index(property_name)))
49
- parse(value, property_name)
50
- else
51
- value.each { |el|
52
- if (el.is_a?(Array) or el.is_a?(Hash))
53
- parse(el, property_name)
54
- current_class.push(get_property(property_name, 'object', true, @equal_keys.index(property_name)))
55
- else
56
- type = el.class.name.split('::').last.downcase
57
- current_class.push(get_property(property_name, type, true, @equal_keys.index(property_name)))
58
- end
59
- }
60
- end
61
- when "hash"
62
- current_class.push(get_property(property_name, 'object', false, @equal_keys.index(property_name)))
63
- parse(value, property_name)
64
- else
65
- current_class.push(get_property(property_name, type, false, @equal_keys.index(property_name)))
66
- end
67
- }
88
+ # Parses an element value an verify if it should create a new Class
89
+ # inside @classes
90
+ #
91
+ # @param class_name [String] Name of the class the element is inside
92
+ # @param name [String] The element name
93
+ # @param value [Any] The element value
94
+ # @param array [Boolean] Indicates the element is inside an Array
95
+ def parse_el(class_name, name, value, array = false)
96
+ type = value.class.name.split('::').last.downcase
97
+
98
+ return parse_array(class_name, name, value) if type == 'array'
99
+
100
+ cur_class = find(class_name)
101
+ key = @equal_keys.index(name)
102
+ cur_class.push(property(name, type, array, key))
103
+
104
+ parse_hash(name, value) if type == 'hash'
68
105
  end
69
106
 
70
- def classes(base_name)
71
- hash = JSON.parse(json_string)
72
- if (hash.is_a?(Array))
73
- hash = { base_name + "Object" => hash }
107
+ # Parses an hash calling parse_el for each element
108
+ #
109
+ # @param class_name [String] Name of the class the hash is inside
110
+ # @param hash [Hash] The hash value
111
+ def parse_hash(class_name, hash)
112
+ hash.each do |property_name, value|
113
+ parse_el(class_name, property_name, value)
74
114
  end
75
- parse(hash, base_name)
76
- return @classes
77
115
  end
78
116
 
117
+ # Parses an element that represents an array calling parse_el
118
+ # for each element.
119
+ # If the array is empty, it creates a new class to represent
120
+ # each array's element.
121
+ #
122
+ # @param class_name [String] Name of the class the array is inside
123
+ # @param name [String] The element name
124
+ # @param value [Any] The element value
125
+ def parse_array(class_name, name, value)
126
+ if value.length == 0
127
+ parse_el(class_name, name, nil, true)
128
+ else
129
+ value.each do |val|
130
+ parse_el(class_name, name, val, true)
131
+ end
132
+ end
133
+ end
79
134
  end
80
135
  end
@@ -2,10 +2,10 @@ require 'date'
2
2
 
3
3
  module Kilza
4
4
  class Java
5
-
6
5
  class Class
7
6
  include Kilza::Class
8
7
 
8
+ # Represents the Java class package
9
9
  attr_accessor :package
10
10
 
11
11
  def initialize(name, package = nil)
@@ -14,77 +14,58 @@ module Kilza
14
14
  end
15
15
 
16
16
  def sources
17
- cur_path = File.expand_path(__FILE__)
18
- java_path = File.join(File.dirname(cur_path), File.basename(cur_path, '.rb'), "java.erb")
19
- eruby_java = Erubis::Eruby.new(File.read(java_path))
20
-
21
- java = Kilza::Source.new
22
- java.source = eruby_java.result(binding)
23
- java.file_name = @name.capitalize + ".java"
24
-
25
- result = [
26
- java
27
- ]
28
- return result
17
+ [code('java', 'java')]
29
18
  end
30
19
  end
31
-
32
20
  end
33
21
  end
34
22
 
35
23
  module Kilza
24
+ # Objective-C Language parser
36
25
  class Java
37
26
  include Kilza::Language
38
27
 
39
28
  def initialize(json_string)
40
29
  super(json_string)
41
30
 
42
- @reserved_words = [
43
- "abstract", "continue", "for", "new", "switch", "assert", "default", "goto",
44
- "package", "synchronized", "boolean", "do", "if", "private", "this", "break", "double", "implements",
45
- "protected", "throw", "byte", "else", "import", "public", "throws", "case", "enum", "instanceof",
46
- "null", "return", "transient", "catch", "extends", "int", "short", "try", "char", "final", "interface", "static",
47
- "void", "class", "finally", "long", "strictfp", "volatile", "const", "float", "native", "super", "while"
48
- ]
31
+ @reserved_words = %w(
32
+ abstract continue for new switch assert default goto,
33
+ package synchronized boolean do if private this break double implements,
34
+ protected throw byte else import public throws case enum instanceof,
35
+ null return transient catch extends int short try char final interface static,
36
+ void class finally long strictfp volatile const float native super while
37
+ )
49
38
 
50
39
  @types = {
51
- "nilclass" => "Object",
52
- "string" => "String",
53
- "fixnum" => "Long",
54
- "float" => "Double",
55
- "falseclass" => "Boolean",
56
- "trueclass" => "Boolean",
57
- "object" => "Object"
40
+ 'nilclass' => 'Object',
41
+ 'string' => 'String',
42
+ 'fixnum' => 'Long',
43
+ 'float' => 'Double',
44
+ 'falseclass' => 'Boolean',
45
+ 'trueclass' => 'Boolean',
46
+ 'hash' => 'Object'
58
47
  }
59
48
 
60
- @equal_keys = [
61
- "id",
62
- "identifier",
63
- "uid"
64
- ]
49
+ @equal_keys = %w(id identifier uid)
65
50
  end
66
51
 
67
- def get_class(name)
52
+ def clazz(name)
68
53
  Kilza::Java::Class.new(name)
69
54
  end
70
55
 
71
- def parse(hash, class_name)
72
- super(hash, class_name)
56
+ def classes(class_name)
57
+ super(class_name)
73
58
 
74
- @classes.each { |cl|
75
- cl.properties.each { |pr|
76
- if pr.is_object?
77
- pr.type = pr.name.capitalize
78
- end
59
+ @classes.each do |cl|
60
+ cl.properties.each do |pr|
61
+ pr.type = pr.name.capitalize if pr.object? || (pr.array? && pr.null?)
79
62
 
80
- if pr.is_array?
81
- cl.imports.push("import java.util.ArrayList;") if cl.imports.index("import java.util.ArrayList;").nil?
82
- end
63
+ cl.imports.push('import java.util.ArrayList;') if pr.array? &&
64
+ cl.imports.index('import java.util.ArrayList;').nil?
83
65
 
84
- pr.type = @types[pr.type] if not @types[pr.type].nil?
85
- }
86
- }
66
+ pr.type = @types[pr.type] unless @types[pr.type].nil?
67
+ end
68
+ end
87
69
  end
88
-
89
70
  end
90
71
  end
@@ -20,7 +20,7 @@ public class <%= @name %> implements Serializable
20
20
  <% for @property in @properties %>
21
21
  @Expose
22
22
  @SerializedName(FIELD_<%= @property.name.upcase %>)
23
- <% if @property.is_array? %>
23
+ <% if @property.array? %>
24
24
  private ArrayList<<%= @property.type %>> <%= @property.name %>;
25
25
  <% else %>
26
26
  private <%= @property.type %> <%= @property.name %>;
@@ -51,13 +51,13 @@ public class <%= @name %> implements Serializable
51
51
  protected void parseObject(JSONObject object)
52
52
  {
53
53
  <% for @property in @properties %>
54
- <% if @property.is_array? %>
54
+ <% if @property.array? %>
55
55
  if (object.optJSONArray(FIELD_<%= @property.name.upcase %>) != null)
56
56
  {
57
57
  this.<%= @property.name %> = new ArrayList<>();
58
58
  JSONArray <%= @property.name %>JsonArray = object.optJSONArray(FIELD_<%= @property.name.upcase %>);
59
59
  for (int i = 0; i < <%= @property.name %>JsonArray.length(); i++) {
60
- <% if @property.is_object? %>
60
+ <% if @property.object? || @property.null? %>
61
61
  JSONObject <%= @property.name %> = <%= @property.name %>JsonArray.optJSONObject(i);
62
62
  <% else %>
63
63
  <%= @property.type %> <%= @property.name %> = <%= @property.name %>JsonArray.optJSON<%= @property.type %>(i);
@@ -66,10 +66,10 @@ public class <%= @name %> implements Serializable
66
66
  }
67
67
  }
68
68
  <% else %>
69
- <% if @property.is_object? %>
69
+ <% if @property.object? %>
70
70
  this.<%= @property.name %> = new <%= @property.type %>(object.optJSONObject(FIELD_<%= @property.name.upcase %>));
71
71
  <% else %>
72
- <% if @property.is_nil? %>
72
+ <% if @property.null? %>
73
73
  this.<%= @property.name %> = object.opt(FIELD_<%= @property.name.upcase %>);
74
74
  <% else %>
75
75
  this.<%= @property.name %> = object.opt<%= @property.type %>(FIELD_<%= @property.name.upcase %>);
@@ -80,14 +80,16 @@ public class <%= @name %> implements Serializable
80
80
  }
81
81
 
82
82
  <% for @property in @properties %>
83
- public void set<%= @property.name.capitalize %>(<%= @property.is_array? ? "ArrayList<" + @property.type + ">" : @property.type %> value) {
83
+ public void set<%= @property.name.capitalize %>(<%= @property.array? ? "ArrayList<" + @property.type + ">" : @property.type %> value) {
84
84
  this.<%= @property.name %> = value;
85
85
  }
86
86
 
87
- <% if @property.is_boolean? %>
87
+ <% if @property.array? %>
88
+ public ArrayList<<%= @property.type %>> get<%= @property.name.capitalize %>() {
89
+ <% elsif @property.boolean? %>
88
90
  public <%= @property.type %> is<%= @property.name.capitalize %>() {
89
91
  <% else %>
90
- public <%= @property.is_array? ? "ArrayList<" + @property.type + ">" : @property.type %> get<%= @property.name.capitalize %>() {
92
+ public <%= @property.type %> get<%= @property.name.capitalize %>() {
91
93
  <% end %>
92
94
  return this.<%= @property.name %>;
93
95
  }
@@ -97,8 +99,8 @@ public class <%= @name %> implements Serializable
97
99
  @eq = []
98
100
  @hs = []
99
101
  for @property in @properties
100
- @eq.push("((#{@name}) obj).get#{@property.name.capitalize}().equals(#{@property.name})") if (@property.is_key)
101
- @hs.push("(#{@property.name}).hashCode()") if (@property.is_key)
102
+ @eq.push("((#{@name}) obj).get#{@property.name.capitalize}().equals(#{@property.name})") if (@property.key)
103
+ @hs.push("#{@property.name}.hashCode()") if (@property.key)
102
104
  end
103
105
  %>
104
106
  <% if @eq.length > 0 %>
@@ -2,93 +2,69 @@ require 'date'
2
2
 
3
3
  module Kilza
4
4
  class Objc
5
-
6
5
  class Class
7
6
  include Kilza::Class
8
7
 
9
8
  def sources
10
- cur_path = File.expand_path(__FILE__)
11
- m_path = File.join(File.dirname(cur_path), File.basename(cur_path, '.rb'), "m.erb")
12
- h_path = File.join(File.dirname(cur_path), File.basename(cur_path, '.rb'), "h.erb")
13
-
14
- eruby_m = Erubis::Eruby.new(File.read(m_path))
15
- eruby_h = Erubis::Eruby.new(File.read(h_path))
16
-
17
- m = Kilza::Source.new
18
- m.source = eruby_m.result(binding)
19
- m.file_name = @name.capitalize + ".m"
20
-
21
- h = Kilza::Source.new
22
- h.source = eruby_h.result(binding)
23
- h.file_name = @name.capitalize + ".h"
24
-
25
- result = [
26
- h,m
27
- ]
28
- return result
9
+ [code('objc', 'h'), code('objc', 'm')]
29
10
  end
30
11
  end
31
-
32
12
  end
33
13
  end
34
14
 
35
15
  module Kilza
16
+ # Objective-C Language parser
36
17
  class Objc
37
18
  include Kilza::Language
38
19
 
39
20
  def initialize(json_string)
40
21
  super(json_string)
41
22
 
42
- @reserved_words = [
43
- "auto", "break", "case", "char", "const", "continue",
44
- "class", "default", "do", "double", "else", "enum", "extern", "float", "for",
45
- "goto", "if", "id", "implementation", "inline", "int", "interface", "long",
46
- "nonatomic", "property", "protocol", "readonly", "readwrite", "register",
47
- "restrict", "retain", "return", "short", "signed", "sizeof", "static", "strong",
48
- "struct", "switch", "typedef", "union", "unsafe_unretained", "unsigned", "void",
49
- "volatile", "weak", "while", "_bool", "_complex", "_imaginary", "sel", "imp",
50
- "bool", "nil", "yes", "no", "self", "super", "__strong", "__weak", "oneway",
51
- "in", "out", "inout", "bycopy", "byref"
52
- ]
23
+ @reserved_delimiter = '_my'
24
+
25
+ @reserved_words = %w(
26
+ auto break case char const continue,
27
+ class default do double else enum extern float for,
28
+ goto if id implementation inline int interface long,
29
+ new nonatomic property protocol readonly readwrite register,
30
+ restrict retain return short signed sizeof static strong,
31
+ struct switch typedef union unsafe_unretained unsigned void,
32
+ volatile weak while _bool _complex _imaginary sel imp,
33
+ bool nil yes no self super __strong __weak oneway,
34
+ in out inout bycopy byref
35
+ )
53
36
 
54
37
  @types = {
55
- "nilclass" => "id",
56
- "string" => "NSString *",
57
- "fixnum" => "NSNumber *",
58
- "float" => "NSNumber *",
59
- "falseclass" => "NSNumber *",
60
- "trueclass" => "NSNumber *",
61
- "object" => "NSObject *"
38
+ 'nilclass' => 'id',
39
+ 'string' => 'NSString *',
40
+ 'fixnum' => 'NSNumber *',
41
+ 'float' => 'NSNumber *',
42
+ 'falseclass' => 'NSNumber *',
43
+ 'trueclass' => 'NSNumber *',
44
+ 'hash' => 'NSObject *'
62
45
  }
63
46
 
64
- @equal_keys = [
65
- "id",
66
- "identifier",
67
- "uid"
68
- ]
47
+ @equal_keys = %w(id identifier uid)
69
48
  end
70
49
 
71
- def get_class(name)
50
+ def clazz(name)
72
51
  Kilza::Objc::Class.new(name)
73
52
  end
74
53
 
75
- def parse(hash, class_name)
76
- super(hash, class_name)
77
-
78
- @classes.each { |cl|
79
- cl.properties.each { |pr|
80
- if pr.is_object?
81
- cl.imports.push("#import \"" + pr.name.capitalize + ".h\"")
82
- end
54
+ def classes(class_name)
55
+ super(class_name)
83
56
 
84
- if pr.is_array?
85
- pr.type = "NSMutableArray *"
57
+ @classes.each do |cl|
58
+ cl.properties.each do |pr|
59
+ if pr.object? || (pr.array? && pr.null?)
60
+ pr.type = pr.name.capitalize + ' *'
61
+ cl.imports.push("#import \"#{pr.name.capitalize}.h\"")
86
62
  end
87
63
 
88
- pr.type = @types[pr.type] if not @types[pr.type].nil?
89
- }
90
- }
64
+ pr.type = 'NSMutableArray *' if pr.array?
65
+ pr.type = @types[pr.type] unless @types[pr.type].nil?
66
+ end
67
+ end
91
68
  end
92
-
93
69
  end
94
70
  end
@@ -8,7 +8,7 @@
8
8
  #import <Foundation/Foundation.h>
9
9
 
10
10
  <% for @property in @properties %>
11
- <% if @property.is_object? %>
11
+ <% if @property.object? && !@property.array? %>
12
12
  @class <%= @property.name.capitalize %>;
13
13
  <% end %>
14
14
  <% end %>
@@ -57,9 +57,9 @@ NSString * const k<%= @name %><%= @property.name.capitalize %> = @"<%= @property
57
57
  if (self && [dict isKindOfClass:[NSDictionary class]])
58
58
  {
59
59
  <% for @property in @properties %>
60
- <% if @property.is_object? %>
60
+ <% if @property.object? || (@property.null? && @property.array?) %>
61
61
  NSObject *obj<%= @property.name.capitalize %> = [dict objectForKey:k<%= @name %><%= @property.name.capitalize %>];
62
- <% if @property.is_array? %>
62
+ <% if @property.array? %>
63
63
  if ([obj<%= @property.name.capitalize %> isKindOfClass:[NSArray class]])
64
64
  {
65
65
  NSMutableArray *list<%= @property.name.capitalize %> = [NSMutableArray array];
@@ -88,8 +88,8 @@ NSString * const k<%= @name %><%= @property.name.capitalize %> = @"<%= @property
88
88
  NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
89
89
 
90
90
  <% for @property in @properties %>
91
- <% if @property.is_object? %>
92
- <% if @property.is_array? %>
91
+ <% if @property.object? || (@property.null? && @property.array?) %>
92
+ <% if @property.array? %>
93
93
  NSMutableArray *tempArray<%= @property.name.capitalize %> = [NSMutableArray array];
94
94
  for (NSObject *subArray in self.<%= @property.name %>) {
95
95
  if ([subArray respondsToSelector:@selector(dictionaryRepresentation)]) {
@@ -1,58 +1,62 @@
1
1
  module Kilza
2
+ # Represents a single Class property
2
3
  class Property
4
+ # Normalized property name
5
+ # Starts with _ or alphanumeric character
6
+ # and doesn't contain any special character
3
7
  attr_accessor :name
8
+
9
+ # Original JSON property name
4
10
  attr_accessor :original_name
11
+
12
+ # Ruby string type
13
+ # Can be object, fixnum, float, falseclass, trueclass and nilclass
5
14
  attr_accessor :type
6
- attr_accessor :params
7
- attr_accessor :is_array
8
- attr_accessor :is_key
9
15
 
10
- def initialize(name, type, is_array, is_key)
11
- @name = Kilza::normalize(name)
16
+ # Indicates if the property represents an array of objects
17
+ attr_accessor :array
18
+ alias_method :array?, :array
19
+
20
+ # Indicates if the property should be used for comparing purposes
21
+ # Used to compare if one object is equal to another one
22
+ attr_accessor :key
23
+
24
+ def initialize(name, type, array, key)
25
+ @name = Kilza.normalize(name)
12
26
  @original_name = name
13
27
  @type = type
14
- @is_array = is_array
15
- @is_key = is_key
16
- end
17
-
18
- def is_array?
19
- @is_array
28
+ @array = array
29
+ @key = key
30
+ @original_type = type
20
31
  end
21
32
 
22
- def is_object?
23
- @type == 'object' or @type == @name.capitalize
33
+ def object?
34
+ @original_type == 'hash'
24
35
  end
25
36
 
26
- def is_fixnum?
27
- @type == 'fixnum'
37
+ def fixnum?
38
+ @original_type == 'fixnum'
28
39
  end
29
40
 
30
- def is_boolean?
31
- @type == 'trueclass' or @type == 'falseclass'
41
+ def boolean?
42
+ @original_type == 'trueclass' || @original_type == 'falseclass'
32
43
  end
33
44
 
34
- def is_float?
35
- @type == 'float'
45
+ def float?
46
+ @original_type == 'float'
36
47
  end
37
48
 
38
- def is_nil?
39
- @type == 'nilclass'
49
+ def null?
50
+ @original_type == 'nilclass'
40
51
  end
41
52
 
42
- def to_hash
43
- hash = {
44
- :name => @name,
45
- :original_name => @original_name,
46
- :type => @type,
47
- :params => @params,
48
- :is_array? => @is_array,
49
- :is_object? => is_object?,
50
- :is_fixnum? => is_fixnum?,
51
- :is_boolean? => is_boolean?,
52
- :is_float? => is_float?,
53
- :is_nil? => is_nil?
54
- }
55
- hash
53
+ def to_s
54
+ {
55
+ name: @name,
56
+ original_name: @original_name,
57
+ type: @type,
58
+ array?: @array
59
+ }.to_s
56
60
  end
57
61
  end
58
62
  end
data/lib/kilza/source.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  module Kilza
2
+ # Represents one single source file
2
3
  class Source
4
+ # The file name
3
5
  attr_accessor :file_name
6
+
7
+ # The source code of the file
4
8
  attr_accessor :source
5
9
  end
6
10
  end
data/lib/kilza/version.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # Tranforms a JSON string into Objects
1
2
  module Kilza
2
- VERSION = "1.0.2"
3
+ VERSION = '1.0.4'
3
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kilza
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshiro Sugii
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-10 00:00:00.000000000 Z
11
+ date: 2016-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.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'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: minitest
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - '>='
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: json
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +136,8 @@ dependencies:
108
136
  - - '>='
109
137
  - !ruby/object:Gem::Version
110
138
  version: '0'
111
- description: Parses JSON objects and generates objects into other languages
139
+ description: "\n Ruby gem that can convert JSON strings in Objects.\n It supports
140
+ Objective-C and Java classes. Contribuition would be appreciate.\n "
112
141
  email:
113
142
  - rtoshiro@printwtf.com
114
143
  executables:
@@ -162,5 +191,5 @@ rubyforge_project:
162
191
  rubygems_version: 2.0.14
163
192
  signing_key:
164
193
  specification_version: 4
165
- summary: Parses JSON objects and generates objects into other languages
194
+ summary: Parses JSON and generates objects into other languages
166
195
  test_files: []