<commit_before>//
//  Advertisement.swift
//  PartyUP
//
//  Created by <NAME> on 2016-01-12.
//  Copyright © 2016 Sandcastle Application Development. All rights reserved.
//

import AWSDynamoDB

final class Advertisement: CustomDebugStringConvertible
{
    static var ads = [Advertisement]()
    
	enum Style: Int {
		case Page, Overlay
	}
    
    enum FeedCategory: String {
        case All = "a", Pregame = "p", Venue = "v"
    }
    
    typealias FeedMask = [FeedCategory:String]

	let administration: String
    let feeds: FeedMask
	let pages: [Int]
	let style: Style
	let media: String
    
    init(administration: String, media: String, feeds: FeedMask, pages: [Int], style: Style = .Page) {
        self.administration = administration
		self.feeds = feeds
		self.pages = pages
		self.style = style
		self.media = media
    }
    
    var debugDescription: String {
        get { return "Administration = \(administration)\nFeeds = \(feeds)\nPages = \(pages)\nStyle = \(style)\nMedia = \(media)\n" }
    }
    
    //MARK - Internal Dynamo Representation
    
	internal convenience init(data: AdvertisementDB) {
        var feeder = FeedMask(minimumCapacity: 3)
        if let feeds = data.feeds as? [String] {
            for filter in feeds {
                if let category = FeedCategory(rawValue: filter[filter.startIndex..<filter.startIndex.advancedBy(1)]) {
                    feeder[category] = filter[filter.startIndex.advancedBy(2)..<filter.endIndex]
                }
            }
        }
        
        self.init(
            administration: data.administration! as String,
			media: data.media as! String,
            feeds: feeder,
			pages: data.pages.flatMap { $0.map { $0.integerValue } } ?? [],
			style: data.style.flatMap { Style(rawValue: $0.integerValue) } ?? .Page
        )
    }

    internal var dynamo: AdvertisementDB {
        get {
            let db = AdvertisementDB()
            db.administration = administration
            db.feeds = feeds.map { $0.0.rawValue + ":" + $0.1 }
            db.pages = pages.map { NSNumber(integer: $0) }
            db.style = NSNumber(integer: style.rawValue)
            db.media = media
            
            return db
        }
    }
    
    internal class AdvertisementDB: AWSDynamoDBObjectModel, AWSDynamoDBModeling
    {
        var administration: NSString?
        var identifier: NSNumber?
        var feeds: [NSString]?
		var pages: [NSNumber]?
		var style: NSNumber?
		var media: NSString?
        
        @objc static func dynamoDBTableName() -> String {
            return "Advertisements"
        }
        
        @objc static func hashKeyAttribute() -> String! {
            return "administration"
        }

		@objc static func rangeKeyAttribute() -> String! {
			return "media"
		}
    }
}

<commit_msg>Add apropos checking both for each ad and returning all appropriate ads.<commit_after>//
//  Advertisement.swift
//  PartyUP
//
//  Created by <NAME> on 2016-01-12.
//  Copyright © 2016 Sandcastle Application Development. All rights reserved.
//

import Foundation
import AWSDynamoDB

final class Advertisement: CustomDebugStringConvertible
{
	static var ads = [String:[Advertisement]]()

	static func apropos(identifier: String, ofFeed feed: FeedCategory, inAdministration administration: String) -> [Advertisement]? {
		return ads[administration]?.filter { $0.apropos(identifier, ofFeed: feed) }
	}
    
	enum Style: Int {
		case Page, Overlay
	}
    
    enum FeedCategory: String {
        case All = "a", Pregame = "p", Venue = "v"
    }
    
    typealias FeedMask = [FeedCategory:NSRegularExpression]

	let administration: String
    let feeds: FeedMask
	let pages: [Int]
	let style: Style
	let media: String
    
    init(administration: String, media: String, feeds: FeedMask, pages: [Int], style: Style = .Page) {
        self.administration = administration
		self.feeds = feeds
		self.pages = pages
		self.style = style
		self.media = media
    }
    
    var debugDescription: String {
        get { return "Administration = \(administration)\nFeeds = \(feeds)\nPages = \(pages)\nStyle = \(style)\nMedia = \(media)\n" }
    }

	func apropos(identifier: String, ofFeed feed: FeedCategory) -> Bool {
		return feeds[feed]?.firstMatchInString(identifier, options: [.Anchored], range: NSRange(location: 0, length: identifier.utf16.count)) != nil
	}
    
    //MARK - Internal Dynamo Representation
    
	internal convenience init(data: AdvertisementDB) {
        var feeder = FeedMask(minimumCapacity: 3)
        if let feeds = data.feeds as? [String] {
            for filter in feeds {
                if let category = FeedCategory(rawValue: filter[filter.startIndex..<filter.startIndex.advancedBy(1)]),
					regex = try? NSRegularExpression(pattern: filter[filter.startIndex.advancedBy(2)..<filter.endIndex], options: []) {
						feeder[category] = regex
                }
            }
        }
        
        self.init(
            administration: data.administration! as String,
			media: data.media as! String,
            feeds: feeder,
			pages: data.pages.flatMap { $0.map { $0.integerValue } } ?? [],
			style: data.style.flatMap { Style(rawValue: $0.integerValue) } ?? .Page
        )
    }

    internal var dynamo: AdvertisementDB {
        get {
            let db = AdvertisementDB()
            db.administration = administration
            db.feeds = feeds.map { $0.0.rawValue + ":" + $0.1.pattern }
            db.pages = pages.map { NSNumber(integer: $0) }
            db.style = NSNumber(integer: style.rawValue)
            db.media = media
            
            return db
        }
    }
    
    internal class AdvertisementDB: AWSDynamoDBObjectModel, AWSDynamoDBModeling
    {
        var administration: NSString?
        var identifier: NSNumber?
        var feeds: [NSString]?
		var pages: [NSNumber]?
		var style: NSNumber?
		var media: NSString?
        
        @objc static func dynamoDBTableName() -> String {
            return "Advertisements"
        }
        
        @objc static func hashKeyAttribute() -> String! {
            return "administration"
        }

		@objc static func rangeKeyAttribute() -> String! {
			return "media"
		}
    }
}


{'fraction_non_alphanumeric': 0.10969921183850732, 'fraction_numerical': 0.00804246421103426, 'mean_word_length': 2.295177530471648, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2198772', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1785, 'n_words': 473}