Skip to content

Commit dc6aca5

Browse files
committed
Added documents for Columns / Associations / Datasets
1 parent 14e90eb commit dc6aca5

File tree

6 files changed

+731
-0
lines changed

6 files changed

+731
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SimpleMaster Associations (English)
2+
3+
## Overview
4+
SimpleMaster provides `belongs_to`, `has_one`, `has_many`, and `has_many :through`.
5+
6+
## Definitions
7+
```ruby
8+
class Player < ApplicationRecord
9+
belongs_to :level, foreign_key: :lv, primary_key: :lv
10+
has_many :player_items
11+
end
12+
```
13+
14+
```ruby
15+
class Reward < ApplicationMaster
16+
belongs_to :enemy
17+
belongs_to :reward, polymorphic: true
18+
end
19+
```
20+
21+
The lookup path depends on whether the target is `SimpleMaster::Master`
22+
or `ActiveRecord::Base`.
23+
24+
- Master to Master: use `all_by` / `find_by`
25+
- These are fast, so values are fetched on each call. Cache in a variable if used often.
26+
- ActiveRecord: use `simple_master_connection`
27+
- `belongs_to_store` / `has_many_store` (RequestStore) caches per request.
28+
29+
## Common options
30+
### `class_name:`
31+
- Example: `belongs_to :reward, class_name: "Weapon"`
32+
- Explicitly sets the target class.
33+
34+
### `foreign_key:`
35+
- Example: `has_many :players, foreign_key: :lv`
36+
- Sets the foreign key column.
37+
38+
### `primary_key:`
39+
- Example: `belongs_to :level, primary_key: :lv`
40+
- Sets the target primary key (default is `:id`).
41+
42+
## Association types
43+
- `belongs_to` : `belongs_to :enemy`
44+
- `belongs_to (polymorphic)` : `belongs_to :reward, polymorphic: true`
45+
- Requires `def_column :reward_type, polymorphic_type: true`
46+
- `has_one` : `has_one :profile`
47+
- `has_many` : `has_many :players, foreign_key: :lv`
48+
- `has_many :through` : `has_many :items, through: :player_items`
49+
- `source:` can rename the target association
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# SimpleMaster Association 仕様 (日本語)
2+
3+
## 全体説明
4+
SimpleMaster の Association は `belongs_to` / `has_one` / `has_many` / `has_many :through` を提供します。
5+
6+
## 定義方法
7+
```ruby
8+
class Player < ApplicationRecord
9+
belongs_to :level, foreign_key: :lv, primary_key: :lv
10+
has_many :player_items
11+
end
12+
```
13+
14+
```ruby
15+
class Reward < ApplicationMaster
16+
belongs_to :enemy
17+
belongs_to :reward, polymorphic: true
18+
end
19+
```
20+
21+
対象が `SimpleMaster::Master``ActiveRecord::Base` かで参照方法が変わります。
22+
23+
- Master 同士: `all_by` / `find_by` を使った参照
24+
- 取得が高速なため、都度引き直しとなります。利用時に呼ぶ回数多いなら、変数に格納してください。
25+
- ActiveRecord: `simple_master_connection` で DB 参照
26+
- `belongs_to_store` / `has_many_store` (RequestStore) に保持されるため、リクエストごとにキャッシュが効きます。
27+
28+
## 共通オプション
29+
### `class_name:`
30+
- 例: `belongs_to :reward, class_name: "Weapon"`
31+
- 明示的に参照先クラスを指定します。
32+
33+
### `foreign_key:`
34+
- 例: `has_many :players, foreign_key: :lv`
35+
- 外部キー名を指定します。
36+
37+
### `primary_key:`
38+
- 例: `belongs_to :level, primary_key: :lv`
39+
- 参照先のキーを指定します(デフォルトは `:id`)。
40+
41+
## Association 種別
42+
- `belongs_to` : `belongs_to :enemy`
43+
- `belongs_to (polymorphic)` : `belongs_to :reward, polymorphic: true`
44+
- 前提: `def_column :reward_type, polymorphic_type: true`
45+
- `has_one` : `has_one :profile`
46+
- `has_many` : `has_many :players, foreign_key: :lv`
47+
- `has_many :through` : `has_many :items, through: :player_items`
48+
- `source:` を指定すると参照先の名前を変更できます。

