Class: Diva::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/diva/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, required: false) ⇒ Field

name

Symbol フィールドの名前

type

Symbol フィールドのタイプ。:int, :string, :bool, :time のほか、Diva::Modelのサブクラスを指定する

required

boolean true なら、この項目を必須とする



16
17
18
19
20
# File 'lib/diva/field.rb', line 16

def initialize(name, type, required: false)
  @name = name.to_sym
  @type = Diva::Type.optional(Diva::Type(type))
  @required = !!required
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/diva/field.rb', line 11

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



11
12
13
# File 'lib/diva/field.rb', line 11

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/diva/field.rb', line 11

def type
  @type
end

Instance Method Details

#dump_for_json(value) ⇒ Object



22
23
24
# File 'lib/diva/field.rb', line 22

def dump_for_json(value)
  type.dump_for_json(value)
end

#inspectObject



45
46
47
# File 'lib/diva/field.rb', line 45

def inspect
  "#<#{self.class}: #{name}(#{type})#{required ? '*' : ''}>"
end

#required?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/diva/field.rb', line 26

def required?
  required
end

#schemaObject



30
31
32
33
34
35
# File 'lib/diva/field.rb', line 30

def schema
  {
    name: @name.to_s,
    constraint: @type.schema
  }
end

#to_sObject



41
42
43
# File 'lib/diva/field.rb', line 41

def to_s
  name.to_s
end

#to_symObject



37
38
39
# File 'lib/diva/field.rb', line 37

def to_sym
  name
end