Skip to content

Scheme converter #4

Open
Open
@SleeplessByte

Description

@SleeplessByte
# 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions