ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
ARResource.h
Go to the documentation of this file.
1 // ActiveResourceKit ARResource.h
2 //
3 // Copyright © 2011, 2012, Roy Ratcliffe, Pioneering Software, United Kingdom
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the “Software”), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER
16 // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
18 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22 //
23 //------------------------------------------------------------------------------
24 
25 #import <Foundation/Foundation.h>
26 #import <ActiveModelKit/ActiveModelKit.h>
27 
28 @class ARService;
29 @class ARHTTPResponse;
30 
31 /*!
32  * @brief Provides the core class mirroring Rails' @c ActiveResource::Base class.
33  * @details An active resource mimics an active record. Resources behave as
34  * records. Only their connection fundamentally differs. Active Records connect
35  * to a database whereas Active Resources connect to a RESTful API accessed via
36  * HTTP transport.
37  *
38  * @image html Class_Diagram__Service_and_Resource.png
39  */
40 @interface ARResource : NSObject<AMAttributeMethods, NSCopying>
41 
42 /*!
43  * @brief Constructs an active resource service using class methods to establish
44  * the site and element name.
45  * @details If the ARResource sub-class has a class method called +site, use its
46  * answer to set up the Active Resource site. This assumes that +site answers an
47  * NSURL object. Similarly, sets up the service element name by sending
48  * +elementName to the sub-class, answering a string. However, if the sub-class
49  * does not implement the +elementName class method, the element name derives
50  * from the ARResource sub-class name.
51  */
52 + (ARService *)service;
53 
54 - (id)initWithService:(ARService *)service;
55 
56 - (id)initWithService:(ARService *)service attributes:(NSDictionary *)attributes;
57 
58 - (id)initWithService:(ARService *)service attributes:(NSDictionary *)attributes persisted:(BOOL)persisted;
59 
60 /*!
61  * @brief Answers options for applying this resource's identity to a nested
62  * service.
63  * @details Use it to access this resource's subelements. First derive a nested
64  * service for the subelement. Doing so parameterises the resource
65  * identity. Then apply these options to select this resource's subelement or
66  * subelements.
67  */
68 - (NSDictionary *)optionsForSubelement;
69 
70 //------------------------------------------------------------------------- Base
71 
72 /*!
73  * Retains the active-resource service. Does not copy the service. This has important
74  * implications. If you alter service properties, the changes affect all the
75  * resources which depend upon it.
76  */
77 @property(strong, NS_NONATOMIC_IOSONLY) ARService *service;
78 
79 /*!
80  * @brief Asks for the resource service, lazily constructing a service instance if the
81  * resource does not currently retain a service.
82  * @details Asks the class for a site URL and an element name. Optionally
83  * implement @c +site and @c +elementName to supply the URL and element name. If
84  * your class does not supply an implementation for @c +elementName, the element
85  * name derives from the sub-class name. This assumes that you do not directly
86  * instantiate the ARResource class. If you do, the element name remains @c nil.
87  */
89 
90 //------------------------------------------------------------------- Attributes
91 
92 @property(copy, NS_NONATOMIC_IOSONLY) NSDictionary *attributes;
93 
94 /*!
95  * @brief Merges the contents of a dictionary into the receiver's attributes.
96  * @param attributes The dictionary (or hash) of attribute key-value pairs used
97  * for merging with the receiver's attributes.
98  * @details If keys within the given dictionary of @a attributes match keys in
99  * the receiver's current attributes, merging replaces the objects in the
100  * receiver by those in the given dictionary.
101  */
102 - (void)mergeAttributes:(NSDictionary *)attributes;
103 
104 /*!
105  * @brief Loads resource attributes from a dictionary of key-value pairs.
106  * @details Argument @a removeRoot becomes a do-not-care if @a attributes
107  * contains just a single key-object pair. In such a case, removing the root
108  * depends on whether or not the single key matches the service element name.
109  *
110  * Attribute keys use Rails conventions: underscored and lower case. You can
111  * also access the attribute values using Key-Value Coding where the keys follow
112  * Cocoa conventions: camel case with leading lower case letter. The resource
113  * translates the KVC-style keys to Rails conventions when looking up the
114  * corresponding value, @c aKey becomes @c a_key.
115  */
116 - (void)loadAttributes:(NSDictionary *)attributes removeRoot:(BOOL)removeRoot;
117 
118 //--------------------------------------------------------------- Prefix Options
119 
120 @property(copy, NS_NONATOMIC_IOSONLY) NSDictionary *prefixOptions;
121 
122 //-------------------------------------------------- Schema and Known Attributes
123 
124 - (NSDictionary *)schema;
125 
126 /*!
127  * @brief Answers all the known attributes belonging to this active resource, a
128  * unique array of attribute key strings.
129  * @details The resulting array includes all the service's known attributes plus
130  * this resource instance's known attributes. Duplicates if any do @e not
131  * appear. This deviates from Rails at version 3.1.0 where duplicates @e do
132  * appear.
133  */
134 - (NSArray *)knownAttributes;
135 
136 //-------------------------------------------------------------------- Persisted
137 
138 @property(assign, NS_NONATOMIC_IOSONLY) BOOL persisted;
139 
140 - (BOOL)isNew;
141 - (BOOL)isNewRecord;
142 
143 //------------------------------------------------------------------ Primary Key
144 
145 /*!
146  * @brief Answers the primary key.
147  * @details By convention, Rails' primary keys appear in the column named @c
148  * id. Column type is integer. Hence the ActiveResourceKit implementation
149  * answers the primary key only if the type is a number. (This includes other
150  * types of number.) Otherwise it answers @c nil.
151  * @par Integer Width
152  * The exact integer type depends on the database implementation and
153  * configuration at the server side. Databases typically store integers as
154  * signed 32-bit integers. Send -[NSNumber intValue] to the resulting number to
155  * retrieve the signed 32-bit identifier in such cases.
156  */
157 - (NSNumber *)ID;
158 
159 - (void)setID:(NSNumber *)ID;
160 
161 //------------------------------------------------------------- RESTful Services
162 
163 - (void)saveWithCompletionHandler:(void (^)(ARHTTPResponse *response, NSError *error))completionHandler;
164 
165 - (void)destroyWithCompletionHandler:(void (^)(ARHTTPResponse *response, NSError *error))completionHandler;
166 
167 - (void)existsWithCompletionHandler:(void (^)(ARHTTPResponse *response, BOOL exists, NSError *error))completionHandler;
168 
169 /*!
170  * @brief Answers a serialised data representation of the resource according to
171  * the configured serialisation format.
172  */
173 - (NSData *)encode;
174 
175 @end