1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
# -*- coding: utf-8 -*-
require 'diva/type'
=begin rdoc
Modelのキーの情報を格納する。
キーひとつにつき1つのインスタンスが作られる。
=end
module Diva
class Field
attr_reader :name, :type, :required
# [name] Symbol フィールドの名前
# [type] Symbol フィールドのタイプ。:int, :string, :bool, :time のほか、Diva::Modelのサブクラスを指定する
# [required] boolean _true_ なら、この項目を必須とする
def initialize(name, type, required: false)
@name = name.to_sym
@type = Diva::Type.optional(Diva::Type(type))
@required = !!required
end
def dump_for_json(value)
type.dump_for_json(value)
end
def required?
required
end
def schema
{
name: @name.to_s,
constraint: @type.schema
}
end
def to_sym
name
end
def to_s
name.to_s
end
def inspect
"#<#{self.class}: #{name}(#{type})#{required ? '*' : ''}>"
end
end
end
|