Skip to content

Commit

Permalink
Document the code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkowz committed Aug 1, 2015
1 parent 6c3b81b commit e84a649
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 72 deletions.
66 changes: 42 additions & 24 deletions Swifternalization/Expression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,62 @@

import Foundation

/// String that contains expression pattern, e.g. `ie:x<5`, `exp:^1$`.
/**
String that contains expression pattern, e.g. `ie:x<5`, `exp:^1$`.
*/
typealias ExpressionPattern = String

/**
Represents simple epxressions.
This class contains pattern of expression and localized value as well as
length variations if any are associated. During instance initialization pattern
is analyzed and correct expression matcher is created. If no matcher matches
the expression pattern then when validating there is only check if passed value
is the same like pattern (equality). If there is matcher then its internal logic
validates passed value.
*/
struct Expression {
/// Pattern of expression.
let pattern: String
/**
Pattern of an expression.
*/
let pattern: ExpressionPattern

/// A localized value. If `lengthVariations` array is empty or you want to
/// get full localized value use this property.
let localizedValue: String
/**
A localized value. If length vartiations array is empty or you want to
get full localized value use this property.
*/
let value: String

/// Array of length variations
/**
Array of length variations.
*/
let lengthVariations: [LengthVariation]

/// Expression matcher that is used in validation
/**
Expression matcher that is used in validation.
*/
private var expressionMatcher: ExpressionMatcher? = nil

// Returns expression object
init(pattern: String, localizedValue: String, lengthVariations: [LengthVariation] = [LengthVariation]()) {
/**
Returns expression object.
*/
init(pattern: String, value: String, lengthVariations: [LengthVariation] = [LengthVariation]()) {
self.pattern = pattern
self.localizedValue = localizedValue
self.value = value
self.lengthVariations = lengthVariations

// Create expression matcher
/*
Create expression matcher if pattern matches some expression type.
If not matching any expression type then the pattern equality test
will be perfomed when during validation.
*/
if let type = getExpressionType(pattern) {
switch (type as ExpressionPatternType) {
case .Inequality:
expressionMatcher = InequalityExpressionParser(pattern).parse()

case .InequalityExtended:
expressionMatcher = InequalityExtendedExpressionParser(pattern).parse()

case .Regex:
expressionMatcher = RegexExpressionParser(pattern).parse()
}
expressionMatcher = {
switch (type as ExpressionPatternType) {
case .Inequality: return InequalityExpressionParser(pattern).parse()
case .InequalityExtended: return InequalityExtendedExpressionParser(pattern).parse()
case .Regex: return RegexExpressionParser(pattern).parse()
}
}()
}
}

Expand All @@ -64,7 +82,7 @@ struct Expression {
}

/**
Method used to get `ExpressionPatternType` of passed `ExpressionPattern`.
Method used to get `ExpressionPatternType` of passed expression pattern.

:param: pattern expression pattern that will be checked.
:returns: `ExpressionPatternType` if pattern is supported, otherwise nil.
Expand Down
4 changes: 2 additions & 2 deletions Swifternalization/ExpressionMatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

/**
Protocol that is the base protocol to conform for expression matchers like
`InequalityExpressionMatcher` or `RegexExpressionMatcher`.
Protocol that is the base protocol to conform for expression matchers like
`InequalityExpressionMatcher` or `RegexExpressionMatcher`.
*/
protocol ExpressionMatcher {
/**
Expand Down
11 changes: 8 additions & 3 deletions Swifternalization/LengthVariation.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import Foundation

/**
Length variation representation.
Length variation representation. It contains a width property which specifies
up to which width of a screen the text in `value` property should be presented.
*/
struct LengthVariation {
/// width of a screen.
/**
Max width of a screen on which the `value` should be presented.
*/
let width: Int

/// localized string.
/**
String with localized content in some language.
*/
let value: String
}
10 changes: 5 additions & 5 deletions Swifternalization/LoadedTranslationsProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LoadedTranslationsProcessor {
case .Simple:
// Simple translation with key and value.
let value = $0.content[$0.key] as! String
return Translation(key: $0.key, expressions: [Expression(pattern: $0.key, localizedValue: value)])
return Translation(key: $0.key, expressions: [Expression(pattern: $0.key, value: value)])

case .WithExpressions:
// Translation that contains expression.
Expand All @@ -60,7 +60,7 @@ class LoadedTranslationsProcessor {
var expressions = [Expression]()
for (key, value) in $0.content as! Dictionary<String, String> {
let pattern = sharedExpressions.filter({$0.identifier == key}).first?.pattern ?? key
expressions.append(Expression(pattern: pattern, localizedValue: value))
expressions.append(Expression(pattern: pattern, value: value))
}
return Translation(key: $0.key, expressions: expressions)

Expand All @@ -70,7 +70,7 @@ class LoadedTranslationsProcessor {
for (key, value) in $0.content as! Dictionary<String, String> {
lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(key), value: value))
}
return Translation(key: $0.key, expressions: [Expression(pattern: $0.key, localizedValue: lengthVariations.last!.value, lengthVariations: lengthVariations)])
return Translation(key: $0.key, expressions: [Expression(pattern: $0.key, value: lengthVariations.last!.value, lengthVariations: lengthVariations)])

case .WithExpressionsAndLengthVariations:
// The most advanced translation type. It contains expressions
Expand All @@ -87,9 +87,9 @@ class LoadedTranslationsProcessor {
for (lvKey, lvValue) in value as! Dictionary<String, String> {
lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(lvKey), value: lvValue))
}
expressions.append(Expression(pattern: pattern, localizedValue: lengthVariations.last!.value, lengthVariations: lengthVariations))
expressions.append(Expression(pattern: pattern, value: lengthVariations.last!.value, lengthVariations: lengthVariations))
} else if value is String {
expressions.append(Expression(pattern:pattern, localizedValue: value as! String))
expressions.append(Expression(pattern:pattern, value: value as! String))
}
}
return Translation(key: $0.key, expressions: expressions)
Expand Down
5 changes: 0 additions & 5 deletions Swifternalization/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Regex {

:param: str A string that will be matched.
:param: pattern A regex pattern.

:returns: `String` that matches pattern or nil.
*/
class func matchInString(str: String, pattern: String, capturingGroupIdx: Int?) -> String? {
Expand All @@ -44,7 +43,6 @@ class Regex {

:param: str A string that will be matched.
:param: pattern A regexp pattern.

:returns: `String` that matches pattern or nil.
*/
class func firstMatchInString(str: String, pattern: String) -> String? {
Expand All @@ -59,7 +57,6 @@ class Regex {

:param: str A string that will be matched.
:param: pattern A regexp pattern.

:returns: Array of `Strings`s. If nothing found empty array is returned.
*/
class func matchesInString(str: String, pattern: String) -> [String] {
Expand All @@ -77,7 +74,6 @@ class Regex {
Returns new `NSRegularExpression` object.

:param: pattern A regexp pattern.

:returns: `NSRegularExpression` object or nil if it cannot be created.
*/
private class func regexp(pattern: String) -> NSRegularExpression? {
Expand All @@ -94,7 +90,6 @@ class Regex {

:param: str A string that is source of substraction.
:param: range A range that tells which part of `str` will be substracted.

:returns: A string contained in `range`.
*/
private class func substring(str: String, range: NSRange) -> String {
Expand Down
Loading

0 comments on commit e84a649

Please sign in to comment.