class Object

Public Instance Methods

assist_line(field) click to toggle source
# File lib/languages/objc/objc.rb, line 62
def assist_line(field)
  return nil if field[:type].length <= 1
  elements = []
  field[:type].reverse.each do |type|
    elements.push case
      when is_array?(type)
        "array"
      when is_dict?(type)
        "dict"
      when type == "talkobject"
        "TalkObject"
      when is_native?(type)
        "native"
      else
        truncated_name(type)
    end
  end

  stringified = elements.map { |e| "@\"#{e}\"" }
  "@[#{stringified.join(",")}]"
end
autogenerated_warning() click to toggle source
# File lib/languages/cs/cs.rb, line 23
def autogenerated_warning
  <<-AUTOGEN_DONE
/* Autogenerated from Talk
** Please do not edit this file directly. Instead, modify the underlying .talk files. */
  AUTOGEN_DONE
end
class_field_maps(cls) click to toggle source
# File lib/languages/objc/objc.rb, line 94
def class_field_maps(cls)
  @target[:map].select { |map| map[:class_name] == truncated_name(cls[:name]) }
end
class_has_bigdec(cls) click to toggle source
# File lib/languages/java/java.rb, line 59
def class_has_bigdec(cls)
  cls[:field].each do |f|
    return true if f[:type][0] == "uint64"
  end

  false
end
class_has_map(cls) click to toggle source
# File lib/languages/cs/cs.rb, line 58
def class_has_map(cls)
  cls[:field].each do |f|
    return true if f[:type].include? "{}"
  end

  false
end
class_line(cls) click to toggle source
# File lib/languages/js/js.rb, line 32
def class_line(cls)
  @rendered ||= Set.new
  out = []

  return nil unless cls[:implement]
  return nil if cls[:name] == rootclass
  return nil if @rendered.include? cls
  @rendered.add(cls)
  out << class_line(class_named(cls[:inherits])) unless cls[:inherits].nil?
  out << comment_block(cls)
  out << "// " + cls[:name]

  fields = {}
  unless cls[:field].nil? then
    cls[:field].each do |field|
      mapped = mapped_name(cls[:name], field[:name], :field)
      fields[mapped] = {typeStack:field[:type]}
      fields[mapped][:canonicalName] = field[:name] unless mapped == field[:name]
    end
  end

  out << "TalkObject.addClass('#{cls[:name]}', #{fields.to_json}, '#{truncated_name(superclass(cls))}');"
  out.join("\n")
end
class_named(name, classes) click to toggle source
# File lib/languages/language.rb, line 36
def class_named(name, classes)
  name_elements = name.split(".")
  classes.each do |defn|
    defn_elements = defn[:name].split(".")
    return defn if defn_elements.length >= name_elements.length && defn_elements[-name_elements.length .. -1] == name_elements
  end

  nil
end
class_package(cls) click to toggle source
# File lib/languages/cs/cs.rb, line 42
def class_package(cls)
  cls[:name].split(".")[0..-2].join(".")
end
classname_for_filename(name) click to toggle source
# File lib/languages/language.rb, line 4
def classname_for_filename(name) # /path/to/file_name.rb to FileName
  File.basename(name.to_s, ".rb").split('_').collect { |word| word.capitalize }.join("")
end
comment_block(tag, indent_level=0) click to toggle source
# File lib/languages/cs/cs.rb, line 70
def comment_block(tag, indent_level=0)
  lines = []
  indent = "\t" * indent_level
  lines.push(indent)
  lines.push(indent + "/**")
  lines.push(wrap_text_to_width(tag[:description], 80, indent + " *  ")) unless tag[:description].nil?
  lines.push(indent + " *  ")
  lines.push(indent + " *  " + definition_reference(tag))
  lines.push(indent + " */")

  lines.join("\n")
end
comment_line(tag) click to toggle source
# File lib/languages/objc/objc.rb, line 42
def comment_line(tag)
  tag[:description].nil? ? "" : "//!< #{tag[:description]}"
