Class: IronNails::View::CommandBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ironnails/view/commands/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ CommandBuilder

Returns a new instance of CommandBuilder.



107
108
109
110
111
112
113
114
# File 'lib/ironnails/view/commands/command.rb', line 107

def initialize(controller)
  @controller = controller
  @command_mapping= {
          :event => EventCommand,
          :timed => TimedCommand,
          :behavior => BehaviorCommand
  }
end

Instance Attribute Details

#command_mappingObject

Returns the value of attribute command_mapping.



104
105
106
# File 'lib/ironnails/view/commands/command.rb', line 104

def command_mapping
  @command_mapping
end

#controllerObject

Returns the value of attribute controller.



102
103
104
# File 'lib/ironnails/view/commands/command.rb', line 102

def controller
  @controller
end

Instance Method Details

#create_command_from(definition) ⇒ Object



200
201
202
# File 'lib/ironnails/view/commands/command.rb', line 200

def create_command_from(definition)
  command_mapping[definition[:type]||:behavior].new definition
end

#generate_command_collection_from_normalized_definitions(definitions) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/ironnails/view/commands/command.rb', line 191

def generate_command_collection_from_normalized_definitions(definitions)
  commands = CommandCollection.new
  definitions.each do |name, cmd_def|
    cmd = create_command_from(cmd_def.merge({ :view_model => view_model, :name => name, :controller => controller.controller_name }))
    commands << cmd
  end if definitions.is_a?(Hash)
  commands
end

#generate_for(cmd_def) ⇒ Object

Given a set of command_definitions it will generate a collection of Command objects for the view model



118
119
120
121
122
# File 'lib/ironnails/view/commands/command.rb', line 118

def generate_for(cmd_def)
  norm = normalize_command_definitions(cmd_def)
  cmds = generate_command_collection_from_normalized_definitions norm
  cmds
end

#normalize_command_definition(name, options) ⇒ Object

Generates a command definition for our view model. When it can’t find a key :action in the options hash for the view_action it will default to using the name as the command as the connected option. It will generate a series of commands for items that have more than one trigger



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ironnails/view/commands/command.rb', line 143

def normalize_command_definition(name, options)
  mode = options[:mode]
  act = options[:action]||name
  action = act
  action = controller.method(act) if act.is_a?(Symbol) || act.is_a?(String)
  if options.has_key?(:triggers) && !options[:triggers].nil?
    triggers = options[:triggers]

    cmd_def =
            if  triggers.is_a?(String) || triggers.is_a?(Symbol)
              {
                      :element => triggers,
                      :event   => :click,
                      :action  => action,
                      :mode    => mode,
                      :type    => :event
              }
            elsif triggers.is_a?(Hash)
              {:action => action, :mode => mode, :type => :event }.merge triggers
            elsif triggers.is_a?(Array)
              defs = []
              triggers.each do |trig|
                trig = { :element => trig, :event => :click } unless trig.is_a? Hash
                trig[:event] = :click unless trig.has_key? :event
                defs << { :action => action, :mode => mode, :type => :event }.merge(trig)
              end
              defs
            end
    cmd_def
  else
    exec = options[:execute]
    execute = exec
    execute = controller.method(exec) if exec.is_a?(Symbol) || exec.is_a?(String)
    callback = options[:callback]
    callback = controller.method(callback) if callback.is_a?(Symbol) || callback.is_a?(String)
    controller_action, controller_condition = execute || action, options[:condition]
    res = {
            :action => controller_action,
            :condition => controller_condition,
            :mode => mode,
            :callback => callback,
            :type => options[:type] || :behavior
    }
    res[:interval] = options[:interval] if options.key? :interval
    res
  end
end

#normalize_command_definitions(definitions) ⇒ Object

Generates the command definitions for our view model.



129
130
131
132
133
134
135
136
137
# File 'lib/ironnails/view/commands/command.rb', line 129

def normalize_command_definitions(definitions)
  command_definitions = {}

  definitions.each do |k, v|
    command_definitions[k] = normalize_command_definition(k, v)
  end unless definitions.nil?

  command_definitions
end

#view_modelObject



124
125
126
# File 'lib/ironnails/view/commands/command.rb', line 124

def view_model
  WpfApplication.current.nails_engine
end