Class: LcpRuby::Types::TypeDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/types/type_definition.rb

Constant Summary collapse

BASE_TYPE_COLUMN_MAP =
{
  "enum" => :string,
  "rich_text" => :text,
  "uuid" => :string,
  "file" => :string
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ TypeDefinition

Returns a new instance of TypeDefinition.



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
# File 'lib/lcp_ruby/types/type_definition.rb', line 16

def initialize(attrs = {})
  @name = attrs[:name].to_s
  @base_type = attrs[:base_type].to_s
  @transforms = Array(attrs[:transforms]).map(&:to_s)
  @validations = Array(attrs[:validations]).map { |v| normalize_validation(v) }
  @input_type = attrs[:input_type]&.to_s
  @renderer = attrs[:renderer]&.to_s
  @column_options = attrs[:column_options] || {}
  @html_input_attrs = attrs[:html_input_attrs] || {}
  @column_type = resolve_column_type(attrs[:column_type])
  @default_index_visible = attrs[:default_index_visible]
  @null_false_validation = if attrs.key?(:null_false_validation)
    Array(attrs[:null_false_validation]).map { |v| normalize_validation(v) }
  end
  # Preserve nil-vs-[] distinction for reserved_clashes (parallel with
  # null_false_validation): nil → walk recurses to base_type;
  # [] → walk stops, returns []. Same semantics as Decision 12 for
  # null_false_validation, applied uniformly to enable inheritance
  # (Decision 11) when the attribute is absent on a custom type.
  @reserved_clashes = if attrs.key?(:reserved_clashes)
    Array(attrs[:reserved_clashes]).map(&:to_s)
  end

  validate!
end

Instance Attribute Details

#base_typeObject (readonly)

Returns the value of attribute base_type.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def base_type
  @base_type
end

#column_optionsObject (readonly)

Returns the value of attribute column_options.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def column_options
  @column_options
end

#column_typeObject (readonly)

Returns the value of attribute column_type.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def column_type
  @column_type
end

#default_index_visibleObject (readonly)

Returns the value of attribute default_index_visible.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def default_index_visible
  @default_index_visible
end

#html_input_attrsObject (readonly)

Returns the value of attribute html_input_attrs.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def html_input_attrs
  @html_input_attrs
end

#input_typeObject (readonly)

Returns the value of attribute input_type.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def input_type
  @input_type
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def name
  @name
end

#null_false_validationObject (readonly)

Returns the value of attribute null_false_validation.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def null_false_validation
  @null_false_validation
end

#rendererObject (readonly)

Returns the value of attribute renderer.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def renderer
  @renderer
end

#reserved_clashesObject (readonly)

Returns the value of attribute reserved_clashes.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def reserved_clashes
  @reserved_clashes
end

#transformsObject (readonly)

Returns the value of attribute transforms.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def transforms
  @transforms
end

#validationsObject (readonly)

Returns the value of attribute validations.



11
12
13
# File 'lib/lcp_ruby/types/type_definition.rb', line 11

def validations
  @validations
end

Class Method Details

.from_hash(hash) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lcp_ruby/types/type_definition.rb', line 42

def self.from_hash(hash)
  hash = hash.transform_keys(&:to_s) if hash.is_a?(Hash)
  attrs = {
    name: hash["name"],
    base_type: hash["base_type"],
    transforms: hash["transforms"],
    validations: hash["validations"],
    input_type: hash["input_type"],
    renderer: hash["renderer"],
    column_options: symbolize_keys(hash["column_options"]),
    html_input_attrs: symbolize_keys(hash["html_input_attrs"]),
    column_type: hash["column_type"],
    default_index_visible: hash["default_index_visible"]
  }
  # Conditionally pass null_false_validation and reserved_clashes only
  # when the YAML/hash has them — preserves the nil-vs-[] distinction in
  # the initializer (Decision 12 for null_false_validation; Decision 11
  # walk inheritance for reserved_clashes when absent).
  attrs[:null_false_validation] = hash["null_false_validation"] if hash.key?("null_false_validation")
  attrs[:reserved_clashes] = hash["reserved_clashes"] if hash.key?("reserved_clashes")
  new(**attrs)
end

.symbolize_keys(hash) ⇒ Object



106
107
108
109
# File 'lib/lcp_ruby/types/type_definition.rb', line 106

def self.symbolize_keys(hash)
  return {} unless hash.is_a?(Hash)
  hash.transform_keys(&:to_sym)
end