lib/jaspion/kilza/class.rb in jaspion-kilza-1.1.1 vs lib/jaspion/kilza/class.rb in jaspion-kilza-1.1.2

- old
+ new

@@ -3,33 +3,33 @@ # Represents one single object class module Class # Class name attr_accessor :name - # Array with all class dependecies - # Specific for each language - attr_accessor :imports - # Array with all class properties attr_accessor :properties # Initializes a Class object # # @param name [String] Class Name def initialize(name) - @name = Kilza.clean(name) - @name[0] = @name[0].capitalize + @name = Jaspion::Kilza::Class.normalize(name) + # @name[0] = @name[0].capitalize @properties = [] @imports = [] end + def imports + @imports.sort.separate.flatten + end + # Adds a new property # # @param property [Kilza::Property] Property to include def push(property) index = @properties.index(property) - if !index.nil? + unless index.nil? current = @properties[index] @properties[index] = update(current, property) else @properties.push(property) end @@ -66,10 +66,29 @@ imports: @imports, properties: properties }.to_s end + # Adds an new import statement + # + # @param import [String|Array] The whole statement that has to be printted + # It can be an Array + def push_import(import) + import = [import] if import.is_a? String + index = @imports.index(import) + @imports.push(import) if index.nil? + end + + # Removes an new import statement + # + # @param import [String|Array] The statement to be removed + def delete_import(import) + import = [import] if import.is_a? String + i = @imports.index(import) + @imports.delete_at(i) unless i.nil? + end + protected # Compares two properties and fill the src property with relevant # dst property values. If src.type is nilclass and dst.type not, replaces # it with dst.type @@ -81,9 +100,24 @@ src.type = dst.type if src.null? && !dst.null? src.original_type = dst.original_type if src.null? && !dst.null? src.array = dst.array unless src.array? src.key = dst.key unless src.key src + end + + # Removes everything except numbers and letters + # and removes all _ at the beginning and capitalizes the first character + # + # @param str [String] string to be cleaned + # + # @return [String] cleaned string + def self.normalize(str) + return if str.nil? + str = '_' + str if str[0].number? + str = str.gsub(/[^a-zA-Z0-9]/, '') + str = str.gsub(/_*(.+)/) { $1 } + str[0] = str[0].capitalize + str end end end end