Class: Cmtool::Menu::Register

Inherits:
Object
  • Object
show all
Defined in:
lib/cmtool/menu.rb

Overview

Register class on which the register blocks are executed. Should hold all possible DSL logic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegister



172
173
174
# File 'lib/cmtool/menu.rb', line 172

def initialize
  @items = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



201
202
203
# File 'lib/cmtool/menu.rb', line 201

def method_missing(*args)
  @items.send(*args).to_a
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



170
171
172
# File 'lib/cmtool/menu.rb', line 170

def items
  @items
end

Instance Method Details

#after(label, &block) ⇒ Object



214
215
216
217
218
219
220
221
222
# File 'lib/cmtool/menu.rb', line 214

def after(label, &block)
  @before = []
  @before << items.shift while items.first && items.first.options[:label] != label
  @before << items.shift if items.any?
  @after = @items
  @items = []
  instance_eval(&block)
  @items = @before + @items + @after
end

#append_to(label, options = {}, &block) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cmtool/menu.rb', line 224

def append_to(label, options = {}, &block)
  item = items.find{|i| i.options[:label] == label}
  if item
    register = item.instance_variable_get('@register')
    if register
      register.instance_variable_get('@items') << Cmtool::Menu::Divider.new unless options[:skip_divider].present?
      register.instance_eval(&block)
    end
  else
    instance_eval(&block)
  end
end

#before(label, &block) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/cmtool/menu.rb', line 205

def before(label, &block)
  @before = []
  @before << items.shift while items.first && items.first.options[:label] != label
  @after = @items
  @items = []
  instance_eval(&block)
  @items = @before + @items + @after
end


193
194
195
# File 'lib/cmtool/menu.rb', line 193

def engine_link(engine, options = {})
  @items << EngineLink.new(engine, options)
end

#group(options = {}, &block) ⇒ Object



180
181
182
# File 'lib/cmtool/menu.rb', line 180

def group(options = {}, &block)
  @items << Group.new(options, &block)
end


189
190
191
# File 'lib/cmtool/menu.rb', line 189

def resource_link(resource, options = {})
  @items << ResourceLink.new(resource, options)
end


197
198
199
# File 'lib/cmtool/menu.rb', line 197

def resource_links
  @items.select{|i| i.is_a?(ResourceLink)}
end

#t(*args) ⇒ Object



176
177
178
# File 'lib/cmtool/menu.rb', line 176

def t(*args)
  -> { I18n.t(*args) }
end

#title(*args) ⇒ Object



184
185
186
187
# File 'lib/cmtool/menu.rb', line 184

def title(*args)
  return @title if args.empty?
  @title = args.first
end