Class: RubyMVC::Models::ViewModelTemplate

Inherits:
Object
  • Object
show all
Includes:
ActionProvider
Defined in:
lib/ruby_mvc/models/view_model_template.rb

Overview

This class provides a proxy for Model instances that allows them to control the visibility of which properties are exposed for a given view. ViewModelTemplate instances can be reused across view instances where the same properties are to be exposed.

The ViewModelTemplate class implements the same interface as Model instances so that they can be used as drop-in replacements everywhere a model instance can be used.

The key difference is that the ViewModelTemplate exists outside of any particular model instances, allowing the same view binding to work for numerous concrete model instances, effectively speifying the template through which the model may be accessed.

Instance Attribute Summary collapse

Attributes included from ActionProvider

#actions

Instance Method Summary collapse

Methods included from ActionProvider

#action

Constructor Details

#initialize(options = {}, &block) ⇒ ViewModelTemplate

When the ViewModelTemplate is initialized, it requires a set of options and/or an optional initializer block that is executed within the scope of the newly created instance.



54
55
56
57
58
59
60
61
62
# File 'lib/ruby_mvc/models/view_model_template.rb', line 54

def initialize(options = {}, &block)
  @options = options
  @labels = []
  @link_labels = []
  @options[:editable] ||= {}
  @options[:properties] ||= []
  @options[:links] ||= []
  self.instance_eval(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



164
165
166
167
168
169
170
# File 'lib/ruby_mvc/models/view_model_template.rb', line 164

def method_missing(m, *args, &block)
  if @model
    @model.send(m, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#titleObject

Returns the value of attribute title.



48
49
50
# File 'lib/ruby_mvc/models/view_model_template.rb', line 48

def title
  @title
end

Instance Method Details

#apply(model) ⇒ Object

This method is used to apply the template to a specific, concrete model instance, creating a clone of the template in the process so that multiple different models can be used with the same template definition.



159
160
161
162
# File 'lib/ruby_mvc/models/view_model_template.rb', line 159

def apply(model)
  @model = model
  self.clone
end

#editable(key, val = true) ⇒ Object

This method is used to mark a property key editable or not through the view. This method does not impact the editability of the underlying model



97
98
99
# File 'lib/ruby_mvc/models/view_model_template.rb', line 97

def editable(key, val = true)
  @options[:editable][key.to_sym] = val
end

#is_editable?(key) ⇒ Boolean

Implement the Model#is_editable? method in terms of the template definition. If the template doesn’t further restrict the behavior, the model’s definition is used instead.

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
# File 'lib/ruby_mvc/models/view_model_template.rb', line 84

def is_editable?(key)
  k = key.to_sym
  if @options[:editable].has_key? k
    @options[:editable][k]
  else
    true
  end
end

#keysObject



64
65
66
# File 'lib/ruby_mvc/models/view_model_template.rb', line 64

def keys
  @options[:properties]
end

#labelsObject

Implement the Model#labels method in terms of the template definition



71
72
73
# File 'lib/ruby_mvc/models/view_model_template.rb', line 71

def labels
  @labels
end

This method is used to declare a linked property for the model and make it available to the view



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/ruby_mvc/models/view_model_template.rb', line 131

def link(key, options = {})
  key = key.to_sym
  
  if false == options[:show]
    show = false
  else
    show = true
  end

  editable(key, false) if false == options[:editable]
  
  if show && !@options[:links].include?(key)
    @options[:links] << key
    l = options[:alias] || key.to_s.capitalize
    @link_labels << options.merge({ :key => key, :label => l })
  elsif !show
    @options[:links].delete(key)
    @link_labels.delete_if do |k|
      k[:key] == key
    end
  end
end


75
76
77
# File 'lib/ruby_mvc/models/view_model_template.rb', line 75

def link_labels
  @link_labels
end

#property(key, options = {}) ⇒ Object

This method is used to mark a property visible or not through the view.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ruby_mvc/models/view_model_template.rb', line 104

def property(key, options = {})
#      puts "options: #{options.inspect}"
  key = key.to_sym
  
  if false == options[:show]
    show = false
  else
    show = true
  end

  editable(key, false) if false == options[:editable]

  if show && !@options[:properties].include?(key)
    @options[:properties] << key
    l = options[:alias] || key.to_s.capitalize
    @labels << options.merge({ :key => key, :label => l })
  elsif !show
    @options[:properties].delete(key)
    @labels.delete_if do |k|
      k[:key] == key
    end
  end
end