From: Vladimir Zhukov Date: 2012-08-02T21:27:43+09:00 Subject: meta-programming question This is a multi-part message in MIME format. --------------040709010301040500000606 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit I'm working with sunspot gem and try create facets dynamically The problem with *facet(:total_view_count) do end* block I don't know how write code correctly. question is about ruby meta-programming here is my code: ||| ||Content.search do| | ||keywords(query)| | ||with(:community_ids, filtered_community_ids)| | ||with(:company_id, company.id)| | ||with(:content_type, content_type) if content_type| | ||order_by(*order) if order| | ||paginate(:page => page)| | ||# begin faceting area| | ||facet :topic_ids| | ||facet :community_ids , :only => filtered_community_ids| | ||facet(:total_view_count) do| | ||FacetsForSearch.view_facet.each do |range|| | ||row(range[:title]) do| | ||instance_eval do| | ||range[:code].call(self)| | ||end| | ||end| | ||end| | ||end| | ||end| || *FacetsForSearch.view_facet* - contain hashes with lambda here is the code |def view_facet| | ||[ {:id => 'view_1', :title => '0 to 100', :code => lambda{ with(:total_view_count, 0...100) } },| | ||{:id => 'view_2', :title => '101 to 200', :code => lambda{ with(:total_view_count, 101...200) } },| | ||{:id => 'view_3', :title => '201 to 300', :code => lambda{ with(:total_view_count, 201...300) } },| | ||{:id => 'view_4', :title => 'over 300', :code => lambda{ with(:total_view_count).greater_than(300) } },| | ||]| |end| Without instance eval code should look like: | ||facet(:total_view_count) do| | ||row('0_to_100') do| | ||with(:total_view_count, 0...100)| | ||end| | ||row('101_to_200') do| | ||with(:total_view_count, 101...200)| | ||end| | ||row('201_to_300') do| | ||with(:total_view_count, 201...300)| | ||end| | ||row('over_300') do| | ||with(:total_view_count).greater_than(300)| | ||end| | ||end| I try replace static code with lambda and can't figure out how to do this. Help! --------------040709010301040500000606 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

I'm working with sunspot gem and try create facets dynamically

The problem with facet(:total_view_count) do end block I don't know how write code correctly. question is about ruby meta-programming

here is my code:

  Content.search do
    keywords(query)
    with(:community_ids, filtered_community_ids)
    with(:company_id, company.id)
    with(:content_type, content_type) if content_type
    order_by(*order) if order
    paginate(:page => page)
    # begin faceting area
    facet :topic_ids
    facet :community_ids , :only => filtered_community_ids
    facet(:total_view_count) do 
      FacetsForSearch.view_facet.each do |range|
        row(range[:title]) do
          instance_eval do
            range[:code].call(self)
          end
        end
      end
    end
  end

FacetsForSearch.view_facet - contain hashes with lambda here is the code

def view_facet
  [ {:id => 'view_1', :title => '0 to 100',   :code => lambda{ with(:total_view_count, 0...100) } },
    {:id => 'view_2', :title => '101 to 200', :code => lambda{ with(:total_view_count, 101...200) } },
    {:id => 'view_3', :title => '201 to 300', :code => lambda{ with(:total_view_count, 201...300) } },
    {:id => 'view_4', :title => 'over 300',   :code => lambda{ with(:total_view_count).greater_than(300) } },
  ]
end

Without instance eval code should look like:

 facet(:total_view_count) do 
    row('0_to_100') do 
      with(:total_view_count, 0...100)
    end
    row('101_to_200') do 
      with(:total_view_count, 101...200) 
    end
    row('201_to_300') do 
      with(:total_view_count, 201...300) 
    end
    row('over_300') do 
      with(:total_view_count).greater_than(300) 
    end
  end

I try replace static code with lambda and can't figure out how to do this.
Help!


--------------040709010301040500000606--