Class: Typesmith::IndexedProperty

Inherits:
BaseProperty show all
Defined in:
lib/typesmith/indexed_property.rb

Constant Summary

Constants inherited from BaseProperty

BaseProperty::PRIMITIVE_TYPES

Instance Attribute Summary collapse

Attributes inherited from BaseProperty

#name, #optional

Instance Method Summary collapse

Constructor Details

#initialize(name, key_type, value_type, optional: false) ⇒ IndexedProperty

Returns a new instance of IndexedProperty.



7
8
9
10
11
12
13
# File 'lib/typesmith/indexed_property.rb', line 7

def initialize(name, key_type, value_type, optional: false)
  super(name, optional: optional)
  @key_type = key_type
  @value_type = value_type
  self.class.validate_type(key_type)
  self.class.validate_type(value_type)
end

Instance Attribute Details

#key_typeObject (readonly)

Returns the value of attribute key_type.



5
6
7
# File 'lib/typesmith/indexed_property.rb', line 5

def key_type
  @key_type
end

#value_typeObject (readonly)

Returns the value of attribute value_type.



5
6
7
# File 'lib/typesmith/indexed_property.rb', line 5

def value_type
  @value_type
end

Instance Method Details

#process_value(value) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/typesmith/indexed_property.rb', line 19

def process_value(value)
  value.transform_values do |v|
    if value_type.is_a?(Array)
      v.map { |item| SimpleProperty.new(nil, value_type.first).process_value(item) }
    else
      SimpleProperty.new(nil, value_type).process_value(v)
    end
  end
end

#to_typescriptObject



15
16
17
# File 'lib/typesmith/indexed_property.rb', line 15

def to_typescript
  "#{camelized_name}#{optional_suffix}: { [key: #{get_type_string(key_type)}]: #{get_type_string(value_type)} };"
end