Class: SinatraResource::Builder
- Inherits:
-
Object
- Object
- SinatraResource::Builder
- Defined in:
- lib/builder.rb,
lib/builder/helpers.rb,
lib/builder/mongo_helpers.rb,
lib/builder/action_definitions.rb
Defined Under Namespace
Modules: ActionDefinitions, Helpers, MongoHelpers
Constant Summary collapse
- FILTER_KEY =
"filter"
- SEARCH_KEY =
"search"
- SHOW_KEY =
"show"
- ITEMS_PER_PAGE =
20
Instance Method Summary collapse
- #build ⇒ Object
- #build_delete ⇒ Object
- #build_get_many ⇒ Object
- #build_get_one ⇒ Object
- #build_helpers ⇒ Object
- #build_post ⇒ Object
- #build_put ⇒ Object
-
#initialize(klass) ⇒ Builder
constructor
A new instance of Builder.
Constructor Details
#initialize(klass) ⇒ Builder
Returns a new instance of Builder.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/builder.rb', line 11 def initialize(klass) @klass = klass @resource_config = @klass.resource_config @child_assoc = @resource_config[:child_assoc] @model = @resource_config[:model] @parent = @resource_config[:parent] @path = @resource_config[:path] if @parent @parent_resource_config = @parent.resource_config @parent_model = @parent_resource_config[:model] end end |
Instance Method Details
#build ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/builder.rb', line 25 def build build_get_one build_get_many build_post build_put build_delete build_helpers end |
#build_delete ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/builder.rb', line 158 def build_delete model = @model resource_config = @resource_config if !@parent @klass.delete '/:id/?' do id = params.delete("id") role = role_for(model, id) document_for_delete(role, model, resource_config, true, id, nil, nil) display(:delete, "", resource_config) end else child_assoc = @child_assoc parent_model = @parent_model parent_resource_config = @parent_resource_config path = @path @parent.delete "/:parent_id/#{path}/:id/?" do id = params.delete("id") parent_id = params.delete("parent_id") parent_role = role_for(parent_model, parent_id) parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, nil, nil) # ------ role = role_for_nested(parent_document, child_assoc, model, id) document = document_for_delete(role, model, resource_config, true, id, parent_document, child_assoc) display(:delete, "", resource_config, parent_id) end end end |
#build_get_many ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/builder.rb', line 64 def build_get_many model = @model resource_config = @resource_config if !@parent @klass.get '/?' do role = lookup_role(nil) page = get_page(params) documents = documents_for_get_many(role, model, resource_config, page, ITEMS_PER_PAGE, true, nil, nil) document_count = document_count_for_get_many(model, resource_config, nil, nil) page_count = calculate_page_count(document_count, ITEMS_PER_PAGE) resources = build_resources(documents, resource_config, page, page_count, document_count, ITEMS_PER_PAGE) log_event(:get_many, params) display(:list, resources, resource_config) end else child_assoc = @child_assoc parent_model = @parent_model parent_resource_config = @parent_resource_config path = @path @parent.get "/:parent_id/#{path}/?" do parent_id = params.delete("parent_id") parent_role = role_for(parent_model, parent_id) parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, nil, nil) # ------ role = lookup_role(nil) page = get_page(params) documents = documents_for_get_many(role, model, resource_config, page, ITEMS_PER_PAGE, true, parent_document, child_assoc) document_count = document_count_for_get_many(model, resource_config, parent_document, child_assoc) page_count = calculate_page_count(document_count, ITEMS_PER_PAGE) resources = build_resources(documents, resource_config, page, page_count, document_count, ITEMS_PER_PAGE) log_event(:get_many, params) display(:list, resources, resource_config, parent_id) end end end |
#build_get_one ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/builder.rb', line 34 def build_get_one model = @model resource_config = @resource_config if !@parent @klass.get '/:id/?' do id = params.delete("id") role = role_for(model, id) document = document_for_get_one(role, model, resource_config, true, id, nil, nil) resource = build_resource(role, document, resource_config) display(:read, resource, resource_config) end else child_assoc = @child_assoc parent_model = @parent_model parent_resource_config = @parent_resource_config path = @path @parent.get "/:parent_id/#{path}/:id/?" do id = params.delete("id") parent_id = params.delete("parent_id") parent_role = role_for(parent_model, parent_id) parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, nil, nil) # ------ role = role_for_nested(parent_document, child_assoc, model, id) document = document_for_get_one(role, model, resource_config, true, id, parent_document, child_assoc) resource = build_resource(role, document, resource_config) display(:read, resource, resource_config, parent_id) end end end |
#build_helpers ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/builder.rb', line 186 def build_helpers @klass.helpers do include ActionDefinitions include Helpers include MongoHelpers end end |
#build_post ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/builder.rb', line 100 def build_post model = @model resource_config = @resource_config if !@parent @klass.post '/?' do role = lookup_role(nil) document = document_for_post(role, model, resource_config, true, nil, nil) resource = build_resource(role, document, resource_config) display(:create, resource, resource_config) end else child_assoc = @child_assoc parent_model = @parent_model parent_resource_config = @parent_resource_config path = @path @parent.post "/:parent_id/#{path}/?" do parent_id = params.delete("parent_id") parent_role = role_for(parent_model, parent_id) parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, nil, nil) # ------ role = lookup_role(nil) document = document_for_post(role, model, resource_config, true, parent_document, child_assoc) resource = build_resource(role, document, resource_config) display(:create, resource, resource_config, parent_id) end end end |
#build_put ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/builder.rb', line 128 def build_put model = @model resource_config = @resource_config if !@parent @klass.put '/:id/?' do id = params.delete("id") role = role_for(model, id) document = document_for_put(role, model, resource_config, true, id, nil, nil) resource = build_resource(role, document, resource_config) display(:update, resource, resource_config) end else child_assoc = @child_assoc parent_model = @parent_model parent_resource_config = @parent_resource_config path = @path @parent.put "/:parent_id/#{path}/:id/?" do id = params.delete("id") parent_id = params.delete("parent_id") parent_role = role_for(parent_model, parent_id) parent_document = document_for_get_one(parent_role, parent_model, parent_resource_config, false, parent_id, id, id) # ------ role = role_for_nested(parent_document, child_assoc, model, id) document = document_for_put(role, model, resource_config, true, id, parent_document, child_assoc) resource = build_resource(role, document, resource_config) display(:update, resource, resource_config, parent_id) end end end |