Class: Scorpio::SchemaInstanceBase

Inherits:
Object
  • Object
show all
Includes:
FingerprintHash, Memoize
Defined in:
lib/scorpio/schema_instance_base.rb,
lib/scorpio/schema_instance_base/to_rb.rb

Overview

base class for representing an instance of an instance described by a schema

Defined Under Namespace

Modules: OverrideFromExtensions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FingerprintHash

#==, #hash

Methods included from Memoize

#clear_memo, #memoize

Constructor Details

#initialize(instance) ⇒ SchemaInstanceBase

Returns a new instance of SchemaInstanceBase.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/scorpio/schema_instance_base.rb', line 48

def initialize(instance)
  unless respond_to?(:schema)
    raise(TypeError, "cannot instantiate #{self.class.inspect} which has no method #schema. please use Scorpio.class_for_schema")
  end

  self.instance = instance

  if @instance.is_a?(Scorpio::JSON::HashNode)
    extend SchemaInstanceBaseHash
  elsif @instance.is_a?(Scorpio::JSON::ArrayNode)
    extend SchemaInstanceBaseArray
  end
  # certain methods need to be redefined after we are extended by Enumerable
  extend OverrideFromExtensions
end

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



70
71
72
# File 'lib/scorpio/schema_instance_base.rb', line 70

def instance
  @instance
end

Class Method Details

.class_commentObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/scorpio/schema_instance_base/to_rb.rb', line 5

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

.inspectObject



14
15
16
17
18
19
20
21
22
# File 'lib/scorpio/schema_instance_base.rb', line 14

def inspect
  if !respond_to?(:schema)
    super
  elsif !name || name =~ /\AScorpio::SchemaClasses::/
    %Q(#{SchemaClasses.inspect}[#{schema_id.inspect}])
  else
    %Q(#{name} (#{schema_id}))
  end
end

.nameObject



40
41
42
43
44
45
# File 'lib/scorpio/schema_instance_base.rb', line 40

def name
  unless super
    SchemaClasses.const_set(schema_classes_const_name, self)
  end
  super
end

.schema_classes_const_nameObject



33
34
35
36
37
38
# File 'lib/scorpio/schema_instance_base.rb', line 33

def schema_classes_const_name
  name = schema.schema_id.gsub(/[^\w]/, '_')
  name = 'X' + name unless name[/\A[a-zA-Z_]/]
  name = name[0].upcase + name[1..-1]
  name
end

.schema_idObject



10
11
12
# File 'lib/scorpio/schema_instance_base.rb', line 10

def schema_id
  schema.schema_id
end

.to_rbObject



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
120
121
122
123
124
# File 'lib/scorpio/schema_instance_base/to_rb.rb', line 65

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
    property_schema = schema['properties'].respond_to?(:to_hash) &&
      schema['properties'][propname].respond_to?(:to_hash) &&
      schema['properties'][propname]
    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
      lines << "  #" # blank comment line between description and @return
    end

    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(', ')}]"

    lines << "  def #{propname}"
    lines << "    super"
    lines << "  end"
  end
  lines << "end"
  lines.join("\n")
end

.to_sObject



23
24
25
26
27
28
29
30
31
# File 'lib/scorpio/schema_instance_base.rb', line 23

def to_s
  if !respond_to?(:schema)
    super
  elsif !name || name =~ /\AScorpio::SchemaClasses::/
    %Q(#{SchemaClasses.inspect}[#{schema_id.inspect}])
  else
    name
  end
end

Instance Method Details

#derefObject



72
73
74
75
76
77
78
79
# File 'lib/scorpio/schema_instance_base.rb', line 72

def deref
  derefed = instance.deref
  if derefed.object_id == instance.object_id
    self
  else
    self.class.new(derefed)
  end
end

#fingerprintObject



120
121
122
# File 'lib/scorpio/schema_instance_base.rb', line 120

def fingerprint
  {class: self.class, instance: instance}
end

#fragmentObject



86
87
88
# File 'lib/scorpio/schema_instance_base.rb', line 86

def fragment
  instance.fragment
end

#fully_validateObject



90
91
92
# File 'lib/scorpio/schema_instance_base.rb', line 90

def fully_validate
  schema.fully_validate(instance)
end

#inspectObject



99
100
101
# File 'lib/scorpio/schema_instance_base.rb', line 99

def inspect
  "\#<#{self.class.to_s} #{instance.inspect}>"
end

#modified_copy(&block) ⇒ Object



81
82
83
84
# File 'lib/scorpio/schema_instance_base.rb', line 81

def modified_copy(&block)
  modified_instance = instance.modified_copy(&block)
  self.class.new(modified_instance)
end

#object_group_textObject



116
117
118
# File 'lib/scorpio/schema_instance_base.rb', line 116

def object_group_text
  instance.object_group_text
end

#pretty_print(q) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/scorpio/schema_instance_base.rb', line 102

def pretty_print(q)
  q.instance_exec(self) do |obj|
    text "\#<#{obj.class.to_s}"
    group_sub {
      nest(2) {
        breakable ' '
        pp obj.instance
      }
    }
    breakable ''
    text '>'
  end
end

#validateObject



93
94
95
# File 'lib/scorpio/schema_instance_base.rb', line 93

def validate
  schema.validate(instance)
end

#validate!Object



96
97
98
# File 'lib/scorpio/schema_instance_base.rb', line 96

def validate!
  schema.validate!(instance)
end