Class: KwalifyToJsonSchema::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/kwalify_to_json_schema/options.rb

Overview

The possible options for the conversion and the associated accessors

Constant Summary collapse

DECLARATION =

Converter options: | Name | Type | Default value| Description | |———————–|——–|————–|——————————————————————————————| | :id | string | nil | The JSON schema identifier | | :title | string | nil | The JSON schema title | | :description | string | nil | The JSON schema description. If not given the Kwalify description will be used if present| | :issues_to_description| boolean| false | To append the issues to the JSON schema description | | :issues_to_stderr | boolean| false | To write the issues to standard error output | | :custom_processing | object | nil | To customize the conversion | | :schema_version | string | “draft-04” | JSON schema version. Changing this value only change the value of $schema field | | :verbose | boolean| false | To be verbose when converting | –

%q(
ID                    # The JSON schema identifier [string] (nil)
TITLE                 # The JSON schema title [string] (nil)
DESCRIPTION           # The JSON schema description. If not given the Kwalify description will be used if present [string] (nil)
ISSUES_TO_DESCRIPTION # To append the issues to the JSON schema description [boolean] (false)
ISSUES_TO_STDERR      # To write the issues to standard error output [boolean] (false)
CUSTOM_PROCESSING     # To customize the conversion [object] (nil)
SCHEMA_VERSION        # JSON schema version. Changing this value only change the value of $schema field[string] ("draft-04")
VERBOSE               # To be verbose when converting [boolean] (false)
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Options

Returns a new instance of Options.



30
31
32
# File 'lib/kwalify_to_json_schema/options.rb', line 30

def initialize(options)
  @options_hash = options.is_a?(Options) ? options.options_hash : options
end

Instance Attribute Details

#options_hashObject (readonly)

The options as Hash



28
29
30
# File 'lib/kwalify_to_json_schema/options.rb', line 28

def options_hash
  @options_hash
end

Class Method Details

.cli_option(name) ⇒ Object

Get description for option name



88
89
90
91
# File 'lib/kwalify_to_json_schema/options.rb', line 88

def self.cli_option(name)
  o = parse_hash[name]
  [o[:name], :type => o[:type].to_sym, :default => o[:default_value], :desc => o[:description]]
end

.parseObject

Parse options declaration text and give an array of Hash



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kwalify_to_json_schema/options.rb', line 39

def self.parse
  DECLARATION.lines.map { |l|
    next nil if l.strip.empty?

    # Parse line
    const_name, comment = l.split("#", 2).map(&:strip)
    name = const_name.downcase.to_s
    description = comment.split("[").first.strip
    # Get type and default value
    m = comment.match(/\[(.+)\].*\((.+)\)/)
    type, default_value = m.captures
    default_value = eval(default_value)

    # Create read accessor
    attr_reader_name = "#{name}#{type == "boolean" ? "?" : ""}"

    # Array entry as Hash for the option
    {
      const_name: const_name,
      const_name_full: "#{Options.name}::#{const_name}",
      name: name.to_sym,
      description: description,
      type: type,
      default_value: default_value,
      attr_reader_name: attr_reader_name,
    }
  }.compact
end

.parse_hashObject

Same as :parse but give a Hash with the name as key



69
70
71
# File 'lib/kwalify_to_json_schema/options.rb', line 69

def self.parse_hash
  parse.map { |e| [e[:name], e] }.to_h
end

.setupObject

Setup the constants and methods for the options Example: ID will lead to get ID constant and :id method



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/kwalify_to_json_schema/options.rb', line 75

def self.setup
  parse.each { |o|
    # Create constant
    const_set o[:const_name], o[:name]

    # Create read accessor
    define_method(o[:attr_reader_name]) {
      options_hash[o[:name]] || o[:default_value]
    }
  }
end

Instance Method Details

#to_sObject



34
35
36
# File 'lib/kwalify_to_json_schema/options.rb', line 34

def to_s
  YAML.dump("Options" => options_hash)
end