end
constant_definition(constant) click to toggle source
# File lib/languages/objc/objc.rb, line 38
def constant_definition(constant)
  "#{constant[:name]} = #{constant[:value].to_i}, #{comment_line(constant)}"
end
convert_field_for_map(field) click to toggle source
# File lib/languages/cs/cs.rb, line 97
def convert_field_for_map(field)
  case field
  when "byte"
    "byte"
  when "short"
    "short"
  when "int"
    "int"
  when "long"
    "long"
  when "double"
    "double"
  else
    field
  end
end
definition_reference(tag) click to toggle source
# File lib/languages/cs/cs.rb, line 30
def definition_reference(tag)
  "@talkFile #{tag[:__meta][:file]}:#{tag[:__meta][:line]}"
end
detail_comment_block(cls) click to toggle source
# File lib/languages/java/java.rb, line 91
def detail_comment_block(cls)
  lines = []
  lines.push("\t/**")
  lines.push("\t *  #{truncated_name cls[:name]}")
  cls[:field].each { |f| lines.push("\t *  @param #{field_datatype f} #{f[:name]}") }
  lines.join("\n")
end
dissect_type(type) click to toggle source
# File lib/contexts/field.rb, line 1
def dissect_type(type)
        containers = []
        while is_container?(type)
                containers.push type[-2..-1] # last two characters
                type = type[0..-3] # all but last two
        end

        containers.push type
        containers.reverse
end
dynamic_body_for_named_wrapper() click to toggle source
# File lib/languages/objc/objc.rb, line 103
def dynamic_body_for_named_wrapper
  return "@dynamic body;" if truncated_name(@current_class[:name]) == 'NamedObjectWrapper'
  ""
end
enumeration_line(enumeration) click to toggle source
# File lib/languages/js/js.rb, line 75
def enumeration_line(enumeration)
  constants = {}
  enumeration[:constant].each { |constant| constants[constant[:name]] = constant[:value] }

  out = []
  out << comment_block(enumeration)
  out << "TalkObject.addEnumeration('#{enumeration[:name]}', #{constants.to_json});"
  out.join("\n")
end
field_accessors(cls, field) click to toggle source
# File lib/languages/cs/cs.rb, line 168
def field_accessors(cls, field)
  lines = []
  lines.push "\t\t/**"
  lines.push "\t\t#{wrap_text_to_width(field[:description])}"
  lines.push "\t\t#{field[:deprecated] if field.has_key? :deprecated}"
  lines.push "\t\t#{definition_reference(field)}"
  lines.push "\t\t@param #{field[:name]} #{field[:description]}"
  lines.push "\t\t*/"
  lines.push "\t\tpublic void #{setter_name(field)}(#{field_datatype(field)} #{mapped_name(cls, field,:field)}) {"
  lines.push "\t\t\tthis.#{mapped_name(cls, field, :field)} = #{mapped_name(cls, field, :field)};"
  lines.push "\t\t}"
  lines.push "\t\tpublic #{field_datatype(field)} #{getter_name(field)}() {"
  lines.push "\t\t\treturn this.#{mapped_name(cls, field, :field)};"
  lines.push "\t\t}"
  lines.join "\n"
end
field_datatype(field) click to toggle source
# File lib/languages/cs/cs.rb, line 149
def field_datatype(field)
  field_datatype_rec(field, field[:type])
end
field_datatype_basic(field, type) click to toggle source
# File lib/languages/cs/cs.rb, line 114
def field_datatype_basic(field, type)
  if is_primitive? type then
    case type
    when "string"
      "string"
    when "real"
      "double"
    when "bool"
      "bool"
    when "object"
      "object"
    when "talkobject"
      rootclass
    when "int8"
      "sbyte"
    when "uint8"
      "byte"
    when "int16"
      "short"
    when "uint16"
      "ushort"
    when "int32"
      "int"
    when "uint32"
      "uint"
    when "int64"
      "long"
    when "uint64"
      "ulong"
    end
  else
    truncated_name type
  end
