Module: SinatraResource::Builder::Helpers
- Defined in:
- lib/builder/helpers.rb
Instance Method Summary collapse
-
#build_resource(role, document, resource_config) ⇒ Hash<String => Object>
Build a resource, based on
document
, appropriate forrole
. -
#build_resources(documents, resource_config, page, page_count, document_count, items_per_page) ⇒ Array<Hash<String => Object>>
Builds a list of resources, based on
documents
, using the appropriate role for each document. - #calculate_page_count(document_count, items_per_page) ⇒ Object
-
#check_params(action, role, resource_config, leaf) ⇒ undefined
Halt unless the current params are ok for
action
androle
. -
#check_permission(action, role, resource_config) ⇒ undefined
Halt unless the current role has permission to carry out
action
. -
#convert ⇒ String
Convert
object
to desired format. -
#display(action, object, resource_config, parent_id = nil) ⇒ String
Display
object
as appropriate foraction
. -
#do_callback(name, resource_config, document, parent_document) ⇒ undefined
Execute a callback.
-
#full_uri(path) ⇒ String
Convert a path to a full URI.
-
#get_page(params) ⇒ Integer
Get the page parameter.
-
#link_to_page(page_number) ⇒ Hash
Build a hash that contains a URL to
page_number
. -
#minimum_role(action, resource_config, property = nil) ⇒ Symbol
Return the minimum role required for
action
, and, if specified,property
. -
#role_for(model, id) ⇒ Symbol
Get role, using
model
andid
. -
#role_for_nested(parent, child_assoc, child_model, child_id) ⇒ Symbol
Get role for a nested resource situation.
Instance Method Details
#build_resource(role, document, resource_config) ⇒ Hash<String => Object>
Build a resource, based on document
, appropriate for role
.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/builder/helpers.rb', line 17 def build_resource(role, document, resource_config) resource = {} resource_config[:properties].each_pair do |property, hash| hide = if params[SHOW_KEY] == "all" false else resource_config[:properties][property][:hide_by_default] end if (:read, role, resource_config, property) && !hide resource[property.to_s] = value(property, document, hash) end end resource end |
#build_resources(documents, resource_config, page, page_count, document_count, items_per_page) ⇒ Array<Hash<String => Object>>
Builds a list of resources, based on documents
, using the appropriate role for each document. (Delegates to lookup_role
.)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/builder/helpers.rb', line 46 def build_resources(documents, resource_config, page, page_count, document_count, items_per_page) if page_count > 0 && page > page_count error 400, convert(body_for(:errors, "page (#{page}) must be <= page_count (#{page_count})")) end { 'previous' => page > 1 ? link_to_page(page - 1) : nil, 'next' => page < page_count ? link_to_page(page + 1) : nil, 'page_count' => page_count, 'page_size' => items_per_page, 'document_count' => document_count, 'members' => documents.map do |document| build_resource(lookup_role(document), document, resource_config) end, } end |
#calculate_page_count(document_count, items_per_page) ⇒ Object
63 64 65 |
# File 'lib/builder/helpers.rb', line 63 def calculate_page_count(document_count, items_per_page) (document_count.to_f / items_per_page).ceil end |
#check_params(action, role, resource_config, leaf) ⇒ undefined
Halt unless the current params are ok for action
and role
.
82 83 84 85 86 |
# File 'lib/builder/helpers.rb', line 82 def check_params(action, role, resource_config, leaf) return unless leaf params_check_action(action) params_check_action_and_role(action, role, resource_config) end |
#check_permission(action, role, resource_config) ⇒ undefined
Halt unless the current role has permission to carry out action
99 100 101 102 103 104 |
# File 'lib/builder/helpers.rb', line 99 def (action, role, resource_config) (action, role, resource_config) unless (action, role, resource_config) error 401, convert(body_for(:unauthorized)) end end |
#convert ⇒ String
Convert object
to desired format.
For example, an application might want to convert object
to JSON or XML.
Applications must override this method.
116 117 118 |
# File 'lib/builder/helpers.rb', line 116 def convert raise NotImplementedError end |
#display(action, object, resource_config, parent_id = nil) ⇒ String
Display object
as appropriate for action
.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/builder/helpers.rb', line 130 def display(action, object, resource_config, parent_id = nil) case action when :list when :read when :create response.status = 201 response.headers['Location'] = location(object, resource_config, parent_id) when :update when :delete response.status = 204 else raise Error, "Unexpected: #{action.inspect}" end convert(object) end |
#do_callback(name, resource_config, document, parent_document) ⇒ undefined
Execute a callback.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/builder/helpers.rb', line 161 def do_callback(name, resource_config, document, parent_document) proc = resource_config[:callbacks][name] return unless proc if document && parent_document proc.call(self, document, parent_document) elsif document proc.call(self, document) elsif parent_document proc.call(self, parent_document) else proc.call(self) end end |
#full_uri(path) ⇒ String
Convert a path to a full URI.
Applications must override this method.
183 184 185 |
# File 'lib/builder/helpers.rb', line 183 def full_uri(path) raise NotImplementedError end |
#get_page(params) ⇒ Integer
Get the page parameter.
192 193 194 195 196 197 198 199 200 |
# File 'lib/builder/helpers.rb', line 192 def get_page(params) raw = params.delete('page') return 1 unless raw page = raw.to_i if page < 1 error 400, convert(body_for(:errors, "page must be >= 1")) end page end |
#link_to_page(page_number) ⇒ Hash
Build a hash that contains a URL to page_number
.
207 208 209 210 211 212 |
# File 'lib/builder/helpers.rb', line 207 def link_to_page(page_number) q = Rack::Utils.parse_query(request.query_string) q['page'] = page_number query_string = Rack::Utils.build_query(q) { 'href' => "#{request.path}?#{query_string}" } end |
#minimum_role(action, resource_config, property = nil) ⇒ Symbol
Return the minimum role required for action
, and, if specified, property
.
258 259 260 261 262 263 264 265 266 267 |
# File 'lib/builder/helpers.rb', line 258 def minimum_role(action, resource_config, property=nil) if property.nil? p = resource_config[:permission] raise Error, "undefined #{action.inspect} permission" unless p p[action] else hash = resource_config[:properties][property] hash ? hash[to_r_or_w(action)] : :nobody end || :anonymous end |
#role_for(model, id) ⇒ Symbol
Get role, using model
and id
. Delegates to lookup_role
.
When id
is present, it can help determine ‘relative’ roles such as ‘ownership’ of the current user of a particular document.
224 225 226 |
# File 'lib/builder/helpers.rb', line 224 def role_for(model, id) lookup_role(model.find_by_id(id)) end |
#role_for_nested(parent, child_assoc, child_model, child_id) ⇒ Symbol
Get role for a nested resource situation. Delegates to lookup_role
.
241 242 243 244 |
# File 'lib/builder/helpers.rb', line 241 def role_for_nested(parent, child_assoc, child_model, child_id) lookup_role( find_nested_document(parent, child_assoc, child_model, child_id)) end |