Skip to content

Commit

Permalink
Merge b6266dd into 46fb40c
Browse files Browse the repository at this point in the history
  • Loading branch information
notEthan committed Jul 25, 2018
2 parents 46fb40c + b6266dd commit e3e84ed
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions lib/scorpio/schema_instance_base/to_rb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,76 @@ module Scorpio
# base class for representing an instance of an instance described by a schema
class SchemaInstanceBase
class << self
def class_comment
lines = []

description = schema &&
schema['description'].respond_to?(:to_str) &&
schema['description'].to_str
if description
description.split("\n", -1).each do |descline|
lines << "# " + descline
end
lines << "#"
end

schema.described_hash_property_names.each_with_index do |propname, i|
lines << "#" unless i == 0
lines << "# @!attribute [rw] #{propname}"

property_schema = schema['properties'].respond_to?(:to_hash) &&
schema['properties'][propname].respond_to?(:to_hash) &&
schema['properties'][propname]

required = property_schema && property_schema['required']
required ||= schema['required'].respond_to?(:to_ary) && schema['required'].include?(propname)
lines << "# @required" if required

type = property_schema &&
property_schema['type'].respond_to?(:to_str) &&
property_schema['type'].to_str
simple = {'string' => 'String', 'number' => 'Numeric', 'boolean' => 'Boolean', 'null' => 'nil'}
rettypes = []
if simple.key?(type)
rettypes << simple[type]
elsif type == 'object' || type == 'array'
rettypes = []
schema_class = Scorpio.class_for_schema(property_schema)
unless schema_class.name =~ /\AScorpio::SchemaClasses::/
rettypes << schema_class.name
end
rettypes << {'object' => '#to_hash', 'array' => '#to_ary'}[type]
elsif type
# not really valid, but there's some information in there. whatever it is.
rettypes << type
end
# we'll add Object to all because the accessor methods have no enforcement that their value is
# of the specified type, and may return anything really. TODO: consider if this is of any value?
rettypes << 'Object'
lines << "# @return [#{rettypes.join(', ')}]"

description = property_schema &&
property_schema['description'].respond_to?(:to_str) &&
property_schema['description'].to_str
if description
description.split("\n", -1).each do |descline|
lines << "# " + descline
end
end
end
lines.join("\n")
end

def to_rb
lines = []
description = schema &&
schema['description'].respond_to?(:to_str) &&
schema['description'].to_str
if description
description.split("\n", -1).each do |descline|
lines << "# " + descline
end
end
lines << "class #{name}"
schema.described_hash_property_names.each_with_index do |propname, i|
lines << "" unless i == 0
Expand All @@ -22,6 +90,7 @@ def to_rb

required = property_schema && property_schema['required']
required ||= schema['required'].respond_to?(:to_ary) && schema['required'].include?(propname)
lines << " # @required" if required

type = property_schema &&
property_schema['type'].respond_to?(:to_str) &&
Expand Down

0 comments on commit e3e84ed

Please sign in to comment.