Module: ModelView

Defined in:
lib/model_view.rb,
lib/model_view/version.rb,
lib/model_view/resolver.rb

Defined Under Namespace

Classes: Resolver

Constant Summary collapse

ROOT =
:__root__
VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#as_hash(object, scope = nil, context = {}) ⇒ Object



39
40
41
# File 'lib/model_view.rb', line 39

def as_hash(object, scope=nil, context={})
  ModelView::Resolver.resolve(object, @scopes, scope || ROOT, context)
end

#extend_scope(*scope) ⇒ Object

Raises:

  • (Exception)


30
31
32
33
# File 'lib/model_view.rb', line 30

def extend_scope(*scope)
  raise Exception.new("Root scope can not extend another scope") if @current_scope.nil? || @current_scope == ROOT
  scope.flatten.each { |s| @scopes[@current_scope][:extends] << s }
end

#field(field_name, arg = {}, &block) ⇒ Object



7
8
9
10
# File 'lib/model_view.rb', line 7

def field(field_name, arg={}, &block)
  scope_name = @current_scope || ROOT
  add_field scope_name, field_name, arg, block
end

#fields(*fields) ⇒ Object



12
13
14
# File 'lib/model_view.rb', line 12

def fields(*fields)
  fields.flatten.each { |f| field f }
end

#include_scope(*scope) ⇒ Object

Raises:

  • (Exception)


25
26
27
28
# File 'lib/model_view.rb', line 25

def include_scope(*scope)
  raise Exception.new("Root scope can not include another scope") if @current_scope.nil? || @current_scope == ROOT
  scope.flatten.each { |s| @scopes[@current_scope][:includes] << s }
end

#scope(scope_name, &block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/model_view.rb', line 16

def scope(scope_name, &block)
  sym_scope_name = scope_name.to_sym
  add_scope(sym_scope_name)

  @current_scope = sym_scope_name
  instance_eval &block
  @current_scope = nil
end

#scopesObject



35
36
37
# File 'lib/model_view.rb', line 35

def scopes
  @scopes
end