docs/simple_master_columns_en.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# SimpleMaster Columns (English)
2+
3+
## Overview
4+
SimpleMaster columns are defined with `def_column`. At load time, type conversion,
5+
cache helpers, and accessor methods are generated.
6+
The behavior depends on `type` and DSL options.
7+
8+
```ruby
9+
class Weapon < ApplicationMaster
10+
def_column :id
11+
def_column :name, type: :string
12+
def_column :attack, type: :float
13+
def_column :rarity
14+
15+
enum :rarity, { common: 0, rare: 1, epic: 2 }
16+
end
17+
```
18+
19+
## Common options
20+
### `type:`
21+
- Example: `def_column :attack, type: :float`
22+
- See the column type list below.
23+
24+
### `group_key:`
25+
- Example: `def_column :lv, type: :integer, group_key: true`
26+
- You can also use `group_key :lv`.
27+
28+
### `db_column_name:`
29+
- Use when the DB column name differs.
30+
- Example: `def_column :start_at, type: :time, db_column_name: :start_time`
31+
32+
### `globalize:`
33+
- Adds locale-aware values using `I18n.locale`.
34+
- Example: `def_column :name, globalize: true`
35+
- You can also use `globalize :name`.
36+
- Translation values live in `@_globalized_name` like `{ en: "Storm Edge", ja: "..." }`.
37+
- Not supported on `id` / `enum` / `bitmask` / `sti` / `polymorphic_type`.
38+
- Cannot be used with `group_key`.
39+
40+
## Column types
41+
42+
### id (IdColumn)
43+
**Usage**
44+
```ruby
45+
def_column :id
46+
```
47+
**Behavior**
48+
- Converts to `to_i` on assignment.
49+
- In tests, updates `id_hash` when changed.
50+
51+
### integer
52+
**Usage**
53+
```ruby
54+
def_column :lv, type: :integer
55+
```
56+
**Behavior**
57+
- Converts to `to_i` on assignment (empty string becomes `nil`).
58+
59+
### float
60+
**Usage**
61+
```ruby
62+
def_column :attack, type: :float
63+
```
64+
**Behavior**
65+
- Converts to `to_f` on assignment (empty string becomes `nil`).
66+
67+
### string
68+
**Usage**
69+
```ruby
70+
def_column :name, type: :string
71+
```
72+
**Behavior**
73+
- Converts to `to_s` on assignment.
74+
- Values are cached to reuse identical objects (`object_cache`).
75+
76+
### symbol
77+
**Usage**
78+
```ruby
79+
def_column :kind, type: :symbol
80+
```
81+
**Behavior**
82+
- Converts to `to_s` + `to_sym` on assignment.
83+
- SQL/CSV output uses a string.
84+
85+
### boolean
86+
**Usage**
87+
```ruby
88+
def_column :is_boss, type: :boolean
89+
```
90+
**Behavior**
91+
- Integers use 0/1, strings accept "true" or "1".
92+
- Adds a `name?` predicate.
93+
- SQL/CSV output is 0/1.
94+
95+
### json
96+
**Usage**
97+
```ruby
98+
def_column :info, type: :json
99+
```
100+
**Options**
101+
- `symbolize_names: true` converts JSON keys to symbols.
102+
103+
**Behavior**
104+
- Parses string values with `JSON.parse`.
105+
- SQL/CSV output uses `JSON.generate`.
106+
- Non-string assignments are not transformed by `symbolize_names`.
107+
108+
### time
109+
**Usage**
110+
```ruby
111+
def_column :start_at, type: :time
112+
```
113+
**Options**
114+
- `db_type: :time` outputs `HH:MM:SS` only.
115+
116+
**Behavior**
117+
- Parses strings with `Date._parse` into `Time`.
118+
- Sub-seconds are truncated.
119+
120+
### enum
121+
**Usage**
122+
```ruby
123+
def_column :rarity, enum: { common: 0, rare: 1, epic: 2 }
124+
# or
125+
def_column :rarity
126+
enum :rarity, { common: 0, rare: 1, epic: 2 }
127+
```
128+
**Options**
129+
- `prefix`, `suffix` add a prefix/suffix to predicates.
130+
- `prefix: true` => `rarity_common?`
131+
- `suffix: :rarity` => `common_rarity?`
132+
133+
**Behavior**
134+
- Values are stored as symbols.
135+
- Adds `rarities` and `rarity_before_type_cast`.
136+
- Predicate methods (e.g. `common?`) are generated.
137+
138+
### bitmask
139+
**Usage**
140+
```ruby
141+
def_column :flags, type: :integer
142+
bitmask :flags, as: [:tradeable, :soulbound, :limited]
143+
```
144+
**Behavior**
145+
- Accepts array/symbol/integer and converts to bit integer.
146+
- `flags` returns an array of symbols.
147+
- Adds `flags_value` / `flags_value=` for raw integer bits.
148+
149+
### sti (STI type column)
150+
**Usage**
151+
```ruby
152+
def_column :type, sti: true
153+
```
154+
**Behavior**
155+
- Converts `type` to a string.
156+
- Defines `sti_base_class` and `sti_column`.
157+
- Loader should resolve classes by `type`.
158+
159+
### polymorphic_type
160+
**Usage**
161+
```ruby
162+
def_column :reward_type, polymorphic_type: true
163+
```
164+
**Behavior**
165+
- Used for `belongs_to polymorphic` type columns.
166+
- Stores a class name string and sets `reward_type_class`.
167+
- Empty strings become `nil`.
168+
169+
## Custom column types
170+
Define custom columns by subclassing `SimpleMaster::Master::Column`.
171+
If the class name ends with `Column`, the `type` is auto-registered.
172+
173+
```ruby
174+
class MoneyColumn < SimpleMaster::Master::Column
175+
private
176+
177+
def code_for_conversion
178+
<<-RUBY
179+
value = value&.to_i
180+
RUBY
181+
end
182+
183+
def code_for_sql_value
184+
<<-RUBY
185+
#{name}
186+
RUBY
187+
end
188+
end
189+
190+
class Product < ApplicationMaster
191+
def_column :price, type: :money
192+
end
193+
```
194+
195+
- Ensure the file is loaded before use.
196+
- Override `init` if you need custom methods.
197+
- See [lib/simple_master/master/column.rb](lib/simple_master/master/column.rb).

0 commit comments

Comments
 (0)