Class: Typesmith::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/typesmith/definition.rb

Defined Under Namespace

Classes: Error, UndefinedAttributeError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#attributesObject (readonly)

Returns the value of attribute attributes.



55
56
57
# File 'lib/typesmith/definition.rb', line 55

def attributes
  @attributes
end

Class Method Details

.propertiesObject



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_typescriptObject



29
30
31
# File 'lib/typesmith/definition.rb', line 29

def to_typescript
  generate_typescript_type(name.demodulize, properties)
end

.typescript_type_nameObject



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

#propertiesObject



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