Class: JSON2Ruby::CLI
- Inherits:
-
Object
- Object
- JSON2Ruby::CLI
- Defined in:
- lib/json2ruby/cli.rb
Class Method Summary collapse
- .display_entity(hsh, ent) ⇒ Object
- .ensure_output_dir(options) ⇒ Object
- .get_cli_options ⇒ Object
- .parse_files(options) ⇒ Object
- .run ⇒ Object
- .write_files(rootclasses, writer, options) ⇒ Object
Class Method Details
.display_entity(hsh, ent) ⇒ Object
174 175 176 177 178 179 180 181 |
# File 'lib/json2ruby/cli.rb', line 174 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
29 30 31 32 33 34 35 |
# File 'lib/json2ruby/cli.rb', line 29 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
37 38 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 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 |
# File 'lib/json2ruby/cli.rb', line 37 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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/json2ruby/cli.rb', line 118 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
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/json2ruby/cli.rb', line 8 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
140 141 142 143 144 145 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 |
# File 'lib/json2ruby/cli.rb', line 140 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 |