Class: Typesmith::Definition
- Inherits:
-
Object
- Object
- Typesmith::Definition
- Defined in:
- lib/typesmith/definition.rb
Defined Under Namespace
Classes: Error, UndefinedAttributeError
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
- .properties ⇒ Object
- .property(name, type: nil, optional: false, &block) ⇒ Object
- .to_typescript ⇒ Object
- .typescript_type_name ⇒ Object
Instance Method Summary collapse
- #as_json(*_args) ⇒ Object
-
#initialize(attributes = {}) ⇒ Definition
constructor
A new instance of Definition.
- #properties ⇒ Object
- #to_json(*_args) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Definition
Returns a new instance of Definition.
61 62 63 64 65 |
# File 'lib/typesmith/definition.rb', line 61 def initialize(attributes = {}) @attributes = {} process_attributes(attributes.deep_symbolize_keys) validate_required_attributes end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
55 56 57 |
# File 'lib/typesmith/definition.rb', line 55 def attributes @attributes end |
Class Method Details
.properties ⇒ Object
25 26 27 |
# File 'lib/typesmith/definition.rb', line 25 def properties @properties ||= {} end |
.property(name, type: nil, optional: false, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/typesmith/definition.rb', line 12 def property(name, type: nil, optional: false, &block) properties[name] = case type when Array ArrayProperty.new(name, type.first, optional: optional) when Hash IndexedProperty.new(name, type.keys.first, type.values.first, optional: optional) when nil block ? NestedProperty.new(name, block, optional: optional) : SimpleProperty.new(name, :any, optional: optional) else SimpleProperty.new(name, type, optional: optional) end end |
.to_typescript ⇒ Object
29 30 31 |
# File 'lib/typesmith/definition.rb', line 29 def to_typescript generate_typescript_type(name.demodulize, properties) end |
.typescript_type_name ⇒ Object
33 34 35 |
# File 'lib/typesmith/definition.rb', line 33 def typescript_type_name name.demodulize end |
Instance Method Details
#as_json(*_args) ⇒ Object
71 72 73 |
# File 'lib/typesmith/definition.rb', line 71 def as_json(*_args) @attributes end |
#properties ⇒ Object
57 58 59 |
# File 'lib/typesmith/definition.rb', line 57 def properties self.class.properties end |
#to_json(*_args) ⇒ Object
67 68 69 |
# File 'lib/typesmith/definition.rb', line 67 def to_json(*_args) @attributes end |