Sha256: 57e77f4b328f3ecf9076c4dec6d887ad5250de357f99f461dceffc126111d573

Contents?: true

Size: 1.67 KB

Versions: 5

Compression:

Stored size: 1.67 KB

Contents

require 'volt/controllers/reactive_accessors'

class ModelController
  include ReactiveAccessors

  def self.model(val)
    @default_model = val
  end

  # Sets the current model on this controller
  def model=(val)
    # Start with a nil reactive value.
    @model ||= ReactiveValue.new(Proc.new { nil })

    if Symbol === val || String === val
      collections = [:page, :store, :params, :controller]
      if collections.include?(val.to_sym)
        @model.cur = self.send(val).cur
      else
        raise "#{val} is not the name of a valid model, choose from: #{collections.join(', ')}"
      end
    elsif val
      @model.cur = val.cur
    else
      raise "model can not be #{val.inspect}"
    end
  end

  def model
    @model
  end

  def self.new(*args, &block)
    inst = self.allocate

    inst.model = (@default_model || :controller)

    inst.initialize(*args, &block)

    return inst
  end

  def initialize(*args)


    # Set the instance variable to match any passed in arguments
    if args.size > 0
      args[0].each_pair do |key, value|
        instance_variable_set(:"@#{key}", value)
      end
    end
  end

  # Change the url params, similar to redirecting to a new url
  def go(url)
    self.url.parse(url)
  end

  def page
    $page.page
  end

  def paged
    $page.page
  end

  def store
    $page.store
  end

  def flash
    $page.flash
  end

  def params
    $page.params
  end

  def url
    $page.url
  end

  def channel
    $page.channel
  end

  def tasks
    $page.tasks
  end

  def controller
    @controller ||= ReactiveValue.new(Model.new)
  end

  def method_missing(method_name, *args, &block)
    return @model.send(method_name, *args, &block)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
volt-0.7.18 lib/volt/controllers/model_controller.rb
volt-0.7.17 lib/volt/controllers/model_controller.rb
volt-0.7.16 lib/volt/controllers/model_controller.rb
volt-0.7.15 lib/volt/controllers/model_controller.rb
volt-0.7.14 lib/volt/controllers/model_controller.rb