end
field_datatype_rec(field, stack) click to toggle source
# File lib/languages/cs/cs.rb, line 83
def field_datatype_rec(field, stack)
  return field_datatype_basic(field, stack.last) if stack.length == 1
  t = stack.last

  r = field_datatype_rec(field, stack[0 .. -2])
  if is_array? t then
    "#{r}[]"
  elsif is_dict? t then
    "Dictionary<string, #{convert_field_for_map(r)}>"
  else
    nil
  end
end
field_definition(cls, field) click to toggle source
# File lib/languages/objc/objc.rb, line 123
def field_definition(cls, field)
  base_type = field[:type].last
  objc_type = case
    when is_array?(base_type)
      "NSMutableArray *"
    when is_dict?(base_type)
      "NSMutableDictionary *"
    when (base_type == "talkobject" or base_type == "object")
      "TalkObject *"
    when base_type.match(/(u)?int(8|16|32|64)/)
      primitive_type(not($1.nil?), $2)
    when base_type == "real"
      "double"
    when base_type == "bool"
      "BOOL"
    when base_type == "string"
      "NSString *"
    else
      truncated_name(base_type) + " *"
  end
  "#{objc_type} #{mapped_name(cls, field, :field)}"
end
field_tags(field) click to toggle source
# File lib/languages/objc/objc.rb, line 114
def field_tags(field)
  case field[:type].last
  when "real", "bool", "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64"
    "(readwrite,assign)"
  else
    "(readwrite,nonatomic,retain)"
  end
end
field_variable(cls,field) click to toggle source
# File lib/languages/cs/cs.rb, line 153
def field_variable(cls,field)
  lines = []
  lines.push comment_block(field, 2)
  lines.push "\t\tpublic #{field_datatype(field)} #{mapped_name(cls, field, :field)} { get; set; }"
  lines.join("\n")
end
filename_for_class(cls) click to toggle source
# File lib/languages/objc/objc.rb, line 14
def filename_for_class(cls)
  class_dir = "classes"
  if meta(:namespace) == "true" then
    namespace = cls[:name][@prefix.length..-1].split(".")[0..-2]
    return File.join(class_dir, truncated_name(cls)) if namespace.empty?

    namespace = namespace[1..-1] while namespace[0].length == 0
    File.join(class_dir, namespace.join("/"), truncated_name(cls))
  else
    File.join(class_dir, truncated_name(cls))
  end
end
filename_for_entity(name) click to toggle source
# File lib/languages/cs/cs.rb, line 18
def filename_for_entity(name)
  name = name[:name] if name.is_a? Hash
  name.gsub(".", "/") + ".cs"
end
getter_name(field) click to toggle source
# File lib/languages/cs/cs.rb, line 164
def getter_name(field)
  "get#{field[:name].sub(/^(\w)/) {|s| s.capitalize}}"
end
glossary_line(glossary) click to toggle source
# File lib/languages/js/js.rb, line 65
def glossary_line(glossary)
  terms = {}
  glossary[:term].each { |term| terms[term[:name]] = term[:value] }

  out = []
  out << comment_block(glossary)
  out << "TalkObject.addGlossary('#{glossary[:name]}', #{terms.to_json});"
  out.join("\n")
end
glossary_term_name(name) click to toggle source
# File lib/languages/objc/objc.rb, line 34
def glossary_term_name(name)
  "k"+name
end
import_classes() click to toggle source
# File lib/languages/cs/cs.rb, line 66
def import_classes
  (list_references_for_class(@current).map { |name| "using #{name};"}).join("\n")
end
is_array?(type) click to toggle source
# File lib/languages/language.rb, line 28
def is_array?(type)
  type == "[]"
end
is_container?(type) click to toggle source
# File lib/contexts/field.rb, line 20
def is_container?(type)
        type.end_with? "[]" or type.end_with? "{}"
