@blazetrails/activemodel 0.0.0-stage → 0.1.0-alpha0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (228) hide show
  1. package/LICENSE +21 -0
  2. package/dist/access.d.ts +6 -0
  3. package/dist/access.js +12 -0
  4. package/dist/access.js.map +1 -0
  5. package/dist/api.d.ts +55 -0
  6. package/dist/api.js +28 -0
  7. package/dist/api.js.map +1 -0
  8. package/dist/attribute/user-provided-default.d.ts +13 -0
  9. package/dist/attribute/user-provided-default.js +44 -0
  10. package/dist/attribute/user-provided-default.js.map +1 -0
  11. package/dist/attribute-assignment.d.ts +52 -0
  12. package/dist/attribute-assignment.js +190 -0
  13. package/dist/attribute-assignment.js.map +1 -0
  14. package/dist/attribute-methods.d.ts +232 -0
  15. package/dist/attribute-methods.js +429 -0
  16. package/dist/attribute-methods.js.map +1 -0
  17. package/dist/attribute-mutation-tracker.d.ts +64 -0
  18. package/dist/attribute-mutation-tracker.js +170 -0
  19. package/dist/attribute-mutation-tracker.js.map +1 -0
  20. package/dist/attribute-registration.d.ts +121 -0
  21. package/dist/attribute-registration.js +160 -0
  22. package/dist/attribute-registration.js.map +1 -0
  23. package/dist/attribute-set/builder.d.ts +70 -0
  24. package/dist/attribute-set/builder.js +216 -0
  25. package/dist/attribute-set/builder.js.map +1 -0
  26. package/dist/attribute-set/yaml-encoder.d.ts +12 -0
  27. package/dist/attribute-set/yaml-encoder.js +35 -0
  28. package/dist/attribute-set/yaml-encoder.js.map +1 -0
  29. package/dist/attribute-set.d.ts +40 -0
  30. package/dist/attribute-set.js +110 -0
  31. package/dist/attribute-set.js.map +1 -0
  32. package/dist/attribute.d.ts +93 -0
  33. package/dist/attribute.js +265 -0
  34. package/dist/attribute.js.map +1 -0
  35. package/dist/attributes.d.ts +77 -0
  36. package/dist/attributes.js +80 -0
  37. package/dist/attributes.js.map +1 -0
  38. package/dist/callbacks.d.ts +48 -0
  39. package/dist/callbacks.js +73 -0
  40. package/dist/callbacks.js.map +1 -0
  41. package/dist/constants.generated.d.ts +2 -0
  42. package/dist/constants.generated.js +197 -0
  43. package/dist/constants.generated.js.map +1 -0
  44. package/dist/conversion.d.ts +41 -0
  45. package/dist/conversion.js +40 -0
  46. package/dist/conversion.js.map +1 -0
  47. package/dist/deprecator.d.ts +7 -0
  48. package/dist/deprecator.js +7 -0
  49. package/dist/deprecator.js.map +1 -0
  50. package/dist/dirty.d.ts +79 -0
  51. package/dist/dirty.js +139 -0
  52. package/dist/dirty.js.map +1 -0
  53. package/dist/error.d.ts +27 -0
  54. package/dist/error.js +227 -0
  55. package/dist/error.js.map +1 -0
  56. package/dist/errors.d.ts +81 -0
  57. package/dist/errors.js +220 -0
  58. package/dist/errors.js.map +1 -0
  59. package/dist/forbidden-attributes-protection.d.ts +16 -0
  60. package/dist/forbidden-attributes-protection.js +22 -0
  61. package/dist/forbidden-attributes-protection.js.map +1 -0
  62. package/dist/gem-version.d.ts +10 -0
  63. package/dist/gem-version.js +16 -0
  64. package/dist/gem-version.js.map +1 -0
  65. package/dist/i18n.d.ts +3 -0
  66. package/dist/i18n.js +8 -0
  67. package/dist/i18n.js.map +1 -0
  68. package/dist/index.d.ts +72 -0
  69. package/dist/index.js +59 -0
  70. package/dist/index.js.map +1 -0
  71. package/dist/lint.d.ts +60 -0
  72. package/dist/lint.js +58 -0
  73. package/dist/lint.js.map +1 -0
  74. package/dist/locale/en.d.ts +2 -0
  75. package/dist/locale/en.js +41 -0
  76. package/dist/locale/en.js.map +1 -0
  77. package/dist/model.d.ts +90 -0
  78. package/dist/model.js +47 -0
  79. package/dist/model.js.map +1 -0
  80. package/dist/namespaces.d.ts +27 -0
  81. package/dist/namespaces.js +38 -0
  82. package/dist/namespaces.js.map +1 -0
  83. package/dist/naming.d.ts +76 -0
  84. package/dist/naming.js +201 -0
  85. package/dist/naming.js.map +1 -0
  86. package/dist/nested-error.d.ts +18 -0
  87. package/dist/nested-error.js +17 -0
  88. package/dist/nested-error.js.map +1 -0
  89. package/dist/secure-password.d.ts +31 -0
  90. package/dist/secure-password.js +166 -0
  91. package/dist/secure-password.js.map +1 -0
  92. package/dist/serialization.d.ts +40 -0
  93. package/dist/serialization.js +229 -0
  94. package/dist/serialization.js.map +1 -0
  95. package/dist/serializers/json.d.ts +27 -0
  96. package/dist/serializers/json.js +43 -0
  97. package/dist/serializers/json.js.map +1 -0
  98. package/dist/translation.d.ts +27 -0
  99. package/dist/translation.js +65 -0
  100. package/dist/translation.js.map +1 -0
  101. package/dist/type/big-integer.d.ts +6 -0
  102. package/dist/type/big-integer.js +10 -0
  103. package/dist/type/big-integer.js.map +1 -0
  104. package/dist/type/binary.d.ts +19 -0
  105. package/dist/type/binary.js +59 -0
  106. package/dist/type/binary.js.map +1 -0
  107. package/dist/type/boolean.d.ts +10 -0
  108. package/dist/type/boolean.js +38 -0
  109. package/dist/type/boolean.js.map +1 -0
  110. package/dist/type/date-time.d.ts +23 -0
  111. package/dist/type/date-time.js +56 -0
  112. package/dist/type/date-time.js.map +1 -0
  113. package/dist/type/date.d.ts +27 -0
  114. package/dist/type/date.js +80 -0
  115. package/dist/type/date.js.map +1 -0
  116. package/dist/type/decimal.d.ts +20 -0
  117. package/dist/type/decimal.js +75 -0
  118. package/dist/type/decimal.js.map +1 -0
  119. package/dist/type/float.d.ts +11 -0
  120. package/dist/type/float.js +40 -0
  121. package/dist/type/float.js.map +1 -0
  122. package/dist/type/helpers/accepts-multiparameter-time.d.ts +25 -0
  123. package/dist/type/helpers/accepts-multiparameter-time.js +51 -0
  124. package/dist/type/helpers/accepts-multiparameter-time.js.map +1 -0
  125. package/dist/type/helpers/mutable.d.ts +10 -0
  126. package/dist/type/helpers/mutable.js +13 -0
  127. package/dist/type/helpers/mutable.js.map +1 -0
  128. package/dist/type/helpers/numeric.d.ts +16 -0
  129. package/dist/type/helpers/numeric.js +48 -0
  130. package/dist/type/helpers/numeric.js.map +1 -0
  131. package/dist/type/helpers/time-value.d.ts +31 -0
  132. package/dist/type/helpers/time-value.js +104 -0
  133. package/dist/type/helpers/time-value.js.map +1 -0
  134. package/dist/type/helpers/timezone.d.ts +5 -0
  135. package/dist/type/helpers/timezone.js +16 -0
  136. package/dist/type/helpers/timezone.js.map +1 -0
  137. package/dist/type/helpers.d.ts +6 -0
  138. package/dist/type/helpers.js +6 -0
  139. package/dist/type/helpers.js.map +1 -0
  140. package/dist/type/immutable-string.d.ts +19 -0
  141. package/dist/type/immutable-string.js +44 -0
  142. package/dist/type/immutable-string.js.map +1 -0
  143. package/dist/type/integer.d.ts +36 -0
  144. package/dist/type/integer.js +87 -0
  145. package/dist/type/integer.js.map +1 -0
  146. package/dist/type/internal/sentinels.d.ts +12 -0
  147. package/dist/type/internal/sentinels.js +3 -0
  148. package/dist/type/internal/sentinels.js.map +1 -0
  149. package/dist/type/registry.d.ts +19 -0
  150. package/dist/type/registry.js +35 -0
  151. package/dist/type/registry.js.map +1 -0
  152. package/dist/type/serialize-cast-value.d.ts +32 -0
  153. package/dist/type/serialize-cast-value.js +57 -0
  154. package/dist/type/serialize-cast-value.js.map +1 -0
  155. package/dist/type/string.d.ts +8 -0
  156. package/dist/type/string.js +31 -0
  157. package/dist/type/string.js.map +1 -0
  158. package/dist/type/time.d.ts +16 -0
  159. package/dist/type/time.js +62 -0
  160. package/dist/type/time.js.map +1 -0
  161. package/dist/type/value.d.ts +42 -0
  162. package/dist/type/value.js +96 -0
  163. package/dist/type/value.js.map +1 -0
  164. package/dist/type.d.ts +10 -0
  165. package/dist/type.js +47 -0
  166. package/dist/type.js.map +1 -0
  167. package/dist/validations/absence.d.ts +10 -0
  168. package/dist/validations/absence.js +15 -0
  169. package/dist/validations/absence.js.map +1 -0
  170. package/dist/validations/acceptance.d.ts +37 -0
  171. package/dist/validations/acceptance.js +66 -0
  172. package/dist/validations/acceptance.js.map +1 -0
  173. package/dist/validations/callbacks.d.ts +45 -0
  174. package/dist/validations/callbacks.js +43 -0
  175. package/dist/validations/callbacks.js.map +1 -0
  176. package/dist/validations/clusivity.d.ts +37 -0
  177. package/dist/validations/clusivity.js +57 -0
  178. package/dist/validations/clusivity.js.map +1 -0
  179. package/dist/validations/comparability.d.ts +27 -0
  180. package/dist/validations/comparability.js +17 -0
  181. package/dist/validations/comparability.js.map +1 -0
  182. package/dist/validations/comparison.d.ts +15 -0
  183. package/dist/validations/comparison.js +41 -0
  184. package/dist/validations/comparison.js.map +1 -0
  185. package/dist/validations/confirmation.d.ts +29 -0
  186. package/dist/validations/confirmation.js +51 -0
  187. package/dist/validations/confirmation.js.map +1 -0
  188. package/dist/validations/exclusion.d.ts +13 -0
  189. package/dist/validations/exclusion.js +25 -0
  190. package/dist/validations/exclusion.js.map +1 -0
  191. package/dist/validations/format.d.ts +30 -0
  192. package/dist/validations/format.js +80 -0
  193. package/dist/validations/format.js.map +1 -0
  194. package/dist/validations/helper-methods.d.ts +24 -0
  195. package/dist/validations/helper-methods.js +32 -0
  196. package/dist/validations/helper-methods.js.map +1 -0
  197. package/dist/validations/inclusion.d.ts +13 -0
  198. package/dist/validations/inclusion.js +25 -0
  199. package/dist/validations/inclusion.js.map +1 -0
  200. package/dist/validations/length.d.ts +23 -0
  201. package/dist/validations/length.js +108 -0
  202. package/dist/validations/length.js.map +1 -0
  203. package/dist/validations/numericality.d.ts +76 -0
  204. package/dist/validations/numericality.js +202 -0
  205. package/dist/validations/numericality.js.map +1 -0
  206. package/dist/validations/presence.d.ts +10 -0
  207. package/dist/validations/presence.js +15 -0
  208. package/dist/validations/presence.js.map +1 -0
  209. package/dist/validations/resolve-value.d.ts +8 -0
  210. package/dist/validations/resolve-value.js +25 -0
  211. package/dist/validations/resolve-value.js.map +1 -0
  212. package/dist/validations/validates.d.ts +13 -0
  213. package/dist/validations/validates.js +54 -0
  214. package/dist/validations/validates.js.map +1 -0
  215. package/dist/validations/with.d.ts +21 -0
  216. package/dist/validations/with.js +46 -0
  217. package/dist/validations/with.js.map +1 -0
  218. package/dist/validations.d.ts +138 -0
  219. package/dist/validations.js +242 -0
  220. package/dist/validations.js.map +1 -0
  221. package/dist/validator.d.ts +31 -0
  222. package/dist/validator.js +62 -0
  223. package/dist/validator.js.map +1 -0
  224. package/dist/version.d.ts +3 -0
  225. package/dist/version.js +5 -0
  226. package/dist/version.js.map +1 -0
  227. package/package.json +106 -3
  228. package/README.md +0 -3
