Class: JSON2Ruby::CLI
- Inherits:
-
Object
- Object
- JSON2Ruby::CLI
- Defined in:
- lib/json2ruby/cli.rb
Overview
The CLI (Command Line Interface) functionality class for the json2ruby executable
Class Method Summary collapse
-
.display_entity(hsh, ent) ⇒ Object
Display the Entity supplied in ent with the supplied hash value hsh to STDOUT.
-
.ensure_output_dir(options) ⇒ Object
Create the output directory with the options if it does not exist.
-
.get_cli_options ⇒ Object
Process ARGV for command line switches and return the options hash.
-
.parse_files(options) ⇒ Object
Parse all JSON files in ARGV and build the Entity cache, using the supplied options Hash.
-
.run ⇒ Object
Run the json2ruby command, using arguments in ARGV.
-
.write_files(rootclasses, writer, options) ⇒ Object
Write out all types in the Entity cache, except primitives and those contained in the rootclasses array, to the provided writer with the supplied options Hash.
Class Method Details
.display_entity(hsh, ent) ⇒ Object
Display the Entity supplied in ent with the supplied hash value hsh to STDOUT
181 182 183 184 185 186 187 188 |
# File 'lib/json2ruby/cli.rb', line 181 def self.display_entity(hsh, ent) puts "- #{ent.name} (#{ent.class.short_name} - #{hsh})" if ent.is_a?(Entity) ent.attributes.each { |ak,av| puts " #{ak}: #{av.name}" } elsif ent.is_a?(Collection) puts " (Types): #{ent.ruby_types.map { |h,ent| ent.name }.join(',')}" end end |
.ensure_output_dir(options) ⇒ Object
Create the output directory with the options if it does not exist.
32 33 34 35 36 37 38 |
# File 'lib/json2ruby/cli.rb', line 32 def self.ensure_output_dir() puts "Output Directory: #{options[:outputdir]}" if [:verbose] unless Dir.exists?([:outputdir]) puts "Creating Output Directory..." if [:verbose] Dir.mkdir([:outputdir]) end end |
.get_cli_options ⇒ Object
Process ARGV for command line switches and return the options hash.
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/json2ruby/cli.rb', line 41 def self. = {} OptionParser.new do |opts| opts. = "Usage: #{$0} [options] <file.json> [<file.json>....]" opts.on("-o", "--outputdir OUTPUTDIR", "Output directory") do |v| [:outputdir] = v end opts.on("-n", "--namespace MODULENAME", "Module namespace path") do |v| [:namespace] = v end opts.on("-s", "--superclass SUPERCLASS", "Class ancestor") do |v| [:superclass_name] = v end opts.on("-r", "--require REQUIRE", "Require module in file") do |v| [:require] ||= [] [:require] << v end opts.on("-i", "--include INCLUDE", "Include Class/Module in file") do |v| [:include] ||= [] [:include] << v end opts.on("-e", "--extend EXTEND", "Extend from Class/Module in file") do |v| [:extend] ||= [] [:extend] << v end opts.on("-M", "--modules", "Create Modules, not classes") do |v| [:modules] = true end opts.on("-a", "--attributemethod METHODNAME", "Use attribute method instead of attr_accessor") do |v| [:attributemethod] = v end opts.on("-c", "--collectionmethod METHODNAME", "Use collection method instead of attr_accessor") do |v| [:collectionmethod] = v end opts.on("-t", "--types", "Include type in attribute definition call") do |v| [:includetypes] = true end opts.on("-b", "--baseless", "Don't generate classes/modules for the root JSON in each file") do |v| [:baseless] = true end opts.on("-f", "--forceoverwrite", "Overwrite Existing files") do |v| [:forceoverwrite] = v end opts.on("-N", "--forcenumeric", "Use Numeric instead of Integer/Float") do |v| [:forcenumeric] = v end opts.on("-v", "--verbose", "Verbose") do |v| [:verbose] = v end end.parse! # Defaults [:outputdir] ||= File.("./classes") [:namespace] ||= "" [:attributemethod] ||= "attr_accessor" [:collectionmethod] ||= "attr_accessor" [:includetypes] ||= false [:baseless] ||= false [:forceoverwrite] ||= false [:verbose] ||= false [:modulenames] = [:namespace].split("::") end |
.parse_files(options) ⇒ Object
Parse all JSON files in ARGV and build the Entity cache, using the supplied options Hash.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/json2ruby/cli.rb', line 122 def self.parse_files() # Reset the object cache Entity.reset_parse # Load and parse each JSON file puts "Parsing Files..." if [:verbose] rootclasses = [] ARGV.each do |filename| filename = File.(filename) puts "Processing: #{filename}" if [:verbose] file = File.read(filename) data_hash = JSON.parse(file) rootclasses << Entity.parse_from(File.basename(filename,'.*'), data_hash, ) end rootclasses end |
.run ⇒ Object
Run the json2ruby command, using arguments in ARGV.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/json2ruby/cli.rb', line 10 def self.run puts "json2ruby v#{VERSION}\n" # Do the cmdline options = # Ensure Output Directory [:outputdir] = File.([:outputdir], File.dirname(__FILE__)) ensure_output_dir() # Parse Files rootclasses = parse_files() # Write out Ruby (see what I'm doing here?) writer = JSON2Ruby::RubyWriter # Write Output write_files(rootclasses, writer, ) end |
.write_files(rootclasses, writer, options) ⇒ Object
Write out all types in the Entity cache, except primitives and those contained in the rootclasses array, to the provided writer with the supplied options Hash.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/json2ruby/cli.rb', line 146 def self.write_files(rootclasses, writer, ) files = 0 Entity.entities.each do |k,v| next if [:baseless] and rootclasses.include?(v) display_entity(k,v) if [:verbose] && !v.is_a?(Primitive) if v.is_a?(Entity) indent = 0 out = "" [:modulenames].each do |v| out += (' '*indent)+"module #{v}\r\n" indent += 2 end out += writer.to_code(v, indent,) while indent>0 indent -= 2 out += (' '*indent)+"end\r\n" end filename = [:outputdir]+"/#{v.name}.rb" if File.exists?(filename) && ![:forceoverwrite] $stderr.puts "File #{filename} exists. Use -f to overwrite." else File.write(filename, out) files += 1 end end end # Done puts "Done, Generated #{files} file#{files==1 ? '' : 's'}" end |