Skip to content

taichi-ishitani/rupkl

Repository files navigation

Gem Version CI Maintainability codecov

RuPkl

A Pkl parser for Ruby.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add rupkl

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install rupkl

Usage

You can use the methods below to load a Pkl code into a Ruby structure:

  • RuPkl.load
    • Load the given Pkl code into a Ruby structure
  • RuPkl.load_file
    • Load a Pkl code read from the given file path into a Ruby structure
require 'rupkl'

pkl = <<~'PKL'
  // From:
  // https://pkl-lang.org/main/current/language-tutorial/01_basic_config.html
  name = "Pkl: Configure your Systems in New Ways"
  attendants = 100
  isInteractive = true
  amountLearned = 13.37
PKL

RuPkl.load(pkl)
# =>
# {:name=>"Pkl:Configure your Systems in New Ways",
#  :attendants=>100,
#  :isInteractive=>true,
#  :amountLearned=>13.37}

File.open('sample.pkl', 'w') do |f|
  f.write(<<~'PKL')
    // From:
    // https://pkl-lang.org/main/current/language-tutorial/01_basic_config.html
    bird {
      name = "Common wood pigeon"
      diet = "Seeds"
      taxonomy {
        species = "Columba palumbus"
      }
    }

    exampleObjectWithJustIntElements {
      100
      42
    }

    exampleObjectWithMixedElements {
      "Bird Breeder Conference"
      (2000 + 23)
      exampleObjectWithJustIntElements
    }

    pigeonShelter {
      ["bird"] {
        name = "Common wood pigeon"
        diet = "Seeds"
        taxonomy {
          species = "Columba palumbus"
        }
      }
      ["address"] = "355 Bird St."
    }

    birdCount {
      [pigeonShelter] = 42
    }
  PKL
end

RuPkl.load_file('sample.pkl')
# =>
# {:bird=>{:name=>"Common wood pigeon", :diet=>"Seeds", :taxonomy=>{:species=>"Columba palumbus"}},
#  :exampleObjectWithJustIntElements=>[100, 42],
#  :exampleObjectWithMixedElements=>["Bird Breeder Conference", 2023, [100, 42]],
#  :pigeonShelter=>
#   {"bird"=>{:name=>"Common wood pigeon", :diet=>"Seeds", :taxonomy=>{:species=>"Columba palumbus"}},
#    "address"=>"355 Bird St."},
#  :birdCount=>
#   {{"bird"=>{:name=>"Common wood pigeon", :diet=>"Seeds", :taxonomy=>{:species=>"Columba palumbus"}},
#     "address"=>"355 Bird St."}=>42}}

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/taichi-ishitani/rupkl.

Notice

Pkl code snippets used for RSpec examples are originaly from:

Copyright & License

Copyright © 2024 Taichi Ishitani. RuPkl is licensed under the terms of the MIT License, see LICENSE.txt for further details.

Code of Conduct

Everyone interacting in the RuPkl project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.