Class: RubyMVC::Models::ViewModelTemplate

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.



52
53
54
55
56
57
58
# File 'lib/ruby_mvc/models/view_model_template.rb', line 52

def initialize(options = {}, &block)
  @options = options
  @labels = []
  @options[:editable] ||= {}
  @options[:properties] ||= []
  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



130
131
132
133
134
135
136
# File 'lib/ruby_mvc/models/view_model_template.rb', line 130

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.



46
47
48
# File 'lib/ruby_mvc/models/view_model_template.rb', line 46

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.



125
126
127
128
# File 'lib/ruby_mvc/models/view_model_template.rb', line 125

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



89
90
91
# File 'lib/ruby_mvc/models/view_model_template.rb', line 89

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)


76
77
78
79
80
81
82
83
# File 'lib/ruby_mvc/models/view_model_template.rb', line 76

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

#keysObject



60
61
62
# File 'lib/ruby_mvc/models/view_model_template.rb', line 60

def keys
  @options[:properties]
end

#labelsObject

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



67
68
69
# File 'lib/ruby_mvc/models/view_model_template.rb', line 67

def labels
  @labels
end

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

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby_mvc/models/view_model_template.rb', line 96

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