Skip to content

Commit

Permalink
Replaced double splat properties with an optional hash
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBoyBarney committed Feb 18, 2025
1 parent 2fa89a0 commit 012e445
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions spec/kdl/builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe KDL::Builder do

it "builds a node with shorthand properties" do
doc = KDL.build do |kdl|
kdl.node "pokemon", pokemon_type: "normal", level: 10_i64
kdl.node "pokemon", properties: {"pokemon_type" => "normal", "level" => 10_i64}
end

doc.to_s.should eq <<-KDL
Expand All @@ -82,11 +82,11 @@ describe KDL::Builder do

it "builds a node with multiple shorthand arguments and properties" do
doc = KDL.build do |kdl|
kdl.node "pokemon", "snorlax", "jigglypuff", pokemon_type: "normal", level: 10_i64
kdl.node "pokemon", "snorlax", "jigglypuff", properties: {"Pokemon type" => "normal", "Level" => 10_i64}
end

doc.to_s.should eq <<-KDL
pokemon snorlax jigglypuff pokemon_type=normal level=10
pokemon snorlax jigglypuff "Pokemon type"=normal Level=10
KDL
end
Expand Down
15 changes: 9 additions & 6 deletions src/kdl/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,30 @@ module KDL
end
end

# Node name only
def node(name : String, *, type : String? = nil, comment : String? = nil)
node(name, type: type, comment: comment) { }
end

# Name and shorthand arguments
def node(name : String, *arguments : KDL::Value::Type, type : String? = nil, comment : String? = nil)
node name, type: type, comment: comment do
arguments.each &->arg(KDL::Value::Type)
end
end

def node(name : String, *, type : String? = nil, comment : String? = nil, **properties : KDL::Value::Type)
# Name and shorthand properties
def node(name : String, *, type : String? = nil, comment : String? = nil, properties : Hash(String, KDL::Value::Type))
node name, type: type, comment: comment do
properties.each &->prop(Symbol, KDL::Value::Type)
properties.each &->prop(String, KDL::Value::Type)
end
end

def node(name : String, *arguments : KDL::Value::Type, type : String? = nil, comment : String? = nil, **properties : KDL::Value::Type)
# Name and shorthand arguments + properties
def node(name : String, *arguments : KDL::Value::Type, type : String? = nil, comment : String? = nil, properties : Hash(String, KDL::Value::Type))
node name, type: type, comment: comment do
arguments.each &->arg(KDL::Value::Type)
properties.each &->prop(Symbol, KDL::Value::Type)
properties.each &->prop(String, KDL::Value::Type)
end
end

Expand All @@ -61,8 +65,7 @@ module KDL
end
end

def prop(key : String | Symbol, value : KDL::Value::Type, *, type : String? = nil, comment : String? = nil)
key = key.to_s
def prop(key : String, value : KDL::Value::Type, *, type : String? = nil, comment : String? = nil)
if node = current_node
node.properties[key] = KDL::Value.new(value, type: type, comment: comment)
else
Expand Down

0 comments on commit 012e445

Please sign in to comment.