def add_relationships(model, objects): for object_name, data_object in objects.iteritems(): for relationship in data_object.relationships: first_entity = second_entity = None for entity in model.iter("entity"): if entity.get('name') == get_proper_object_name(object_name): first_entity = entity elif entity.get('name') == get_proper_object_name(relationship.related_object.name): second_entity = entity first_entity_name = first_entity.get('name').lower().replace("response", "") if relationship.relationship_type == Relationship.ONE_TO_MANY: add_O2M_relationships(first_entity, second_entity, first_entity_name, relationship.name) elif relationship.relationship_type == Relationship.MANY_TO_MANY: add_M2M_relationships(first_entity, second_entity, relationship.name, get_word_plural(first_entity_name)) elif relationship.relationship_type == Relationship.ONE_TO_ONE: add_O2O_relationships(first_entity, second_entity, get_proper_name(relationship.name), first_entity_name) elif relationship.relationship_type == Relationship.MANY_TO_ONE: """ If we have a entity which maps to another entity more than once, we can't use that entity's name instead, let's use the key of the field. For example, if we had a user who has followers and is also following other users, our user entity would have two fields called "follow". This instead calls them followers and following. """ conflicting = conflicting_entity_name(data_object.relationships, relationship) entity_label = relationship.name if conflicting else first_entity_name add_M2O_relationship(first_entity, second_entity, get_word_plural(entity_label), get_proper_name(relationship.name))
def attribute_mappings(fields): attribute_mapping_string = "" for index, field in enumerate(fields): leading_comma = '' if index == 0 else ', ' swift_variable_name = get_proper_name(field.name) attribute_mapping_string += '{}"{}": "{}"'.format(leading_comma, field.name, swift_variable_name) return attribute_mapping_string
def attribute_mappings(fields): attribute_mapping_string = "" for index, field in enumerate(fields): leading_comma = '' if index == 0 else ', ' swift_variable_name = get_proper_name(field.name) attribute_mapping_string += '{}"{}": "{}"'.format( leading_comma, field.name, swift_variable_name) return attribute_mapping_string
def create_parameter_signature(parameters): method_parts = [] for index, method_field in enumerate(parameters): swift_variable_name = get_proper_name(method_field.name) parameter_signature = "{}: {}".format(swift_variable_name, method_field.swift_type) method_parts.append(parameter_signature) return ", ".join(method_parts)
def create_parameter_signature(parameters): method_parts = [] for index, method_field in enumerate(parameters): objc_variable_name = get_proper_name(method_field.name) parameter_signature = "({}){}".format(method_field.objc_type, objc_variable_name) # If this isn't the first parameter, also include the variable name before the type if index > 0: parameter_signature = "{}:{}".format(objc_variable_name, parameter_signature) method_parts.append(parameter_signature) return " ".join(method_parts)
def add_relationships(model, objects): for object_name, data_object in objects.iteritems(): for relationship in data_object.relationships: first_entity = second_entity = None for entity in model.iter("entity"): if entity.get('name') == get_proper_object_name(object_name): first_entity = entity elif entity.get('name') == get_proper_object_name( relationship.related_object.name): second_entity = entity first_entity_name = first_entity.get('name').lower().replace( "response", "") if relationship.relationship_type == Relationship.ONE_TO_MANY: add_O2M_relationships(first_entity, second_entity, first_entity_name, relationship.name) elif relationship.relationship_type == Relationship.MANY_TO_MANY: add_M2M_relationships(first_entity, second_entity, relationship.name, get_word_plural(first_entity_name)) elif relationship.relationship_type == Relationship.ONE_TO_ONE: add_O2O_relationships(first_entity, second_entity, get_proper_name(relationship.name), first_entity_name) elif relationship.relationship_type == Relationship.MANY_TO_ONE: """ If we have a entity which maps to another entity more than once, we can't use that entity's name instead, let's use the key of the field. For example, if we had a user who has followers and is also following other users, our user entity would have two fields called "follow". This instead calls them followers and following. """ conflicting = conflicting_entity_name( data_object.relationships, relationship) entity_label = relationship.name if conflicting else first_entity_name add_M2O_relationship(first_entity, second_entity, get_word_plural(entity_label), get_proper_name(relationship.name))
def method_name(api): # First create camel cased name from snake case method_name_string = "" for part in re.split(r'[/_]+', api.url_path): if part in [":id", "theID"]: continue else: method_name_string += part.capitalize() first_parameter_name = "Success" request_object = iOSTemplateMethods.get_api_request_object(api) if request_object and len(request_object.properties()) > 0: first_field = request_object.properties()[0] first_parameter_name = get_proper_name(first_field.name, capitalize_first=True) elif ObjCParameter.create_id_parameter(api.url_path, request_object) is not None: first_parameter_name = "TheID" elif SwiftParameter.create_id_parameter(api.url_path, request_object) is not None: first_parameter_name = "TheID" return "{}With{}".format(method_name_string, first_parameter_name)
def media_field_check(fields): statements = ["{} != nil".format(get_proper_name(field.name)) for field in fields] return " || ".join(statements)
def test_get_proper_name(self): self.assertEqual(get_proper_name("unread_count"), "unreadCount") self.assertEqual( get_proper_name("unread_count", capitalize_first=True), "UnreadCount") self.assertEqual(get_proper_name("id"), "theID")
def media_field_check(fields): statements = [ "{} != nil".format(get_proper_name(field.name)) for field in fields ] return " || ".join(statements)
def test_get_proper_name(self): self.assertEqual(get_proper_name("unread_count"), "unreadCount") self.assertEqual(get_proper_name("unread_count", capitalize_first=True), "UnreadCount") self.assertEqual(get_proper_name("id"), "theID")