Class: DdbRuby::FifthEdition::Choices

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



1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
# File 'lib/ddb_ruby/fifth_edition/structs.rb', line 1132

def self.from_dynamic!(d)
  d = Types::Hash[d]
  new(
    race:                    d.fetch("race").map { |x| ChoicesBackground.from_dynamic!(x) },
    choices_class:           d.fetch("class").map { |x| ChoicesBackground.from_dynamic!(x) },
    background:              d.fetch("background").map { |x| ChoicesBackground.from_dynamic!(x) },
    item:                    d.fetch("item"),
    feat:                    d.fetch("feat").map { |x| ChoicesBackground.from_dynamic!(x) },
    choice_definitions:      d.fetch("choiceDefinitions").map { |x| ChoiceDefinition.from_dynamic!(x) },
    definition_key_name_map: DefinitionKeyNameMap.from_dynamic!(d.fetch("definitionKeyNameMap")),
  )
end

.from_json!(json) ⇒ Object



1145
1146
1147
# File 'lib/ddb_ruby/fifth_edition/structs.rb', line 1145

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

Instance Method Details

#to_dynamicObject



1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/ddb_ruby/fifth_edition/structs.rb', line 1149

def to_dynamic
  {
    "race"                 => race.map { |x| x.to_dynamic },
    "class"                => choices_class.map { |x| x.to_dynamic },
    "background"           => background.map { |x| x.to_dynamic },
    "item"                 => item,
    "feat"                 => feat.map { |x| x.to_dynamic },
    "choiceDefinitions"    => choice_definitions.map { |x| x.to_dynamic },
    "definitionKeyNameMap" => definition_key_name_map.to_dynamic,
  }
end

#to_json(options = nil) ⇒ Object



1161
1162
1163
# File 'lib/ddb_ruby/fifth_edition/structs.rb', line 1161

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