Socket
Book a DemoInstallSign in
Socket

dot_options

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dot_options

0.1.2
bundlerRubygems
Version published
Maintainers
1
Created
Source

DotOptions - Options object with dot-notation access

Convert any hash to a an object with deep read/write dot-notation access.

Gem Version Build Status Maintainability

Install

$ gem install dot_options

Usage

# Usage
require 'dot_options'

# Initialize a DotOptions object with a hash:
opts = {
  debug:  true,
  output: { color: true },
  skin:   { background: { color: :black, texture: 'Stripes' } },
}
options = DotOptions.new opts

# Read any option with dot-notation:
p options.skin.background.color
#=> :black

# Update any option leaf with dot-notation:
options.skin.background.color = :black

# Print a flat view of all options
puts options
#=> debug = true
#=> output.color = true
#=> skin.background.color = :black
#=> skin.background.texture = "Stripes"

# ... or a compact inspection string for any branch
p options.skin
#=> <background: <color: :black, texture: "Stripes">>

# Access is also possible using []
p options.skin[:background].color
p options.skin[:background][:color]
#=> :black
#=> :black

Subclassing

Subclassing DotOptions is a useful way to have an object with default options.

# Subclassing
require 'dot_options'

class Skin < DotOptions
  def initialize(options = nil)
    super defaults.merge(options || {})
  end

  def defaults
    {
      color: :black,
      border: { color: :red, width: 3 },
      background: { color: :lime, texture: 'Stripes' },
    }
  end
end

# Get an object with the default options
skin = Skin.new 
p skin.background.color
#=> :lime

# Get an object and override some root options using a hash
skin = Skin.new color: :blue
p skin.color
#=> :blue

# Get an object and update some deep options using a block
skin = Skin.new { border.color = :yellow }
puts skin.border
#=> color = :yellow
#=> width = 3

# The initialization block receives the object itself which can be useful
# in some cases
skin = Skin.new do |skin|
  skin.color = :cyan
  skin.border.width = 10
end
p skin.color
p skin.border
#=> :cyan
#=> <color: :red, width: 10>

Contributing / Support

If you experience any issue, have a question or a suggestion, or if you wish to contribute, feel free to open an issue.

FAQs

Package last updated on 13 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.