Class: SlowBlink::Message::Model
- Inherits:
-
Object
- Object
- SlowBlink::Message::Model
- Defined in:
- lib/slow_blink/message/model.rb
Overview
Use Model to create message models from a Schema
Constant Summary collapse
- DEFAULT_MAX_RECURSION =
the maximum level of nesting in messages able to be decoded by models
100
Instance Attribute Summary collapse
- #maxRecursion ⇒ Integer readonly
- #schema ⇒ SlowBlink::Schema readonly
Instance Method Summary collapse
-
#decode_compact(input) ⇒ Group
Initialise a Group from a compact form string.
-
#group(name) ⇒ Group
Get a Group by name.
-
#initialize(schema, **opts) ⇒ Model
constructor
Generate a Model from a Schema.
Constructor Details
#initialize(schema, **opts) ⇒ Model
Generate a Model from a Schema
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/slow_blink/message/model.rb', line 111 def initialize(schema, **opts) @schema = schema @taggedGroups = {} @groups = {} @maxRecursion = opts[:maxRecursion]||DEFAULT_MAX_RECURSION # any of the groups taggedGroups = @taggedGroups @anyTaggedGroup = Class.new(DynamicGroup) do @anyTaggedGroup = self @taggedGroups = taggedGroups @permittedID = schema.groups.map{|g|g.id}.select{|g|g} end # create an anon class for each group defined in schema schema.groups.each do |g| fields = g.fields.map{|f| _model_field(f)} @groups[g.name] = Class.new(Group) do @name = g.name @id = g.id @fields = fields end if g.id @taggedGroups[g.id] = @groups[g.name] end end end |
Instance Attribute Details
#maxRecursion ⇒ Integer (readonly)
97 98 99 |
# File 'lib/slow_blink/message/model.rb', line 97 def maxRecursion @maxRecursion end |
#schema ⇒ SlowBlink::Schema (readonly)
100 101 102 |
# File 'lib/slow_blink/message/model.rb', line 100 def schema @schema end |
Instance Method Details
#decode_compact(input) ⇒ Group
Initialise a Group from a compact form string
150 151 152 153 154 155 156 157 158 |
# File 'lib/slow_blink/message/model.rb', line 150 def decode_compact(input) depth = @maxRecursion group = @anyTaggedGroup.from_compact(input, depth) if group.nil? raise "top level group cannot be null" else group.get end end |