// // <%= @class_name %>.swift // Generated by RailsToSwift // github.com/JoshJuncker/RailsToSwift //

import Foundation

final class <%= @class_name %>: ResponseObjectSerializable, ResponseCollectionSerializable {

<% @columns.each do |col| %> <% if col.not_null %>

<%= "var #{col.formatted_name}:#{col.formatted_type}?" %>

<% else %>

<%= "var #{col.formatted_name}:#{col.formatted_type}?" %>

<% end %>

<% end %>

init?(response: NSHTTPURLResponse, representation: AnyObject) {

<% @columns.each do |col| %> <% if col.not_null %>

<%= "assert(representation.valueForKey(\"#{col.name}\") != nil, \"Expected #{col.name} to not be nil.\")" %>

<% end %> <% end %>

<% @columns.each do |col| %> <% if col.type == “string” %>

<%= "#{col.formatted_name} = (representation.valueForKey(\"#{col.name}\") as? String) ?? \"\"" %>

<% elsif col.type == “integer” %>

<%= "#{col.formatted_name} = (representation.valueForKey(\"#{col.name}\") as? Int) ?? -1" %>

<% elsif col.type == “float” %>

<%= "#{col.formatted_name} = (representation.valueForKey(\"#{col.name}\") as? Float) ?? -1.0" %>

<% elsif col.type == “datetime” %>

<%= "#{col.formatted_name} = NSDate.fromRails(representation.valueForKey(\"#{col.name}\") as? String ?? \"\")" %>

<% elsif col.type == “text” %>

<%= "#{col.formatted_name} = (representation.valueForKey(\"#{col.name}\") as? String) ?? \"\"" %>

<% end %>

<% if col.not_null %>

<%= "assert(representation.valueForKey(\"#{col.name}\") != nil, \"Expected #{col.name} to not be nil.\")" %>

<% end %> <% end %>

}

func toDict() -> [String: AnyObject] {
        let dict:[String:AnyObject?] = [

<% @columns.each do |col| %> <% line = “” %> <% if col.type == “datetime” %> <% line = “"#{col.name}":#{col.formatted_name}.railsFriendly()” %> <% else %> <% line = “"#{col.name}":#{col.formatted_name}” %> <% end %> <% line += “,” if col != @columns.last %>

<%= line %>

<% end %>

        ]
        return removeNilValuedKeys(dict)
}

}