Module: Garage::Representer::ClassMethods

Defined in:
lib/garage/representer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#representer_attrsObject



84
85
86
# File 'lib/garage/representer.rb', line 84

def representer_attrs
  @representer_attrs ||= []
end

Instance Method Details

#accessible(*args) ⇒ Object



108
109
110
111
112
# File 'lib/garage/representer.rb', line 108

def accessible(*args)
  ->(resource, responder){
    responder.controller.allow_access?(*args)
  }
end

#collection(name, options = {}) ⇒ Object



96
97
98
# File 'lib/garage/representer.rb', line 96

def collection(name, options={})
  representer_attrs << Collection.new(name, options)
end

#inherited(child) ⇒ Object



79
80
81
82
# File 'lib/garage/representer.rb', line 79

def inherited(child)
  super
  child.representer_attrs = self.representer_attrs.clone
end


92
93
94
# File 'lib/garage/representer.rb', line 92

def link(rel, options={}, &block)
  representer_attrs << Link.new(rel, options, block)
end

#metadataObject

represents the representer’s schema in JSON format



115
116
117
118
119
# File 'lib/garage/representer.rb', line 115

def 
  {:definitions => representer_attrs.grep(Definition).map {|definition| definition.name},
   :links => representer_attrs.grep(Link).map {|link| link.options[:as] ? {link.rel => {'as' => link.options[:as]}} : link.rel}
  }
end

#oauth_scope(scope) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/garage/representer.rb', line 100

def oauth_scope(scope)
  ->(resource, responder){
    # FIXME: this only works with User resource for now
    # partial representation will not render request scope-specific fields for better caching
    !resource.partial? && responder.controller.requested_by?(resource) && responder.controller.has_scope?(scope)
  }
end

#param(*keys) ⇒ Object



121
122
123
# File 'lib/garage/representer.rb', line 121

def param(*keys)
  keys.each {|key| params << key }
end

#paramsObject



125
126
127
# File 'lib/garage/representer.rb', line 125

def params
  @params ||= []
end

#property(name, options = {}) ⇒ Object



88
89
90
# File 'lib/garage/representer.rb', line 88

def property(name, options={})
  representer_attrs << Definition.new(name, options)
end