Class: Diva::Field
- Inherits:
-
Object
- Object
- Diva::Field
- Defined in:
- lib/diva/field.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #dump_for_json(value) ⇒ Object
-
#initialize(name, type, required: false) ⇒ Field
constructor
- name
-
Symbol フィールドの名前 [type] Symbol フィールドのタイプ。:int, :string, :bool, :time のほか、Diva::Modelのサブクラスを指定する [required] boolean true なら、この項目を必須とする.
- #inspect ⇒ Object
- #required? ⇒ Boolean
- #schema ⇒ Object
- #to_s ⇒ Object
- #to_sym ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/diva/field.rb', line 11 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
11 12 13 |
# File 'lib/diva/field.rb', line 11 def required @required end |
#type ⇒ Object (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 |
#inspect ⇒ Object
45 46 47 |
# File 'lib/diva/field.rb', line 45 def inspect "#<#{self.class}: #{name}(#{type})#{required ? '*' : ''}>" end |
#required? ⇒ Boolean
26 27 28 |
# File 'lib/diva/field.rb', line 26 def required? required end |
#schema ⇒ Object
30 31 32 33 34 35 |
# File 'lib/diva/field.rb', line 30 def schema { name: @name.to_s, constraint: @type.schema } end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/diva/field.rb', line 41 def to_s name.to_s end |
#to_sym ⇒ Object
37 38 39 |
# File 'lib/diva/field.rb', line 37 def to_sym name end |