Class: Lightrail::Wrapper::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/lightrail/wrapper/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, scope) ⇒ Model

Returns a new instance of Model.



69
70
71
72
# File 'lib/lightrail/wrapper/model.rb', line 69

def initialize(resource, scope)
  @resource = resource
  @scope    = scope
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



67
68
69
# File 'lib/lightrail/wrapper/model.rb', line 67

def resource
  @resource
end

#scopeObject (readonly)

Returns the value of attribute scope.



67
68
69
# File 'lib/lightrail/wrapper/model.rb', line 67

def scope
  @scope
end

Class Method Details

.around_association(collection, scope) ⇒ Object



16
17
18
# File 'lib/lightrail/wrapper/model.rb', line 16

def around_association(collection, scope)
  yield
end

.has_many(*associations) ⇒ Object

Declares that this object is associated to the given association with cardinality 1..*.



30
31
32
33
34
# File 'lib/lightrail/wrapper/model.rb', line 30

def has_many(*associations)
  options = associations.extract_options!
  self.associations += associations.map { |a| Associations::HasManyConfig.new(a, options) }
  define_association_methods(associations)
end

.has_one(*associations) ⇒ Object

Declares that this object is associated to the given association with cardinality 1..1.



22
23
24
25
26
# File 'lib/lightrail/wrapper/model.rb', line 22

def has_one(*associations)
  options = associations.extract_options!
  self.associations += associations.map { |a| Associations::HasOneConfig.new(a, options) }
  define_association_methods(associations)
end

.inherited(base) ⇒ Object



10
11
12
13
14
# File 'lib/lightrail/wrapper/model.rb', line 10

def inherited(base)
  base.class_eval do
    alias_method wrapped_class.model_name.underscore, :resource
  end
end

.valid_includes(includes) ⇒ Object

Based on the declared associations and the given parameters, generate an includes clause that can be passed down to the relation object to avoid doing N+1 queries.



39
40
41
42
43
44
45
46
47
# File 'lib/lightrail/wrapper/model.rb', line 39

def valid_includes(includes)
  result = []
  includes.each do |i|
    if association = associations.find { |a| a.includes == i }
      result << association.name
    end
  end
  result
end

.wrapped_classObject

Returns the original class wrapped by this wrapper.



50
51
52
# File 'lib/lightrail/wrapper/model.rb', line 50

def wrapped_class
  @wrapped_class ||= name.sub(/Wrapper$/, "").constantize
end

Instance Method Details

#render(options = {}, result = Result.new) ⇒ Object

Gets the view and render the associated object.



79
80
81
82
83
# File 'lib/lightrail/wrapper/model.rb', line 79

def render(options={}, result=Result.new)
  name         = options[:as] || wrapped_model_name.underscore
  result[name] = view = self.view
  _include_associations(result, view, options[:include])
end

#render_many(options = {}, result = Result.new) ⇒ Object

Gets the view and render the associated considering it is part of a collection.



86
87
88
89
90
91
# File 'lib/lightrail/wrapper/model.rb', line 86

def render_many(options={}, result=Result.new)
  name     = options[:as] || wrapped_model_name.plural
  view     = self.view
  array    = (result[name] ||= []) << view
  _include_associations(result, view, options[:include])
end

#viewObject

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/lightrail/wrapper/model.rb', line 74

def view
  raise NotImplementedError, "No view implemented for #{self}"
end