end
is_dict?(type) click to toggle source
# File lib/languages/language.rb, line 32
def is_dict?(type)
  type == "{}"
end
is_native?(type) click to toggle source
# File lib/languages/language.rb, line 24
def is_native?(type)
  type != "talkobject" and is_primitive?(type)
end
is_primitive?(type) click to toggle source
# File lib/contexts/field.rb, line 12
def is_primitive?(type)
        primitives = [
                "uint8", "uint16", "uint32", "uint64",
                "int8", "int16", "int32", "int64",
                "string", "real", "bool", "object", "talkobject" ]
        primitives.include? type
end
list_references_for_class(cls) click to toggle source
# File lib/languages/cs/cs.rb, line 46
def list_references_for_class(cls)
  references = Set.new
  cls[:field].each do |field|
    unless is_primitive?(field[:type].first) then
      references.add( class_package( class_named(field[:type][0], @base[:class]) ))
    end
  end

  references.add("System.Collections.Generic") if class_has_map(cls)
  references.to_a
end
make_source() click to toggle source
# File lib/languages/cs/cs.rb, line 3
def make_source
  types = [ :class, :enumeration, :glossary]
  types.each do |type|
    @base[type].each do |current|
      @current = current
      @current[:field] ||= [] if type == :class
      generate_template(filename_for_entity(@current), type.to_s+".cs.erb")
    end
  end
  @base[:protocol].each do |current|
    @current = current
    generate_template("com/acres4/common/protocol/#{current[:name]}.cs", "TalkProtocol.cs.erb")
  end
end
primitive_type(unsigned, size) click to toggle source
# File lib/languages/objc/objc.rb, line 108
def primitive_type(unsigned, size)
  type = "int#{size}_t"
  type = "u" + type if unsigned
  type
end
protocol_constant_name(name) click to toggle source
# File lib/languages/objc/objc.rb, line 84
def protocol_constant_name(name)
  name = (name.split("/").map { |x| x[0].upcase + x[1..-1] }).join("")
  "kProto#{name}"
end
protocol_line(proto) click to toggle source
# File lib/languages/js/js.rb, line 57
def protocol_line(proto)
  methods = proto[:method].map { |m| m[:name] }
  out = []
  out << comment_block(proto)
  out << "TalkObject.addProtocol('#{proto[:name]}', #{methods.to_json});"
  out.join("\n")
end
protocol_method_name(p_name, m_name) click to toggle source
# File lib/languages/objc/objc.rb, line 89
def protocol_method_name(p_name, m_name)
  m_name = m_name[0].upcase + m_name[1..-1]
  "#{protocol_constant_name(p_name)}Method#{m_name}"
end
rootclass() click to toggle source
# File lib/languages/cs/cs.rb, line 34
def rootclass
  @target[:rootclass] || "io.usetalk.TalkObject"
end
safe_escape(str) click to toggle source
# File lib/languages/objc/objc.rb, line 146
def safe_escape(str)
  str.gsub("\\", "\\\\\\").gsub("\"", "\\\"")
end
setter_name(field) click to toggle source
# File lib/languages/cs/cs.rb, line 160
def setter_name(field)
  "set#{field[:name].sub(/^(\w)/) {|s| s.capitalize}}"
end
superclass(cls) click to toggle source
# File lib/languages/cs/cs.rb, line 38
def superclass(cls)
  cls[:inherits] || rootclass
end
talk_definition() click to toggle source
# File lib/languages/objc/objc.rb, line 150
def talk_definition
  @base
end
to_val() click to toggle source
# File lib/contexts/boolean.rb, line 3
def to_val
        self[:value] == "0" ? false : true
end
trimmed_fields(cls) click to toggle source
# File lib/languages/objc/objc.rb, line 98
def trimmed_fields(cls)
  return cls[:field] unless truncated_name(cls[:name]) == 'NamedObjectWrapper'
  cls[:field].reject { |f| f[:name] == "body" }
end