module Jaspion::Kilza::Class

Represents one single object class

Attributes

name[RW]

Class name

properties[RW]

Array with all class properties

Public Class Methods

new(name) click to toggle source

Initializes a Class object

@param name [String] Class Name

# File lib/jaspion/kilza/class.rb, line 14
def initialize(name)
  @name = Jaspion::Kilza::Class.normalize(name)
  # @name[0] = @name[0].capitalize
  @properties = []
  @imports = []
end

Protected Class Methods

normalize(str) click to toggle source

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

# File lib/jaspion/kilza/class.rb, line 113
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

Public Instance Methods

code(lang, file_name) click to toggle source

Returns the #Source object of this Class.

@param lang [String] Language name (java, objc, …) @param file_name [String] Source file name

@return [Kilza::Source] Source object of this Class

# File lib/jaspion/kilza/class.rb, line 48
def code(lang, file_name)
  cur_path = File.expand_path(__FILE__)
  erb_path = File.join(File.dirname(cur_path), 'language', lang)

  path = File.join(erb_path, file_name + '.erb')
  eruby = Erubis::Eruby.new(File.read(path))

  s = Kilza::Source.new
  s.source = eruby.result(binding)
  s.file_name = @name + '.' + file_name
  s
end
delete_import(import) click to toggle source

Removes an new import statement

@param import [String|Array] The statement to be removed

# File lib/jaspion/kilza/class.rb, line 84
def delete_import(import)
  import = [import] if import.is_a? String
  i = @imports.index(import)
  @imports.delete_at(i) unless i.nil?
end
imports() click to toggle source
# File lib/jaspion/kilza/class.rb, line 21
def imports
  @imports.sort.separate.flatten
end
push(property) click to toggle source

Adds a new property

@param property [Kilza::Property] Property to include

# File lib/jaspion/kilza/class.rb, line 28
def push(property)
  index = @properties.index(property)
  unless index.nil?
    current = @properties[index]
    @properties[index] = update(current, property)
  else
    @properties.push(property)
  end
end
push_import(import) click to toggle source

Adds an new import statement

@param import [String|Array] The whole statement that has to be printted

It can be an Array
# File lib/jaspion/kilza/class.rb, line 75
def push_import(import)
  import = [import] if import.is_a? String
  index = @imports.index(import)
  @imports.push(import) if index.nil?
end
sources() click to toggle source
# File lib/jaspion/kilza/class.rb, line 38
def sources
  fail 'It should be implemented'
end
to_s() click to toggle source
# File lib/jaspion/kilza/class.rb, line 61
def to_s
  properties = []
  @properties.each { |p| properties.push(p.to_s) }
  {
    name: @name,
    imports: @imports,
    properties: properties
  }.to_s
end

Protected Instance Methods

update(src, dst) click to toggle source

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

@param property [Kilza::Property] Property to include

@return [Kilza::Property] src property with new values

# File lib/jaspion/kilza/class.rb, line 99
def update(src, dst)
  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