Class: GoonModelGen::Converter::Loader
- Inherits:
-
Object
- Object
- GoonModelGen::Converter::Loader
- Defined in:
- lib/goon_model_gen/converter/loader.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#initialize(config) ⇒ Loader
constructor
A new instance of Loader.
- #load_conv_defs(f, conv_class, hash) ⇒ Object
- #load_mappings(hash, conv_class) ⇒ Object
- #load_model_for_conv(obj) ⇒ Object
- #process(path) ⇒ Object
Constructor Details
#initialize(config) ⇒ Loader
Returns a new instance of Loader.
19 20 21 |
# File 'lib/goon_model_gen/converter/loader.rb', line 19 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
17 18 19 |
# File 'lib/goon_model_gen/converter/loader.rb', line 17 def config @config end |
Instance Method Details
#load_conv_defs(f, conv_class, hash) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/goon_model_gen/converter/loader.rb', line 37 def load_conv_defs(f, conv_class, hash) hash.map do |(name, definition)| model = load_model_for_conv(definition['model']) gen_type = TypeRef.new(name, File.join(config.goa_gen_package_path, definition['package_name'] || f.basename)) mappings = load_mappings(definition['mappings'], conv_class) conv_class.new(name, model, gen_type, mappings).tap do |conv| conv.file = f end end end |
#load_mappings(hash, conv_class) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/goon_model_gen/converter/loader.rb', line 67 def load_mappings(hash, conv_class) hash.map do |(name, props)| props ||= {} props = {'arg' => props} if props.is_a?(String) args = props['args'] || [props['arg'] || name] func, requires_context, returns_error = *conv_class.load_func(props) if func.nil? && (args.length > 1) raise "Invalid argument length: #{args.length} for #{name}: #{args.inspect}" end Mapping.new(name, args, func, requires_context, returns_error).tap do |m| m.allow_zero = props['allow_zero'] m.resolve_package_path(config) end end end |
#load_model_for_conv(obj) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/goon_model_gen/converter/loader.rb', line 48 def load_model_for_conv(obj) pkg_name, pkg_path, pkg_base_path = nil, nil, nil slice_with_ptr = nil case obj when Hash pkg_name, pkg_path = obj['name'], obj['package_path'] slice_with_ptr = obj['slice_with_ptr'] when String pkg_name = obj pkg_base_path = config.model_package_path else raise "Unsupported model type for converter definition: #{obj.inspect}" end TypeRef.new(pkg_name, pkg_path).tap do |t| t.package_base_path = pkg_base_path t.slice_with_ptr = slice_with_ptr end end |
#process(path) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/goon_model_gen/converter/loader.rb', line 23 def process(path) erb = ERB.new(::File.read(path), nil, "-") erb.filename = path txt = erb.result raw = YAML.load(txt) converter_dir = raw['converter_dir'] || ::File.basename(path, '.*') converter_package_path = raw['converter_package_path'] || File.join(config.converter_package_path, converter_dir) ConvFile.new(path, converter_package_path).tap do |f| f.payload_convs = load_conv_defs(f, PayloadConv, raw['payloads'] || {}) f.result_convs = load_conv_defs(f, ResultConv, raw['results'] || {}) end end |