Class: Guide::ViewModel

Inherits:
OpenStruct
  • Object
show all
Defined in:
app/models/guide/view_model.rb

Instance Method Summary collapse

Constructor Details

#initialize(defaults = {}, overrides = {}) ⇒ ViewModel

Returns a new instance of ViewModel.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/guide/view_model.rb', line 2

def initialize(defaults = {}, overrides = {})
  inappropriate_overrides = overrides.keys - defaults.keys
  if inappropriate_overrides.any?
    raise Guide::Errors::InterfaceViolation.new(
      "You added the #{'method'.pluralize(inappropriate_overrides.size)} "\
      "[#{inappropriate_overrides.join(", ")}] to the #{self.class.name} "\
      "in your scenario that #{'is'.pluralize(inappropriate_overrides.size)} "\
      "not included in its official declaration "\
      "(maybe in the `view_model` method on your Structure)"
    )
  end

  @guide_view_model_interface_methods = defaults.keys

  super(defaults.merge(overrides))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'app/models/guide/view_model.rb', line 19

def method_missing(method, *args, &block)
  if respond_to?(method)
    super
  else
    raise Guide::Errors::InterfaceViolation.new(
      "You called a method '#{method}' from your template, "\
      "but it does not exist on the #{self.class.name} in your Structure."
    )
  end
end

Instance Method Details

#guide_view_model_interface_methodsObject



30
31
32
# File 'app/models/guide/view_model.rb', line 30

def guide_view_model_interface_methods
  @guide_view_model_interface_methods
end

#to_aryObject



34
35
36
# File 'app/models/guide/view_model.rb', line 34

def to_ary
  nil # because Cells calls ViewModel#flatten for some reason
end