Class: DdbRuby::FifthEdition::CharacterClass

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/ddb_ruby/fifth_edition/structs.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_dynamic!(d) ⇒ Object



2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
# File 'lib/ddb_ruby/fifth_edition/structs.rb', line 2466

def self.from_dynamic!(d)
  d = Types::Hash[d]
  new(
    id:                     d.fetch("id"),
    entity_type_id:         d.fetch("entityTypeId"),
    level:                  d.fetch("level"),
    is_starting_class:      d.fetch("isStartingClass"),
    hit_dice_used:          d.fetch("hitDiceUsed"),
    definition_id:          d.fetch("definitionId"),
    subclass_definition_id: d.fetch("subclassDefinitionId"),
    definition:             SubclassDefinitionClass.from_dynamic!(d.fetch("definition")),
    subclass_definition:    d.fetch("subclassDefinition") ? SubclassDefinitionClass.from_dynamic!(d.fetch("subclassDefinition")) : nil,
    class_features:         d.fetch("classFeatures").map { |x| ClassClassFeature.from_dynamic!(x) },
  )
end

.from_json!(json) ⇒ Object



2482
2483
2484
# File 'lib/ddb_ruby/fifth_edition/structs.rb', line 2482

def self.from_json!(json)
  from_dynamic!(JSON.parse(json))
end

Instance Method Details

#to_dynamicObject



2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
# File 'lib/ddb_ruby/fifth_edition/structs.rb', line 2486

def to_dynamic
  {
    "id"                   => id,
    "entityTypeId"         => entity_type_id,
    "level"                => level,
    "isStartingClass"      => is_starting_class,
    "hitDiceUsed"          => hit_dice_used,
    "definitionId"         => definition_id,
    "subclassDefinitionId" => subclass_definition_id,
    "definition"           => definition.to_dynamic,
    "subclassDefinition"   => subclass_definition&.to_dynamic,
    "classFeatures"        => class_features.map { |x| x.to_dynamic },
  }
end

#to_json(options = nil) ⇒ Object



2501
2502
2503
# File 'lib/ddb_ruby/fifth_edition/structs.rb', line 2501

def to_json(options = nil)
  JSON.generate(to_dynamic, options)
end