Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheme converter #4

Open
SleeplessByte opened this issue Oct 5, 2018 · 0 comments
Open

Scheme converter #4

SleeplessByte opened this issue Oct 5, 2018 · 0 comments
Labels
💎 feature New feature 🔬 experimental Speculative functionality or implementation

Comments

@SleeplessByte
Copy link
Owner

# rules.rb

def to_h(depth = 0)
  return { error: 'Depth limit reached' } if depth > 2_500
  {
      n: keys.length,
      optional: optional_keys.length,
      attributes: { '*' => to_h_format_attribute(depth, '*', default) }.tap do |hash|
        hash.merge! Hash[*keys.map { |key| to_h_format_attribute(depth, key) }]
      end
  }
end

def to_h_format_attribute(depth, key, value = self[key])
  value_to_h = value.is_a?(Scheme) || value.is_a?(Rules) ? value.to_h(depth + 1) : value.to_h
  [key, { optional: optional_keys.include?(key), value: value_to_h }]
end
# scheme.rb
def to_h(depth = 0)
    { rules: rules.to_h(depth + 1) }
end

Instead of the above, probably want a converter with plugin / adapter system, and change #inspect to work with that. It should then iterate and traverse the rules and use that to convert it into something else, such as a string, or a JSON scheme, or whatever.

@SleeplessByte SleeplessByte added 🔬 experimental Speculative functionality or implementation 💎 feature New feature labels Oct 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💎 feature New feature 🔬 experimental Speculative functionality or implementation
Projects
None yet
Development

No branches or pull requests

1 participant