Class: CIMI::Model::Schema::Ref

Inherits:
Struct show all
Defined in:
lib/cimi/models/schema.rb

Instance Attribute Summary

Attributes inherited from Struct

#schema

Attributes inherited from Attribute

#json_name, #name, #required, #xml_name

Instance Method Summary collapse

Methods inherited from Struct

#convert, #convert_from_json, #convert_from_xml, #convert_to_json, #convert_to_xml, #from_json, #from_xml, #to_json, #to_xml

Methods inherited from Attribute

#convert, #from_json, #from_xml, #required?, #to_json, #to_xml

Constructor Details

#initialize(name, opts = {}, &block) ⇒ Ref

Returns a new instance of Ref.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/cimi/models/schema.rb', line 186

def initialize(name, opts={}, &block)
  raise 'The :class attribute must be set' unless opts[:class]
  refname = "#{opts[:class].name.split("::").last}Ref"
  if CIMI::Model::const_defined?(refname)
    @klass = CIMI::Model::const_get(refname)
  else
    @klass = Class.new(opts[:class]) do |m|
      scalar :href

      def ref_id(ctx)
        # FIXME: We should use ctx's routes to split
        # out the :id
        href.split('/').last
      end

      def find(ctx)
        klass.find(ref_id(ctx), ctx)
      end

      def klass
        self.class.superclass
      end
    end
    CIMI::Model::const_set(refname, @klass)
  end
  @klass.class_eval { def href?; !href.nil?; end }
  opts[:schema] = @klass.schema
  super(name, opts, &block)
end

Instance Method Details

#valid?(value) ⇒ Boolean

The ‘ref’ could be the reference to a CIMI entity or the full CIMI entity representation.

Returns:

  • (Boolean)


219
220
221
222
223
# File 'lib/cimi/models/schema.rb', line 219

def valid?(value)
  !value.href.nil? || @klass.schema.required_attributes.all? { |a|
    a.valid?(value.send(a.name))
  }
end