Module: SinatraResource::Resource::ClassMethods
- Defined in:
- lib/resource.rb
Instance Attribute Summary collapse
-
#resource_config ⇒ Object
readonly
Returns the value of attribute resource_config.
Instance Method Summary collapse
-
#build ⇒ undefined
Build the Sinatra actions based on the DSL statements in this class.
-
#callback(name, &block) ⇒ undefined
Specify a callback.
-
#child_association(method) ⇒ undefined
Specify the association
method
of a parent model that points to its child model. -
#model(model) ⇒ undefined
Specify the underlying
model
. -
#parent(resource) ⇒ undefined
Specify the parent
resource
. -
#path(name) ⇒ undefined
Specify the path.
-
#permission(access_rules) ⇒ undefined
Specify the minimal role needed to access this resource for reading or writing.
-
#property(name, options = {}, &block) ⇒ undefined
Declare a property and its access rules.
-
#relation(name, &block) ⇒ undefined
Declare a relation with a block of code.
-
#roles(klass) ⇒ undefined
Specify the role definitions for this resource.
-
#setup ⇒ Object
For internal use.
Instance Attribute Details
#resource_config ⇒ Object (readonly)
Returns the value of attribute resource_config.
15 16 17 |
# File 'lib/resource.rb', line 15 def resource_config @resource_config end |
Instance Method Details
#build ⇒ undefined
Build the Sinatra actions based on the DSL statements in this class. You will want to do this last.
If for some reason you reopen the class, you will need to call this method again. However, this usage has not been tested.
56 57 58 59 60 |
# File 'lib/resource.rb', line 56 def build deferred_defaults validate Builder.new(self).build end |
#callback(name, &block) ⇒ undefined
Specify a callback.
23 24 25 26 27 28 29 30 31 |
# File 'lib/resource.rb', line 23 def callback(name, &block) unless @resource_config[:callbacks].include?(name) raise DefinitionError, "callback #{name.inspect} is not supported #{self}" end if @resource_config[:callbacks][name] raise DefinitionError, "callback #{name.inspect} already declared in #{self}" end @resource_config[:callbacks][name] = block end |
#child_association(method) ⇒ undefined
Specify the association method
of a parent model that points to its child model.
Required for all nested resources.
42 43 44 45 46 47 |
# File 'lib/resource.rb', line 42 def child_association(method) if @resource_config[:child_assoc] raise DefinitionError, "child_association already declared in #{self}" end @resource_config[:child_assoc] = method end |
#model(model) ⇒ undefined
Specify the underlying model
76 77 78 79 80 81 |
# File 'lib/resource.rb', line 76 def model(model) if @resource_config[:model] raise DefinitionError, "model already declared in #{self}" end @resource_config[:model] = model end |
#parent(resource) ⇒ undefined
Specify the parent resource
. Only used for nested resources.
88 89 90 91 92 93 |
# File 'lib/resource.rb', line 88 def parent(resource) if @resource_config[:parent] raise DefinitionError, "parent already declared in #{self}" end @resource_config[:parent] = resource end |
#path(name) ⇒ undefined
Specify the path. If not specified, SinatraResource will infer the path from the resource class (see the set_default_path
method.)
This method is also useful for nested resources.
103 104 105 106 107 108 |
# File 'lib/resource.rb', line 103 def path(name) if @resource_config[:path] raise DefinitionError, "path already declared in #{self}" end @resource_config[:path] = name end |
#permission(access_rules) ⇒ undefined
Specify the minimal role needed to access this resource for reading or writing.
126 127 128 129 130 131 |
# File 'lib/resource.rb', line 126 def (access_rules) access_rules.each_pair do |verb, role| @resource_config[:roles].validate_role(role) @resource_config[:permission][verb] = role end end |
#property(name, options = {}, &block) ⇒ undefined
Declare a property and its access rules.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/resource.rb', line 145 def property(name, ={}, &block) hide = .delete(:hide_by_default) || false access_rules = if @resource_config[:properties][name] raise DefinitionError, "property #{name.inspect} already declared in #{self}" end @resource_config[:properties][name] = {} if block @resource_config[:properties][name][:w] = :nobody @resource_config[:properties][name][:read_proc] = block if access_rules[:w] && access_rules[:w] != :nobody raise DefinitionError, "property #{name.inspect} is using block " + "form. :w => :nobody is assumed and cannot be overridden." end end access_rules.each_pair do |kind, role| @resource_config[:roles].validate_role(role) @resource_config[:properties][name][kind] = role @resource_config[:properties][name][:hide_by_default] = hide end end |
#relation(name, &block) ⇒ undefined
Declare a relation with a block of code.
Only needed with nested resources.
For example:
relation :create do |parent, child|
Categorization.create(
:category_id => parent.id,
:source_id => child.id
)
end
This runs after a POST to automatically connect parent with child.
Is useful to create a document that joins a category (the parent) to the source (the child).
189 190 191 192 193 194 |
# File 'lib/resource.rb', line 189 def relation(name, &block) if @resource_config[:relation][name] raise DefinitionError, "relation #{name.inspect} already declared in #{self}" end @resource_config[:relation][name] = block end |
#roles(klass) ⇒ undefined
Specify the role definitions for this resource.
213 214 215 216 217 218 |
# File 'lib/resource.rb', line 213 def roles(klass) if @resource_config[:roles] raise DefinitionError, "roles already declared in #{self}" end @resource_config[:roles] = klass end |
#setup ⇒ Object
For internal use. Initializes internal data structure.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/resource.rb', line 221 def setup @resource_config = { :callbacks => { :before_create => nil, :after_create => nil, :before_update => nil, :after_update => nil, :before_destroy => nil, :after_destroy => nil, }, :child_assoc => nil, :model => nil, :parent => nil, :path => nil, :permission => {}, :properties => {}, :relation => { :create => nil, :delete => nil, }, :roles => nil, } end |