Module: SinatraResource::Resource::ClassMethods

Defined in:
lib/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resource_configObject (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

#buildundefined

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.

Returns:

  • (undefined)


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.

Parameters:

  • method (Symbol)

    A symbol that refers to a method on the model.

Returns:

  • (undefined)


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.

Parameters:

  • method (Symbol)

    A symbol that refers to a method on the parent.

Returns:

  • (undefined)


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

Examples:

model User

# which refers to, for example ...
# class User
#   include MongoMapper::Document
#   ...
# end

Parameters:

  • model (MongoMapper::Document)

Returns:

  • (undefined)


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.

Parameters:

  • resource (Class)

Returns:

  • (undefined)


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.

Parameters:

  • name (String)

Returns:

  • (undefined)


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.

Examples:

permission :list   => :basic
permission :read   => :basic
permission :create => :basic
permission :update => :owner
permission :delete => :owner

Parameters:

  • access_rules (Hash<Symbol => Symbol>)

    valid keys are:

    :list, :read, :create, :update, :delete
    

    values should be a role (such as :admin)

Returns:

  • (undefined)


126
127
128
129
130
131
# File 'lib/resource.rb', line 126

def permission(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.

Examples:

property :name,  :r => :basic
property :email, :r => :owner
property :role,  :r => :owner, :w => :admin

Parameters:

  • name (Symbol)
  • access_rules (Hash)

Returns:

  • (undefined)


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, options={}, &block)
  hide = options.delete(:hide_by_default) || false
  access_rules = options
  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).

Parameters:

  • name (Symbol)

Returns:

  • (undefined)


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.

Examples:

roles Roles

# which refers to, for example ...
# module Roles
#   include SinatraResource::Roles
# 
#   role :anonymous
#   role :basic => :anonymous
#   role :admin => :basic
# end

Parameters:

  • klass (Class)

Returns:

  • (undefined)


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

#setupObject

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