class Restroom::Context

Constants

ATTRIBUTES
INHERITABLE
STRUCTURE

Public Class Methods

new(host: nil, parent:, key: nil, **args, &block) click to toggle source
# File lib/restroom/context.rb, line 21
def initialize(host: nil, parent:, key: nil, **args, &block)
  @children = []
  @id = :id

  args.each { |k, v| send "#{k}=", v }
  instance_eval &block if block_given?

  @key = key
  @resource ||= key
  @model ||= classify_resource(@resource)
  @host = host || model # TODO guess model from key
  @parent = parent

  @model.send(:attr_accessor, :restroom_parent) if @model

  @host.include Relation
  @children.each do |child|
    @host.add_relation(child.key, child)
  end
end

Public Instance Methods

classify_resource(resource) click to toggle source
# File lib/restroom/context.rb, line 42
def classify_resource(resource)
  resource.to_s.classify.constantize if resource
end
dump() click to toggle source
# File lib/restroom/context.rb, line 50
def dump
  dumper(self, 0)
end
dumper(context, depth) click to toggle source
# File lib/restroom/context.rb, line 54
def dumper context, depth
  puts "#{'  ' * depth}#{context} - host: #{context.host}, parent: #{context.parent}, class: #{context.model}, id: #{context.id}"#, filter: #{context.response_filter}"
  context.children.each do |child|
    dumper(child, depth+1)
  end
end
exposes(key, **args, &block) click to toggle source
# File lib/restroom/context.rb, line 46
def exposes key, **args, &block
  @children << child = self.class.new(key: key, parent: self, **args, &block)
end