Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactor to enhance readability, foldability, verbosity, etc. #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 82 additions & 94 deletions BaseModel/BaseModel.h
Original file line number Diff line number Diff line change
@@ -1,111 +1,99 @@
//
// BaseModel.h
//
// Version 2.4.3
//
// Created by Nick Lockwood on 25/06/2011.
// Copyright 2011 Charcoal Design
//
// Distributed under the permissive zlib license
// Get the latest version from here:
//
// https://github.com/nicklockwood/BaseModel
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
/***
BaseModel.h

Version 2.4.4

#import <Foundation/Foundation.h>
Created by Nick Lockwood on 25/06/2011.
Copyright 2011 Charcoal Design

Distributed under the permissive zlib license
Get the latest version from here:

extern NSString *const BaseModelSharedInstanceUpdatedNotification;
https://github.com/nicklockwood/BaseModel

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

//the BaseModel protocol defines optional methods that
//you can define on your BaseModel subclasses to extend their functionality
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

@protocol BaseModel <NSObject>
@optional
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.

//loading sequence:
//setUp called first
//then setWithDictionary/Array/String/etc if resource file exists
//then setWithCoder if save file exists
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.

- (void)setUp;
- (void)setWithDictionary:(NSDictionary *)dict;
- (void)setWithArray:(NSArray *)array;
- (void)setWithString:(NSString *)string;
- (void)setWithCoder:(NSCoder *)coder;
3. This notice may not be removed or altered from any source distribution. */

//coding
#import <Foundation/Foundation.h>

- (void)encodeWithCoder:(NSCoder *)coder;
extern NSString *const BaseModelSharedInstanceUpdatedNotification;

@end
// The BaseModel protocol defines optional methods
// that you can define on your BaseModel subclasses to extend their functionality

@protocol BaseModel <NSObject> @optional

//use the BaseModel class as the base class for any of your
//model objects. BaseModels can be standalone objects, or
//act as sub-properties of a larger object
/* loading sequence: 1. setUp
2. setWithDictionary/Array/String/etc if resource file exists
3. setWithCoder if save file exists */
- (void) setUp;
- (void) setWithDictionary:(NSDictionary*)dict;
- (void) setWithArray: (NSArray*)array;
- (void) setWithString: (NSString*)string;
- (void) setWithCoder: (NSCoder*)coder;

- (void)encodeWithCoder:(NSCoder *)coder; //coding

@end

// Use the BaseModel class as the base class for any of your model objects.
// BaseModels can be standalone objects, or act as sub-properties of a larger object

@interface BaseModel : NSObject <BaseModel>

//new autoreleased instance
+ (instancetype)instance;

//shared (singelton) instance
+ (instancetype)sharedInstance;
+ (BOOL)hasSharedInstance;
+ (void)setSharedInstance:(BaseModel *)instance;
+ (void)reloadSharedInstance;

//creating instances from collection or string
+ (instancetype)instanceWithObject:(id)object;
- (instancetype)initWithObject:(id)object;
+ (NSArray *)instancesWithArray:(NSArray *)array;

//creating an instance using NSCoding
+ (instancetype)instanceWithCoder:(NSCoder *)decoder;
- (instancetype)initWithCoder:(NSCoder *)decoder;

//loading and saving the model from a plist file
+ (instancetype)instanceWithContentsOfFile:(NSString *)path;
- (instancetype)initWithContentsOfFile:(NSString *)path;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)atomically;
- (BOOL)useHRCoderIfAvailable;

//resourceFile is a file, typically within the resource bundle that
//is used to initialise any BaseModel instance
//saveFile is a path, typically within application support that
//is used to save the shared instance of the model
//saveFileForID is a path, typically within application support that
//is used to save any instance of the model
+ (NSString *)resourceFile;
+ (NSString *)saveFile;

//save the model
- (void)save;

//generate unique identifier
//useful for creating universally unique
//identifiers and filenames for model objects
+ (NSString *)newUniqueIdentifier;

@end
+ (instancetype) instance; //new autoreleased instance

// shared (singelton) instance
+ (instancetype) sharedInstance;
+ (BOOL) hasSharedInstance;
+ (void) setSharedInstance:(BaseModel*)instance;
+ (void) reloadSharedInstance;

// creating instances from collection or string
+ (instancetype) instanceWithObject:(id)object;
- (instancetype) initWithObject: (id)object;
+ (NSArray*) instancesWithArray:(NSArray*)array;

// creating an instance using NSCoding
+ (instancetype) instanceWithCoder:(NSCoder*)decoder;
- (instancetype) initWithCoder: (NSCoder*)decoder;

// loading and saving the model from a plist file
+ (instancetype) instanceWithContentsOfFile:(NSString*)path;
- (instancetype) initWithContentsOfFile: (NSString*)path;
- (BOOL) writeToFile: (NSString*)path atomically:(BOOL)atomically;
- (BOOL) useHRCoderIfAvailable;

/*
+ resourceFile is a file, typically within the resource bundle,
that is used to initialise any BaseModel instance
+ saveFile is a path, typically within application support,
that is used to save the shared instance of the model
+ saveFileForID is a path, typically within application support,
that is used to save any instance of the model. */

+ (NSString*) resourceFile;
+ (NSString*) saveFile;

- (void) save; //save the model

// Generates a UUID - useful for creating universally unique id's / filenames for model objects

+ (NSString*)newUniqueIdentifier;

@end
Loading