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

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