@@ -0,0 +1,232 @@
1
+ import { Concurrent } from "@blazetrails/ruby-compat/concurrent/map";
2
+ import { Hash } from "@blazetrails/ruby-compat/hash";
3
+ import { CodeGenerator, included, type Extended, type Included, Module } from "@blazetrails/activesupport";
4
+ export interface AttributeMethods {
5
+ methodMissing(method: string, ...args: unknown[]): unknown;
6
+ attributeMissing(match: AttributeMethod, ...args: unknown[]): unknown;
7
+ isRespondTo(method: string): boolean;
8
+ }
9
+ export declare class MissingAttributeError extends globalThis.Error {
10
+ constructor(message?: string);
11
+ }
12
+ export declare class AttrNames {
13
+ static readonly DEF_SAFE_NAME: RegExp;
14
+ static defineAttributeAccessorMethod(owner: unknown, attrName: string, { writer }: {
15
+ writer?: boolean;
16
+ }, block: (tempMethodName: string, attrNameExpr: string) => void): void;
17
+ }
18
+ export declare class AttributeMethodPattern {
19
+ readonly prefix: string;
20
+ readonly suffix: string;
21
+ readonly proxyTarget: string;
22
+ readonly parameters: string | false;
23
+ private readonly regex;
24
+ constructor({ prefix, suffix, parameters, }?: {
25
+ prefix?: string;
26
+ suffix?: string;
27
+ parameters?: string | null | false;
28
+ });
29
+ match(methodName: string): AttributeMethod | null;
30
+ methodName(attrName: string): string;
31
+ private attrName;
32
+ private get camelJoined();
33
+ }
34
+ export declare class AttributeMethod {
35
+ readonly proxyTarget: string;
36
+ readonly attrName: string;
37
+ constructor(proxyTarget: string, attrName: string);
38
+ }
39
+ export interface ReadWriteHost {
40
+ /** @internal */
41
+ _readAttribute(attr: string): unknown;
42
+ /** @internal */
43
+ _writeAttribute(name: string, value: unknown): void;
44
+ [key: string]: unknown;
45
+ }
46
+ export interface AttributeMethodHost {
47
+ attributeNames(): string[];
48
+ attributeMethodPatterns: AttributeMethodPattern[];
49
+ attributeAliases: Record<string, string>;
50
+ _aliasesByAttributeName: Hash<string, string[]>;
51
+ _generatedAttributeMethods?: Module;
52
+ }
53
+ export interface ClassMethodsHost extends AttributeMethodHost, Extended<typeof ClassMethods> {
54
+ }
55
+ export interface InstanceHost {
56
+ _attributes?: {
57
+ isKey(name: string): boolean;
58
+ };
59
+ attributes: Record<string, unknown>;
60
+ attributeMethodPatterns?: AttributeMethodPattern[];
61
+ constructor: AttributeMethodHost;
62
+ }
63
+ export interface InstanceMethodsHost extends InstanceHost, Included<typeof AttributeMethods> {
64
+ constructor: ClassMethodsHost;
65
+ }
66
+ export declare const ClassMethods: {
67
+ attributeMethodPrefix(this: ClassMethodsHost, ...prefixes: string[] | [...prefixes: string[], kwargs: {
68
+ parameters?: string | null | false;
69
+ }]): void;
70
+ attributeMethodSuffix(this: ClassMethodsHost, ...suffixes: string[] | [...suffixes: string[], kwargs: {
71
+ parameters?: string | null | false;
72
+ }]): void;
73
+ attributeMethodAffix(this: ClassMethodsHost, ...affixes: Array<{
74
+ prefix: string;
75
+ suffix: string;
76
+ parameters?: string | null | false;
77
+ }>): void;
78
+ aliasAttribute(this: ClassMethodsHost, newName: string, oldName: string): void;
79
+ eagerlyGenerateAliasAttributeMethods(this: ClassMethodsHost, newName: string, oldName: string): void;
80
+ generateAliasAttributeMethods(this: ClassMethodsHost, codeGenerator: CodeGenerator, newName: string, oldName: string): void;
81
+ aliasAttributeMethodDefinition(this: ClassMethodsHost, codeGenerator: CodeGenerator, pattern: AttributeMethodPattern, newName: string, oldName: string): void;
82
+ isAttributeAlias(this: ClassMethodsHost, newName: string): boolean;
83
+ attributeAlias(this: ClassMethodsHost, name: string): string | undefined;
84
+ defineAttributeMethods(this: ClassMethodsHost, ...attrNames: string[]): void;
85
+ defineAttributeMethod(this: ClassMethodsHost, attrName: string, options?: {
86
+ _owner?: Module | CodeGenerator;
87
+ as?: string;
88
+ }): void;
89
+ /**
90
+ * @inventedArm if — PERMANENT
91
+ * @inventedArm answersWithAMethod — PERMANENT
92
+ */
93
+ defineAttributeMethodPattern(this: ClassMethodsHost, pattern: AttributeMethodPattern, attrName: string, { owner, as, override }: {
94
+ owner: CodeGenerator;
95
+ as: string;
96
+ override?: boolean;
97
+ }): void;
98
+ undefineAttributeMethods(this: ClassMethodsHost): void;
99
+ /** @inventedArm if — PERMANENT */
100
+ aliasesByAttributeName(this: ClassMethodsHost): Hash<string, string[]>;
101
+ /** @internal */
102
+ resolveAttributeName(this: ClassMethodsHost, name: string): string;
103
+ /**
104
+ * @internal
105
+ * @inventedArm if — PERMANENT
106
+ */
107
+ generatedAttributeMethods(this: ClassMethodsHost): Module;
108
+ /** @internal */
109
+ isInstanceMethodAlreadyImplemented(this: ClassMethodsHost, methodName: string): boolean;
110
+ /**
111
+ * @internal
112
+ * @inventedArm if — PERMANENT
113
+ */
114
+ attributeMethodPatternsCache(this: ClassMethodsHost): InstanceType<typeof Concurrent.Map<string, Array<AttributeMethod>>>;
115
+ /** @internal */
116
+ attributeMethodPatternsMatching(this: ClassMethodsHost, methodName: string): Array<AttributeMethod>;
117
+ /** @internal */
118
+ defineProxyCall(this: ClassMethodsHost, codeGenerator: CodeGenerator, name: string, proxyTarget: string, parameters: string | null | false, ...rest: [...callArgs: string[], options: {
119
+ namespace: string;
120
+ as?: string;
121
+ }]): void;
122
+ /** @internal */
123
+ buildMangledName(name: string): string;
124
+ /**
125
+ * @internal
126
+ * @inventedArm if — PERMANENT
127
+ * @inventedArm if — PERMANENT
128
+ * @inventedArm if — PERMANENT
129
+ * @inventedArm rbObjRespondTo — PERMANENT
130
+ * @inventedArm slice — PERMANENT
131
+ */
132
+ defineCall(this: ClassMethodsHost, codeGenerator: CodeGenerator, name: string, targetName: string, mangledName: string, parameters: string | null | false, callArgs: string[], { namespace, as }: {
133
+ namespace: string;
134
+ as: string;
135
+ }): void;
136
+ };
137
+ export declare const AttributeMethods: {
138
+ ClassMethods: {
139
+ attributeMethodPrefix(this: ClassMethodsHost, ...prefixes: string[] | [...prefixes: string[], kwargs: {
140
+ parameters?: string | null | false;
141
+ }]): void;
142
+ attributeMethodSuffix(this: ClassMethodsHost, ...suffixes: string[] | [...suffixes: string[], kwargs: {
143
+ parameters?: string | null | false;
144
+ }]): void;
145
+ attributeMethodAffix(this: ClassMethodsHost, ...affixes: Array<{
146
+ prefix: string;
147
+ suffix: string;
148
+ parameters?: string | null | false;
149
+ }>): void;
150
+ aliasAttribute(this: ClassMethodsHost, newName: string, oldName: string): void;
151
+ eagerlyGenerateAliasAttributeMethods(this: ClassMethodsHost, newName: string, oldName: string): void;
152
+ generateAliasAttributeMethods(this: ClassMethodsHost, codeGenerator: CodeGenerator, newName: string, oldName: string): void;
153
+ aliasAttributeMethodDefinition(this: ClassMethodsHost, codeGenerator: CodeGenerator, pattern: AttributeMethodPattern, newName: string, oldName: string): void;
154
+ isAttributeAlias(this: ClassMethodsHost, newName: string): boolean;
155
+ attributeAlias(this: ClassMethodsHost, name: string): string | undefined;
156
+ defineAttributeMethods(this: ClassMethodsHost, ...attrNames: string[]): void;
157
+ defineAttributeMethod(this: ClassMethodsHost, attrName: string, options?: {
158
+ _owner?: Module | CodeGenerator;
159
+ as?: string;
160
+ }): void;
161
+ /**
162
+ * @inventedArm if — PERMANENT
163
+ * @inventedArm answersWithAMethod — PERMANENT
164
+ */
165
+ defineAttributeMethodPattern(this: ClassMethodsHost, pattern: AttributeMethodPattern, attrName: string, { owner, as, override }: {
166
+ owner: CodeGenerator;
167
+ as: string;
168
+ override?: boolean;
169
+ }): void;
170
+ undefineAttributeMethods(this: ClassMethodsHost): void;
171
+ /** @inventedArm if — PERMANENT */
172
+ aliasesByAttributeName(this: ClassMethodsHost): Hash<string, string[]>;
173
+ /** @internal */
174
+ resolveAttributeName(this: ClassMethodsHost, name: string): string;
175
+ /**
176
+ * @internal
177
+ * @inventedArm if — PERMANENT
178
+ */
179
+ generatedAttributeMethods(this: ClassMethodsHost): Module;
180
+ /** @internal */
181
+ isInstanceMethodAlreadyImplemented(this: ClassMethodsHost, methodName: string): boolean;
182
+ /**
183
+ * @internal
184
+ * @inventedArm if — PERMANENT
185
+ */
186
+ attributeMethodPatternsCache(this: ClassMethodsHost): InstanceType<typeof Concurrent.Map<string, Array<AttributeMethod>>>;
187
+ /** @internal */
188
+ attributeMethodPatternsMatching(this: ClassMethodsHost, methodName: string): Array<AttributeMethod>;
189
+ /** @internal */
190
+ defineProxyCall(this: ClassMethodsHost, codeGenerator: CodeGenerator, name: string, proxyTarget: string, parameters: string | null | false, ...rest: [...callArgs: string[], options: {
191
+ namespace: string;
192
+ as?: string;
193
+ }]): void;
194
+ /** @internal */
195
+ buildMangledName(name: string): string;
196
+ /**
197
+ * @internal
198
+ * @inventedArm if — PERMANENT
199
+ * @inventedArm if — PERMANENT
200
+ * @inventedArm if — PERMANENT
201
+ * @inventedArm rbObjRespondTo — PERMANENT
202
+ * @inventedArm slice — PERMANENT
203
+ */
204
+ defineCall(this: ClassMethodsHost, codeGenerator: CodeGenerator, name: string, targetName: string, mangledName: string, parameters: string | null | false, callArgs: string[], { namespace, as }: {
205
+ namespace: string;
206
+ as: string;
207
+ }): void;
208
+ };
209
+ [included](base: (new (...args: never[]) => unknown) & {
210
+ prototype: object;
211
+ }): void;
212
+ methodMissing(this: InstanceMethodsHost, method: string, ...args: unknown[]): unknown;
213
+ attributeMissing(this: Record<string, unknown>, match: AttributeMethod, ...args: unknown[]): unknown;
214
+ isRespondToWithoutAttributes(this: object, method: string, includePrivateMethods?: boolean): boolean;
215
+ isRespondTo(this: InstanceMethodsHost, method: string, includePrivateMethods?: boolean): boolean;
216
+ /** @internal */
217
+ isAttributeMethod(this: InstanceMethodsHost, attrName: string): boolean;
218
+ /** @internal */
219
+ matchedAttributeMethod(this: InstanceMethodsHost, methodName: string): AttributeMethod | null;
220
+ /** @internal */
221
+ missingAttribute(this: InstanceHost, attrName: string, stack: string[]): never;
222
+ /** @internal */
223
+ _readAttribute(this: InstanceMethodsHost, attr: string): unknown;
224
+ };
225
+ /** @noRailsEquivalent PERMANENT */
226
+ export declare function completeHalfAccessor(klass: unknown, name: string, half: "get" | "set", fn: (this: never, ...args: never[]) => unknown): void;
227
+ /** @noRailsEquivalent PERMANENT */
228
+ export declare function defineMethodAttribute(this: unknown, canonicalName: string, { owner, as }: {
229
+ owner: CodeGenerator;
230
+ as?: string;
231
+ }): void;
232
+ //# sourceMappingURL=attribute-methods.d.ts.map
@@ -0,0 +1,429 @@
1
+ import { unpack1 } from "@blazetrails/ruby-compat/array";
2
+ import { excSetBacktrace, rbFCaller } from "@blazetrails/ruby-compat/backtrace-location";
3
+ import { Concurrent } from "@blazetrails/ruby-compat/concurrent/map";
4
+ import { filterMap } from "@blazetrails/ruby-compat/enumerable";
5
+ import { block, fetch, Hash, isInclude } from "@blazetrails/ruby-compat/hash";
6
+ import { rbClearMethodCache, constDefined, constSet } from "@blazetrails/ruby-compat/include";
7
+ import { basicObjRespondTo, rbFSend, rbObjRespondTo } from "@blazetrails/ruby-compat/object";
8
+ import { regexpEscape } from "@blazetrails/ruby-compat/regexp";
9
+ import { b } from "@blazetrails/ruby-compat/string/b";
10
+ import { camelize, classAttribute, CodeGenerator, extend, include, included, Module, } from "@blazetrails/activesupport";
11
+ import { NoMethodError } from "./attribute-assignment.js";
12
+ import { AttributeRegistration } from "./attribute-registration.js";
13
+ const __FILE__ = import.meta.url;
14
+ const __LINE__ = 0;
15
+ export class MissingAttributeError extends globalThis.Error {
16
+ constructor(message) {
17
+ super(message);
18
+ this.name = "MissingAttributeError";
19
+ }
20
+ }
21
+ export class AttrNames {
22
+ static DEF_SAFE_NAME = /^[a-zA-Z_]\w*$/;
23
+ static defineAttributeAccessorMethod(owner, attrName, { writer = false }, block) {
24
+ const methodName = `${attrName}${writer ? "=" : ""}`;
25
+ // eslint-disable-next-line no-control-regex -- Ruby's `ascii_only?` (attribute_methods.rb:579)
26
+ if (/^[\x00-\x7f]*$/.test(attrName) && AttrNames.DEF_SAFE_NAME.test(attrName)) {
27
+ block(methodName, `'${attrName}'`);
28
+ }
29
+ else {
30
+ const safeName = unpack1(b(attrName), "h*");
31
+ const constName = `ATTR_${safeName}`;
32
+ if (!constDefined(this, constName))
33
+ constSet(this, constName, attrName);
34
+ const tempMethodName = `__temp__${safeName}${writer ? "=" : ""}`;
35
+ const attrNameExpr = `::ActiveModel::AttributeMethods::AttrNames::${constName}`;
36
+ block(tempMethodName, attrNameExpr);
37
+ }
38
+ }
39
+ }
40
+ export class AttributeMethodPattern {
41
+ prefix;
42
+ suffix;
43
+ proxyTarget;
44
+ parameters;
45
+ regex;
46
+ constructor({ prefix = "", suffix = "", parameters = null, } = {}) {
47
+ const bang = suffix.endsWith("!");
48
+ this.prefix = prefix;
49
+ this.suffix = bang ? suffix.slice(0, -1) : suffix;
50
+ this.parameters = parameters == null ? "..." : bang && parameters === false ? "" : parameters;
51
+ this.regex = new RegExp(`^(?:${regexpEscape(this.prefix)})(.*)(?:${regexpEscape(this.suffix)})$`);
52
+ this.proxyTarget = `${prefix}${this.camelJoined ? "Attribute" : "attribute"}${this.suffix}${bang ? "Bang" : ""}`;
53
+ }
54
+ match(methodName) {
55
+ const md = this.regex.exec(methodName);
56
+ if (md) {
57
+ return new AttributeMethod(this.proxyTarget, this.attrName(md[1]));
58
+ }
59
+ return null;
60
+ }
61
+ methodName(attrName) {
62
+ const name = this.camelJoined ? attrName.charAt(0).toUpperCase() + attrName.slice(1) : attrName;
63
+ return `${this.prefix}${name}${this.suffix}`;
64
+ }
65
+ attrName(name) {
66
+ return this.camelJoined ? name.charAt(0).toLowerCase() + name.slice(1) : name;
67
+ }
68
+ get camelJoined() {
69
+ return this.prefix !== "" && !this.prefix.endsWith("_");
70
+ }
71
+ }
72
+ export class AttributeMethod {
73
+ proxyTarget;
74
+ attrName;
75
+ constructor(proxyTarget, attrName) {
76
+ this.proxyTarget = proxyTarget;
77
+ this.attrName = attrName;
78
+ }
79
+ }
80
+ const NAME_COMPILABLE_REGEXP = /^[a-zA-Z_]\w*[!?=]?$/;
81
+ const CALL_COMPILABLE_REGEXP = /^[a-zA-Z_]\w*[!?]?$/;
82
+ export const ClassMethods = {
83
+ attributeMethodPrefix(...prefixes) {
84
+ const last = prefixes[prefixes.length - 1];
85
+ const { parameters = null } = typeof last === "object" && last !== null
86
+ ? prefixes.pop()
87
+ : {};
88
+ this.attributeMethodPatterns = [
89
+ ...this.attributeMethodPatterns,
90
+ ...prefixes.map((prefix) => new AttributeMethodPattern({ prefix, parameters })),
91
+ ];
92
+ this.undefineAttributeMethods();
93
+ },
94
+ attributeMethodSuffix(...suffixes) {
95
+ const last = suffixes[suffixes.length - 1];
96
+ const { parameters = null } = typeof last === "object" && last !== null
97
+ ? suffixes.pop()
98
+ : {};
99
+ this.attributeMethodPatterns = [
100
+ ...this.attributeMethodPatterns,
101
+ ...suffixes.map((suffix) => new AttributeMethodPattern({ suffix, parameters })),
102
+ ];
103
+ this.undefineAttributeMethods();
104
+ },
105
+ attributeMethodAffix(...affixes) {
106
+ this.attributeMethodPatterns = [
107
+ ...this.attributeMethodPatterns,
108
+ ...affixes.map((affix) => new AttributeMethodPattern(affix)),
109
+ ];
110
+ this.undefineAttributeMethods();
111
+ },
112
+ aliasAttribute(newName, oldName) {
113
+ this.attributeAliases = { ...this.attributeAliases, [newName]: oldName };
114
+ this.aliasesByAttributeName().get(oldName).push(newName);
115
+ this.eagerlyGenerateAliasAttributeMethods(newName, oldName);
116
+ },
117
+ eagerlyGenerateAliasAttributeMethods(newName, oldName) {
118
+ CodeGenerator.batch(this.generatedAttributeMethods(), __FILE__, __LINE__, (codeGenerator) => {
119
+ this.generateAliasAttributeMethods(codeGenerator, newName, oldName);
120
+ });
121
+ },
122
+ generateAliasAttributeMethods(codeGenerator, newName, oldName) {
123
+ CodeGenerator.batch(codeGenerator, __FILE__, __LINE__, () => {
124
+ for (const pattern of this.attributeMethodPatterns) {
125
+ this.aliasAttributeMethodDefinition(codeGenerator, pattern, newName, oldName);
126
+ }
127
+ this.attributeMethodPatternsCache().clear();
128
+ });
129
+ },
130
+ aliasAttributeMethodDefinition(codeGenerator, pattern, newName, oldName) {
131
+ const methodName = pattern.methodName(newName);
132
+ const targetName = pattern.methodName(oldName);
133
+ const parameters = pattern.parameters;
134
+ const mangledName = this.buildMangledName(targetName);
135
+ const callArgs = [];
136
+ if (parameters)
137
+ callArgs.push(parameters);
138
+ this.defineCall(codeGenerator, methodName, targetName, mangledName, parameters, callArgs, {
139
+ namespace: "alias_attribute",
140
+ as: methodName,
141
+ });
142
+ },
143
+ isAttributeAlias(newName) {
144
+ return Object.hasOwn(this.attributeAliases, newName);
145
+ },
146
+ attributeAlias(name) {
147
+ return this.attributeAliases[name];
148
+ },
149
+ defineAttributeMethods(...attrNames) {
150
+ CodeGenerator.batch(this.generatedAttributeMethods(), __FILE__, __LINE__, (owner) => {
151
+ for (const attrName of attrNames) {
152
+ this.defineAttributeMethod(attrName, { _owner: owner });
153
+ for (const aliasedName of this.aliasesByAttributeName().get(attrName)) {
154
+ this.generateAliasAttributeMethods(owner, aliasedName, attrName);
155
+ }
156
+ }
157
+ });
158
+ },
159
+ defineAttributeMethod(attrName, options = {}) {
160
+ const { _owner = this.generatedAttributeMethods(), as = attrName } = options;
161
+ CodeGenerator.batch(_owner, __FILE__, __LINE__, (owner) => {
162
+ for (const pattern of this.attributeMethodPatterns) {
163
+ this.defineAttributeMethodPattern(pattern, attrName, { owner, as });
164
+ }
165
+ this.attributeMethodPatternsCache().clear();
166
+ });
167
+ },
168
+ /**
169
+ * @inventedArm if — PERMANENT
170
+ * @inventedArm answersWithAMethod — PERMANENT
171
+ */
172
+ defineAttributeMethodPattern(pattern, attrName, { owner, as, override = false }) {
173
+ const canonicalMethodName = pattern.methodName(attrName);
174
+ const publicMethodName = pattern.methodName(as);
175
+ if (this.isInstanceMethodAlreadyImplemented(publicMethodName)) {
176
+ if (!override)
177
+ return;
178
+ }
179
+ if (pattern.parameters === false && !override && answersWithAMethod(this, publicMethodName)) {
180
+ return;
181
+ }
182
+ const generateMethod = camelize(`define_method_${pattern.proxyTarget}`, false);
183
+ if (rbObjRespondTo(this, generateMethod)) {
184
+ rbFSend(this, generateMethod, String(attrName), { owner, as });
185
+ }
186
+ else {
187
+ this.defineProxyCall(owner, canonicalMethodName, pattern.proxyTarget, pattern.parameters, attrName, {
188
+ namespace: "active_model_proxy",
189
+ as: publicMethodName,
190
+ });
191
+ }
192
+ },
193
+ undefineAttributeMethods() {
194
+ const mod = this.generatedAttributeMethods();
195
+ mod.undefMethod(...mod.instanceMethods());
196
+ this.attributeMethodPatternsCache().clear();
197
+ },
198
+ /** @inventedArm if — PERMANENT */
199
+ aliasesByAttributeName() {
200
+ if (!Object.hasOwn(this, "_aliasesByAttributeName")) {
201
+ this._aliasesByAttributeName = new Hash((h, k) => {
202
+ const v = [];
203
+ h.set(k, v);
204
+ return v;
205
+ });
206
+ }
207
+ return this._aliasesByAttributeName;
208
+ },
209
+ /** @internal */
210
+ resolveAttributeName(name) {
211
+ return fetch(this.attributeAliases, AttributeRegistration.ClassMethods.resolveAttributeName.call(this, name), block((key) => key));
212
+ },
213
+ /**
214
+ * @internal
215
+ * @inventedArm if — PERMANENT
216
+ */
217
+ generatedAttributeMethods() {
218
+ if (!Object.hasOwn(this, "_generatedAttributeMethods")) {
219
+ const mod = new Module();
220
+ include(this, mod);
221
+ this._generatedAttributeMethods = mod;
222
+ }
223
+ return this._generatedAttributeMethods;
224
+ },
225
+ /** @internal */
226
+ isInstanceMethodAlreadyImplemented(methodName) {
227
+ return this.generatedAttributeMethods().isMethodDefined(methodName);
228
+ },
229
+ /**
230
+ * @internal
231
+ * @inventedArm if — PERMANENT
232
+ */
233
+ attributeMethodPatternsCache() {
234
+ const h = this;
235
+ if (!Object.hasOwn(h, "_attributeMethodPatternsCache")) {
236
+ h._attributeMethodPatternsCache = new Concurrent.Map({ initialCapacity: 4 });
237
+ }
238
+ return h._attributeMethodPatternsCache;
239
+ },
240
+ /** @internal */
241
+ attributeMethodPatternsMatching(methodName) {
242
+ return this.attributeMethodPatternsCache().computeIfAbsent(methodName, () => filterMap(this.attributeMethodPatterns, (pattern) => pattern.match(methodName)));
243
+ },
244
+ /** @internal */
245
+ defineProxyCall(codeGenerator, name, proxyTarget, parameters, ...rest) {
246
+ const options = rest[rest.length - 1];
247
+ const callArgs = rest.slice(0, -1);
248
+ const mangledName = this.buildMangledName(name);
249
+ if (parameters)
250
+ callArgs.push(parameters);
251
+ const namespace = `${options.namespace}_${proxyTarget}`;
252
+ this.defineCall(codeGenerator, name, proxyTarget, mangledName, parameters, callArgs, {
253
+ namespace,
254
+ as: options.as ?? name,
255
+ });
256
+ },
257
+ /** @internal */
258
+ buildMangledName(name) {
259
+ if (NAME_COMPILABLE_REGEXP.test(name))
260
+ return name;
261
+ return `__temp__${unpack1(b(name), "h*")}`;
262
+ },
263
+ /**
264
+ * @internal
265
+ * @inventedArm if — PERMANENT
266
+ * @inventedArm if — PERMANENT
267
+ * @inventedArm if — PERMANENT
268
+ * @inventedArm rbObjRespondTo — PERMANENT
269
+ * @inventedArm slice — PERMANENT
270
+ */
271
+ defineCall(codeGenerator, name, targetName, mangledName, parameters, callArgs, { namespace, as }) {
272
+ const reader = this.attributeAliases[name] === targetName && rbObjRespondTo(this, "defineMethodAttribute");
273
+ let descriptor;
274
+ let cachedName = mangledName;
275
+ if (reader) {
276
+ cachedName = `${mangledName}__reader`;
277
+ descriptor = (body) => ({
278
+ get() {
279
+ return body(this, []);
280
+ },
281
+ set(value) {
282
+ rbFSend(this, `${targetName}=`, value);
283
+ },
284
+ configurable: true,
285
+ });
286
+ }
287
+ else if (parameters === false) {
288
+ cachedName = `${mangledName}__getter`;
289
+ descriptor = (body) => ({
290
+ get() {
291
+ return body(this, []);
292
+ },
293
+ configurable: true,
294
+ });
295
+ }
296
+ else {
297
+ descriptor = (body) => ({
298
+ value: function (...args) {
299
+ return body(this, args);
300
+ },
301
+ writable: true,
302
+ configurable: true,
303
+ });
304
+ }
305
+ codeGenerator.defineCachedMethod(cachedName, { namespace, as }, (batch) => {
306
+ let body;
307
+ if (CALL_COMPILABLE_REGEXP.test(targetName)) {
308
+ body = (self, args) => rbFSend(self, targetName, ...sent, ...args);
309
+ }
310
+ else {
311
+ callArgs.unshift(targetName);
312
+ body = (self, args) => rbFSend(self, ...sent, ...args);
313
+ }
314
+ const sent = parameters ? callArgs.slice(0, -1) : callArgs;
315
+ batch.push((mod) => {
316
+ Object.defineProperty(mod, cachedName, descriptor(body));
317
+ });
318
+ });
319
+ },
320
+ };
321
+ export const AttributeMethods = {
322
+ ClassMethods,
323
+ [included](base) {
324
+ extend(base, ClassMethods);
325
+ classAttribute.call(base, "attributeAliases", { instanceWriter: false, default: {} });
326
+ classAttribute.call(base, "attributeMethodPatterns", {
327
+ instanceWriter: false,
328
+ default: [new AttributeMethodPattern()],
329
+ });
330
+ },
331
+ methodMissing(method, ...args) {
332
+ if (this.isRespondToWithoutAttributes(method, true)) {
333
+ throw new NoMethodError(`undefined method '${method}' for an instance of ${this.constructor.name ?? "unknown"}`);
334
+ }
335
+ else {
336
+ const match = this.matchedAttributeMethod(method);
337
+ if (match)
338
+ return this.attributeMissing(match, ...args);
339
+ throw new NoMethodError(`undefined method '${method}' for an instance of ${this.constructor.name ?? "unknown"}`);
340
+ }
341
+ },
342
+ attributeMissing(match, ...args) {
343
+ return rbFSend(this, match.proxyTarget, match.attrName, ...args);
344
+ },
345
+ isRespondToWithoutAttributes(method, includePrivateMethods = false) {
346
+ return basicObjRespondTo(this, method);
347
+ },
348
+ isRespondTo(method, includePrivateMethods = false) {
349
+ if (basicObjRespondTo(this, method)) {
350
+ return true;
351
+ }
352
+ else {
353
+ return this.matchedAttributeMethod(String(method)) !== null;
354
+ }
355
+ },
356
+ /** @internal */
357
+ isAttributeMethod(attrName) {
358
+ return this.isRespondToWithoutAttributes("attributes") && isInclude(this.attributes, attrName);
359
+ },
360
+ /** @internal */
361
+ matchedAttributeMethod(methodName) {
362
+ const matches = this.constructor.attributeMethodPatternsMatching(methodName);
363
+ return matches.find((m) => this.isAttributeMethod(m.attrName)) ?? null;
364
+ },
365
+ /** @internal */
366
+ missingAttribute(attrName, stack) {
367
+ const err = new MissingAttributeError(`missing attribute '${attrName}' for ${this.constructor.name ?? "unknown"}`);
368
+ excSetBacktrace(err, stack);
369
+ throw err;
370
+ },
371
+ /** @internal */
372
+ _readAttribute(attr) {
373
+ return rbFSend(this, attr);
374
+ },
375
+ };
376
+ function answersWithAMethod(klass, name) {
377
+ const start = klass.prototype;
378
+ for (let link = start ?? null; link; link = Object.getPrototypeOf(link)) {
379
+ const descriptor = Object.getOwnPropertyDescriptor(link, name);
380
+ if (!descriptor)
381
+ continue;
382
+ return typeof descriptor.value === "function";
383
+ }
384
+ return false;
385
+ }
386
+ function isDefinedByAClassBody(klass, name) {
387
+ const start = klass.prototype;
388
+ for (let link = start ?? null; link; link = Object.getPrototypeOf(link)) {
389
+ if (!Object.hasOwn(link, name))
390
+ continue;
391
+ return Object.hasOwn(link, "constructor");
392
+ }
393
+ return false;
394
+ }
395
+ /** @noRailsEquivalent PERMANENT */
396
+ export function completeHalfAccessor(klass, name, half, fn) {
397
+ const proto = klass.prototype;
398
+ if (proto == null)
399
+ return;
400
+ const desc = Object.getOwnPropertyDescriptor(proto, name);
401
+ if (desc == null || "value" in desc || desc[half] != null)
402
+ return;
403
+ Object.defineProperty(proto, name, { ...desc, [half]: fn, configurable: true });
404
+ rbClearMethodCache();
405
+ }
406
+ /** @noRailsEquivalent PERMANENT */
407
+ export function defineMethodAttribute(canonicalName, { owner, as = canonicalName }) {
408
+ if (as === canonicalName && isDefinedByAClassBody(this, as))
409
+ return;
410
+ AttrNames.defineAttributeAccessorMethod(owner, canonicalName, {}, (tempMethodName) => {
411
+ owner.defineCachedMethod(tempMethodName, { namespace: "active_model", as }, (sources) => {
412
+ sources.push((mod) => {
413
+ Object.defineProperty(mod, tempMethodName, {
414
+ get() {
415
+ if (!this._attributes.getAttribute(canonicalName).isInitialized()) {
416
+ this.missingAttribute(canonicalName, rbFCaller());
417
+ }
418
+ return this.attribute(canonicalName);
419
+ },
420
+ set(value) {
421
+ this._writeAttribute(canonicalName, value);
422
+ },
423
+ configurable: true,
424
+ });
425
+ });
426
+ });
427
+ });
428
+ }
429
+ //# sourceMappingURL=attribute-methods.js.map