class CIMI::Model::Schema::Ref

Public Class Methods

new(name, opts={}, &block) click to toggle source
# 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

Public Instance Methods

find(ctx) click to toggle source
# File lib/cimi/models/schema.rb, line 201
def find(ctx)
  klass.find(ref_id(ctx), ctx)
end
href?() click to toggle source
# File lib/cimi/models/schema.rb, line 211
def href?; !href.nil?; end
klass() click to toggle source
# File lib/cimi/models/schema.rb, line 205
def klass
  self.class.superclass
end
ref_id(ctx) click to toggle source
# File lib/cimi/models/schema.rb, line 195
def ref_id(ctx)
  # FIXME: We should use ctx's routes to split
  # out the :id
  href.split('/').last
end
valid?(value) click to toggle source

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

# 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