Module: Kilza::Language
Instance Attribute Summary collapse
-
#base_name ⇒ Object
Returns the value of attribute base_name.
-
#classes(base_name) ⇒ Object
Returns the value of attribute classes.
-
#equal_keys ⇒ Object
array with all properties that will be used to compare other objects.
-
#json_string ⇒ Object
Returns the value of attribute json_string.
-
#reserved_words ⇒ Object
words that will receive an undescore before property name.
-
#types ⇒ Object
hash table with all language types mapped to target language.
Instance Method Summary collapse
- #find(name) ⇒ Object
- #get_class(name) ⇒ Object
- #get_property(name, type, is_array, is_key) ⇒ Object
- #initialize(json_string) ⇒ Object
- #parse(hash, class_name) ⇒ Object
Instance Attribute Details
#base_name ⇒ Object
Returns the value of attribute base_name.
7 8 9 |
# File 'lib/kilza/language.rb', line 7 def base_name @base_name end |
#classes(base_name) ⇒ Object
Returns the value of attribute classes.
6 7 8 |
# File 'lib/kilza/language.rb', line 6 def classes @classes end |
#equal_keys ⇒ Object
array with all properties that will be used to compare other objects
11 12 13 |
# File 'lib/kilza/language.rb', line 11 def equal_keys @equal_keys end |
#json_string ⇒ Object
Returns the value of attribute json_string.
8 9 10 |
# File 'lib/kilza/language.rb', line 8 def json_string @json_string end |
#reserved_words ⇒ Object
words that will receive an undescore before property name
10 11 12 |
# File 'lib/kilza/language.rb', line 10 def reserved_words @reserved_words end |
#types ⇒ Object
hash table with all language types mapped to target language
12 13 14 |
# File 'lib/kilza/language.rb', line 12 def types @types end |
Instance Method Details
#find(name) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/kilza/language.rb', line 32 def find(name) @classes.each { |cl| return cl if (cl.name == name) } @classes.push(get_class(name)) return @classes.last end |
#get_class(name) ⇒ Object
22 23 24 25 |
# File 'lib/kilza/language.rb', line 22 def get_class(name) name = "_" + name if not @reserved_words.index(name).nil? Class.new(name) end |
#get_property(name, type, is_array, is_key) ⇒ Object
27 28 29 30 |
# File 'lib/kilza/language.rb', line 27 def get_property(name, type, is_array, is_key) name = "_" + name if not @reserved_words.index(name).nil? Property.new(name, type, is_array, is_key.nil?) end |
#initialize(json_string) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/kilza/language.rb', line 14 def initialize(json_string) @json_string = json_string @classes = [] @types = {} @reserved_words = [] @equal_keys = [] end |
#parse(hash, class_name) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/kilza/language.rb', line 40 def parse(hash, class_name) current_class = find(class_name) hash.each { |property_name, value| type = value.class.name.split('::').last.downcase case type when "array" if (value.length == 0) current_class.push(get_property(property_name, 'object', true, @equal_keys.index(property_name))) parse(value, property_name) else value.each { |el| if (el.is_a?(Array) or el.is_a?(Hash)) parse(el, property_name) current_class.push(get_property(property_name, 'object', true, @equal_keys.index(property_name))) else type = el.class.name.split('::').last.downcase current_class.push(get_property(property_name, type, true, @equal_keys.index(property_name))) end } end when "hash" current_class.push(get_property(property_name, 'object', false, @equal_keys.index(property_name))) parse(value, property_name) else current_class.push(get_property(property_name, type, false, @equal_keys.index(property_name))) end } end |