将实例变量保留为实例变量,而不是其内容

时间:2012-02-23 23:24:23

标签: ruby-on-rails ruby

我对这个问题只有一个模糊的想法,所以问题是必要的:

我有一组值,我定期在我的rails控制器中传递给一个小部件,该页面与页面之间略有不同,而不是我传递给它的部分。这对于每个控制器都开始变得笨拙,所以我添加了一个小类来帮助连接这个过程(下面的基本起始要点)。

#return dashcontroller hash from more succinct cues
module DashControl
  class DashControl

    attr_accessor :title, :instance, :actions 

    def initialize(title='default title', instance='default instance', actions={})
      @title = title
      @instance = instance
      initialize_actions(actions)
    end

    def initialize_actions(actions)
      actions.kind_of?(Hash) ? @actions = actions : initialize_tag(actions)
    end

    def initialize_tag(tag)
      case tag
      when :manage_default
        @actions = {:statusactions => [], 
                    :formactions => [ ['#tabaccount', 'addaccount'],
                                      ['#tabuser', 'addusers'],
                                      ['#tabadd','adddomain'] ],
                    :linkactions => [ [] ],
                    :filteractions => [ [] ] }
      when :none 
        @actions = {}
      #when 
      #  @actions = {}
      else
        @actions = @actions
      end
    end  


    def dashcontroller
      {:title => @title, :instance => @instance, :actions => @actions }
    end

  end
end

所以基本上我只需要传递一个this.dashcontroller的实例,我得到了我需要的哈希,在我的控制器中没有那么多的混乱。问题出在@instance变量上。我想传递我正在使用的实例,例如@book@account等,并将其显示为@book@account等。相反,我会将{1 { }}。对我来说似乎不像我之前使用的那样:instance => (contents of that instance),然后使用它,但查看它可能不会在窗口小部件中产生任何差异,因为我处理事务并处理我的代码fu。

基本上我的问题是如何通过像这样的类推送一个实例变量,并且在它进入时仍然具有accessibile,而不必在另一侧进行任何后空翻和转换。可能有更好的方法,但这就是我现在正在使用的方式。

编辑:伪代码

@account

我想我可以使用它,就像我说的更多是我理解事物如何流动而不是实际的错误或任何困难的问题。我不想在另一端用实例的东西做更多的体操,虽然内容在那里,这就是我真正需要的,我只需要一些思考它就可以减少一些混乱。我真的需要改进我通过这个发送的内容,或者使用它来进一步完善我发送的内容,这是现在要拿走的底线课程。

编辑:

我最终折腾了这个,但这是一次学习经历...我回去了小部件,我知道的比我最初设置小部件时更多,所以我已经能够设置它只采取实例变量和bootstrap它需要什么,而不添加另一个类,清理我的控制器并将大量的东西交回到我怀疑应该/可能已经开始的小部件。

1 个答案:

答案 0 :(得分:0)

根据您的代码和示例,这适合:

# No need to put a class in a namespace of the same name, just make the module a class
# Also, if you inherit from a struct, it can save you a lot of typing. It defines the setters and getters for you.
class DashControl < Struct.new(:title, :instance, :actions)

  # since it looks like you always access it the same way, create a class method
  # which does this initialization and invocation
  def self.for(*args)
    new(*args).dashcontroller
  end

  def initialize(title='default title', instance='default instance', actions=:none)
    # here, we can use our own defaults and normalization and pass the results up to the struct
    super title, instance, normalize(actions)
  end

  # didn't make sense to call this initialize_tag, as it was initializing actions
  # also there was already an initialize actions method which just checked for the case of a hash
  # but then elsewhere you checked for other things. Better to just put it all in one method and return it
  # (then you aren't setting it every time you want to ask it to calculate that value)
  # also using guard clauses (the if statements that return early) instead of the case, as they are easier to understand
  def normalize(actions)
    return Hash.new if actions == :none
    return actions unless actions == :manage_default
    default_actions
  end  

  # the value of default_actions is complicated and noisy, separate it out to its own method
  # this prevents it from cluttering the code around it, and also allows us to access,
  # and to do this without the side effects of setting values.
  def default_actions
    { :statusactions => [], 
      :formactions   => [ ['#tabaccount', 'addaccount'],
                          ['#tabuser', 'addusers'],
                          ['#tabadd','adddomain'] ],
      :linkactions   => [ [] ],
      :filteractions => [ [] ] }
  end

  # use the getters instead of the ivars (I consider this a generally best practice -- and you could have
  # done it before, since you declared the attr_accessor, even though I'm accessing it through the struct)
  def dashcontroller
    {:title => title, :instance => instance, :actions => actions }
  end
end

DashControl.for                                           # => {:title=>"default title", :instance=>"default instance", :actions=>{}}
DashControl.for('Catchy Title', '@book', :none)           # => {:title=>"Catchy Title", :instance=>"@book", :actions=>{}}
DashControl.for('Catchy Title', '@book', :manage_default) # => {:title=>"Catchy Title", :instance=>"@book", :actions=>{:statusactions=>[], :formactions=>[["#tabaccount", "addaccount"], ["#tabuser", "addusers"], ["#tabadd", "adddomain"]], :linkactions=>[[]], :filteractions=>[[]]}}
DashControl.for('Catchy Title', '@book', a: 'b')          # => {:title=>"Catchy Title", :instance=>"@book", :actions=>{:a=>"b"}}
DashControl.for('Catchy Title', '@book', 123)             # => {:title=>"Catchy Title", :instance=>"@book", :actions=>123}