class Praxis::Links

Container for links for a given type

Attributes

Public Class Methods

_finalize!() click to toggle source
Calls superclass method
# File lib/praxis/links.rb, line 57
def self._finalize!
  super
  if @attribute
    # Master and default views must be set for all attributes, always using their :link view
    self.define_default_view
    self.define_master_view
    self.fixup_reference_struct_methods
  end
end
construct(constructor_block, options) click to toggle source
# File lib/praxis/links.rb, line 45
def self.construct(constructor_block, options)
  options[:reference] = @reference
  options[:dsl_compiler_options] = {links: self.links}

  self.attributes(options, &constructor_block)
  self
end
constructable?() click to toggle source
# File lib/praxis/links.rb, line 41
def self.constructable?
  true
end
define_default_view() click to toggle source
# File lib/praxis/links.rb, line 92
def self.define_default_view
  return unless view(:default).nil?

  view(:default) {}
  self.attributes.each do |name, attribute|
    view(:default).attribute(name, view: :link)
  end
end
define_master_view() click to toggle source
# File lib/praxis/links.rb, line 101
def self.define_master_view

  view(:master) {}
  self.attributes.each do |name, attribute|
    view(:master).attribute(name, view: :link)
  end
end
define_reader!(name) click to toggle source
# File lib/praxis/links.rb, line 67
def self.define_reader!(name)
  # it's faster to use define_method in this case than module_eval
  # because we save the attribute lookup on every access.
  attribute = self.attributes[name]
  using = self.links.fetch(name) do
    raise Exceptions::InvalidConfiguration.new("Cannot define attribute for #{name.inspect}")
  end

  define_method(name) do
    value = @object.__send__(using)
    return value if value.nil? || value.kind_of?(attribute.type)
    attribute.load(value)
  end

  # do whatever crazy aliasing we need to here....
  unless name == using
    @attribute.type.instance_eval do
      define_method(using) do
        self.__send__(name)
      end
    end
  end

end
describe(shallow=false,**opts) click to toggle source
Calls superclass method
# File lib/praxis/links.rb, line 53
def self.describe(shallow=false,**opts)
  super(false,**opts) # Links must always describe attributes
end
fixup_reference_struct_methods() click to toggle source

Define methods on the inner Struct class for links that do not have corresponding top-level attributes. This is primarily necessary only for example generation.

# File lib/praxis/links.rb, line 112
def self.fixup_reference_struct_methods
  self.links.each do |name, using|
    next if @reference.attribute.attributes.has_key?(using)
    @reference.attribute.type.instance_eval do
      define_method(using) do
        return nil unless attributes[:links]
        attributes[:links].__send__(name)
      end
    end
  end
end
for(reference) click to toggle source
# File lib/praxis/links.rb, line 27
def self.for(reference)
  if defined?(reference::Links)
    return reference::Links
  end

  klass = Class.new(self) do
    @reference = reference
    @links = Hash.new
    anonymous_type
  end

  reference.const_set :Links, klass
end
validate(*args) click to toggle source
# File lib/praxis/links.rb, line 124
def self.validate(*args)
  # FIXME: what to validate for links?
  []
end