Class: HasHelpers::Cards::CardBuilder::FieldBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/models/has_helpers/cards/card_builder/field_builder.rb

Defined Under Namespace

Classes: ValidationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, ref:, key:, name:, data_type:, mapper:, source: nil, url: nil, badge_color: nil, hover_card: nil, hover_card_source: nil, hover_record_source: nil, hover_resource: nil, hover_resource_source: nil, icon: nil, default_visibility:, default_position:, is_required:, custom_label: false, custom_label_key: nil) ⇒ FieldBuilder

Returns a new instance of FieldBuilder.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 30

def initialize(kind:, ref:, key:, name:, data_type:, mapper:, source: nil, url: nil, badge_color: nil,
               hover_card: nil, hover_card_source: nil, hover_record_source: nil,
               hover_resource: nil, hover_resource_source: nil, icon: nil,
               default_visibility:, default_position:, is_required:, custom_label: false, custom_label_key: nil)
  @kind                  = kind # :resource or :custom
  @ref                   = ref
  @key                   = key&.to_sym
  @name                  = name
  @data_type             = normalize_data_type(data_type)
  @mapper                = mapper && mapper.to_s
  @source                = source
  @url                   = url
  @badge_color           = badge_color
  @hover_card            = hover_card&.to_s
  @hover_card_source     = normalize_source(hover_card_source)
  @hover_record_source   = normalize_source(hover_record_source)
  @hover_resource        = hover_resource&.to_s
  @hover_resource_source = normalize_source(hover_resource_source)
  @icon                  = icon&.to_s
  @default_visibility    = !!default_visibility
  @default_position      = default_position
  @is_required           = !!is_required
  @custom_label          = !!custom_label
  @custom_label_key      = custom_label_key&.to_s
end

Instance Attribute Details

#badge_colorObject (readonly)

Returns the value of attribute badge_color.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def badge_color
  @badge_color
end

#custom_labelObject (readonly)

Returns the value of attribute custom_label.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def custom_label
  @custom_label
end

#custom_label_keyObject (readonly)

Returns the value of attribute custom_label_key.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def custom_label_key
  @custom_label_key
end

#data_typeObject (readonly)

Returns the value of attribute data_type.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def data_type
  @data_type
end

#default_positionObject (readonly)

Returns the value of attribute default_position.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def default_position
  @default_position
end

#default_visibilityObject (readonly)

Returns the value of attribute default_visibility.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def default_visibility
  @default_visibility
end

#hover_cardObject (readonly)

Returns the value of attribute hover_card.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def hover_card
  @hover_card
end

#hover_card_sourceObject (readonly)

Returns the value of attribute hover_card_source.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def hover_card_source
  @hover_card_source
end

#hover_record_sourceObject (readonly)

Returns the value of attribute hover_record_source.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def hover_record_source
  @hover_record_source
end

#hover_resourceObject (readonly)

Returns the value of attribute hover_resource.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def hover_resource
  @hover_resource
end

#hover_resource_sourceObject (readonly)

Returns the value of attribute hover_resource_source.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def hover_resource_source
  @hover_resource_source
end

#iconObject (readonly)

Returns the value of attribute icon.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def icon
  @icon
end

#is_requiredObject (readonly)

Returns the value of attribute is_required.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def is_required
  @is_required
end

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def key
  @key
end

#kindObject (readonly)

Returns the value of attribute kind.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def kind
  @kind
end

#mapperObject (readonly)

Returns the value of attribute mapper.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def mapper
  @mapper
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def name
  @name
end

#refObject (readonly)

Returns the value of attribute ref.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def ref
  @ref
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def source
  @source
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 9

def url
  @url
end

Instance Method Details

#as_jsonObject



113
114
115
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 113

def as_json(*)
  to_h
end

#to_hObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 73

def to_h
  base = {
    kind:                  kind,
    data_type:             data_type,
    default_visibility:    default_visibility,
    default_position:      default_position,
    is_required:           is_required,
    source:                source,
    url:                   url,
    badge_color:           badge_color,
    hover_card:            hover_card,
    hover_card_source:     hover_card_source,
    hover_record_source:   hover_record_source,
    hover_resource:        hover_resource,
    hover_resource_source: hover_resource_source,
    icon:                  icon,
    custom_label:          custom_label,
    custom_label_key:      custom_label_key
  }

  case kind
  when :resource
    base.merge(
      ref: ref,
      name: name
    )
  when :custom
    base.merge(
      key:    key,
      name:   name,
      mapper: mapper
    )
  when :virtual
    base.merge(
      ref:  ref,
      name: name
    )
  end
end

#validate!Object

Raises:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/has_helpers/cards/card_builder/field_builder.rb', line 56

def validate!
  unless %i[resource custom virtual].include?(kind)
    raise ValidationError, "Invalid field kind: #{kind.inspect}"
  end

  raise ValidationError, "data_type is required" if data_type.nil?

  case kind
  when :resource
    raise ValidationError, "resource field must have ref" if ref.nil?
  when :custom
    raise ValidationError, "custom field must have key" if key.nil?
  when :virtual
    raise ValidationError, "virtual field must have ref" if ref.nil?
  end
end