Class: Typesmith::NestedProperty

Inherits:
BaseProperty show all
Defined in:
lib/typesmith/nested_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, block, optional: false) ⇒ NestedProperty

Returns a new instance of NestedProperty.



7
8
9
10
# File 'lib/typesmith/nested_property.rb', line 7

def initialize(name, block, optional: false)
  super(name, optional: optional)
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
end

Instance Method Details

#process_value(value) ⇒ Object



24
25
26
27
28
# File 'lib/typesmith/nested_property.rb', line 24

def process_value(value)
  nested_class = Class.new(Definition)
  nested_class.class_eval(&block)
  nested_class.new(value).attributes
end

#to_typescriptObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/typesmith/nested_property.rb', line 12

def to_typescript
  nested_class = Class.new(Definition)
  nested_class.class_eval(&block)
  [
    "#{camelized_name}#{optional_suffix}: {",
    nested_class.send(:generate_properties, nested_class.properties).split("\n").map do |line|
      "  #{line}"
    end.join("\n"),
    "};"
  ].join("\n")
end