Class: SlowBlink::Message::Model

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(schema, **opts) ⇒ Model

Generate a Model from a Schema

Parameters:

Options Hash (**opts):

  • :maxRecursion (Integer)


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

#maxRecursionInteger (readonly)

Returns:

  • (Integer)


97
98
99
# File 'lib/slow_blink/message/model.rb', line 97

def maxRecursion
  @maxRecursion
end

#schemaSlowBlink::Schema (readonly)

Returns:



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

Parameters:

  • input (StringIO)

    Blink Protocol compact form

Returns:

  • (Group)

    anonymous subclass instance of Group

Raises:



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

#group(name) ⇒ Group

Get a Group by name

Parameters:

  • name (String)

    name of group (may be qualified)

Returns:

  • (Group)

    anonymous subclass of Group

Raises:

  • (RangeError)

    unknown group



167
168
169
170
171
172
173
# File 'lib/slow_blink/message/model.rb', line 167

def group(name)
    if (group = @groups[name]).nil?
        raise RangeError.new "group '#{name}' is unknown"
    else
        group
    end                
end