InquiryLabs

Politics, Programming and Possibilities

Archive for January, 2007

Condition Builder 1.0 Released

I released version 0.5 of this Ruby on Rails plugin a few months ago and have since updated it to include ‘and’ and ‘or’ methods, as well as the ability to use sub-conditions.

Here are some code samples:

  Condition.block { |c|
    c.and "one", 1
    c.and "two", 2
    c.and { |d|
      d.or "three", 3
      d.or "four", 4
    }
  }

# => ["one = ? AND two = ? AND (three = ? OR four = ?)", 1, 2, 3, 4]

Since this uses Ruby’s blocks to construct a Rails condition array, you can use all of Ruby’s familiar logical constructs to achieve cleaner code in some cases. For example:

  Condition.block { |c|
    c.and "user_id", @user_id
    c.and { |d|
      d.or "admin", true
      d.or "joined_at", "<", Date.new(2007, 1, 1)
    } unless @dont_override_user_id
  }

# If @dont_override_user_id is false, then
# => ["user_id = ? AND (admin = ? OR joined_at < ?)", 10, true, Date.new(2007, 1, 1)]
#
# If @dont_override_user_id is true, then
# => ["user_id = ?", 10]

I use this directly in my ActiveRecord queries, like this:

  Book.find(:all,
      :include => {:content_pointer => :invitees},
      :conditions => Condition.block { |c|
        c.and "invitees.user_id", self.id
        c.and "content_pointers.created_by_id", self.id
        c.and "content_pointers.company_id", company.id if company
      })

Note: You may get some unexpected results if you try mixing ‘and’ and ‘or’ within the same block. The current implementation assumes that you will be consistent, and use a new block whenever you wish to switch logical operators. As an example, don’t do this:

Condition.block { |c|
  c.and "user_id", 1
  c.and "book_id", 2
  c.or "company_id", 3
}
# BAD
# Output will be: ["user_id = ? OR book_id = ? OR company_id = ?", 1, 2, 3]

# Do this instead:
Condition.block { |c|
  c.and "user_id", 1
  c.and { |d|
    d.or "book_id", 2
    d.or "company_id", 3
  }
}
# OK
# Output will be: ["user_id = ? AND (book_id = ? OR company_id = ?)", 1, 2, 3]

The Condition Builder plugin is available as a tar/gz file. Download it here.

This was a weird Ruby gotcha that my coworker, Paul Jones, discovered. Here’s a test class to show the point:

class T
  private
  def l=(val); puts val end
  def l; 1 end

  public
  def m=(val); self.l=val end
  def m; self.l end
end

irb> t.m=1
1
=> 1

irb> t.m
NoMethodError: private method `l' called for #
        from (irb):11:in `m'
        from (irb):18

Anyone know why this is the case?

Update: Perhaps I should clarify. I was expecting the “m=” method to work as it did, but I did not expect the NoMethodError when calling “m”. A better question might be, “Why the discrepency?”

Music Video from Remo Conscious on 9/11 Truth

So I hadn’t heard of Remo Conscious until discovering their new music video, “We Know“. It’s a bit of a relief to finally see some pop culture access to the questions surrounding 9/11. If you like hip-hop or just want to see their take, check it out.

  • 2 Comments
  • Filed under: Politics
  • Questions and Answers About This Blog

    As my thoughts have turned to economic and political things, I realize that some of you are wondering if you want to continue subscribing here. There’s a lot of opinionated stuff coming through the pipeline, and while I apologize for my ignorance in some areas, I feel no regret for having shared some of my journey as I’ve sought to answer questions that have troubled me.

    Some of you have expressed varying degrees of approval and disapproval. I understand that. We all see the world through our own constructs (to use a George Kelly term). I’ve had several comments about this site in general:

    Thanks for your blog. I come here for rails blog posts, but I find your political posts sobering and just as good to read.

    Any chance we could get separate feeds for your ruby/technical stuff and… everything else?

    I find your view disturbing. Are you Iranian by any chance?

    Given that this blog’s audience is on one hand growing in its diversity and on the other, more technically oriented in its foundation, I want to provide a means for those who are strictly interested in the technical blogs to feel comfortable. If you wish to subscribe only to technical categories, use these links:

    Ruby on Rails:
    http://blog.inquirylabs.com/category/ruby-on-rails/feed/

    Textmate:
    http://blog.inquirylabs.com/category/textmate/feed/

    Mac OS X:
    http://blog.inquirylabs.com/category/mac-os-x/feed/

    And of course, if you’re interested in all categories, use the main feed:

    http://blog.inquirylabs.com/feed/

    I hope all of you get from this site what you’re looking for. Thanks for visiting!

    Update: I’ve adjusted the settings over at rubycorner.com so that those of you who aggregate there will find the content more ruby-related :) (Thanks to Anon for pointing out the disjunct)

  • 2 Comments
  • Filed under: Uncategorized
  • Impeachment is in the Air

    I sent this off today:

    January 1, 2007

    Honorable Nancy Pelosi
    450 Golden Gate Ave., 14th Floor
    San Francisco, CA 94102

    Speaker Pelosi,

    The American people elected a Democratic majority to restore checks and balances, the rule of law, and our reputation as a law-abiding country in the world community. These cannot be accomplished unless President George W. Bush and Vice President Dick Cheney are impeached.

    After having carefully reviewed the evidence, I respectfully request that you begin the necessary work that will lead to the impeachment of President George W. Bush. The deception has gone far enough: we, the people, are now painfully aware of the propaganda that paved the way for our accepting this war. Please put an end to this treasonous administration.

    I ask you to consider the following high crimes and misdemeanors as a basis for this call for impeachment:

    1. Violating the United Nations Charter by launching an illegal “War of Aggression” against Iraq without cause, using fraud to sell the war to Congress and the public, misusing government funds to begin bombing without Congressional authorization, and subjecting our military personnel to unnecessary harm, debilitating injuries, and deaths.

    2. Violating U.S. and international law by authorizing the torture of thousands of captives, resulting in dozens of deaths, and keeping prisoners hidden from the International Committee of the Red Cross.

    3. Violating the Constitution by arbitrarily detaining Americans, legal residents, and non-Americans, without due process, without charge, and without access to counsel.

    4. Violating the Geneva Conventions by targeting civilians, journalists, hospitals, and ambulances, and using illegal weapons, including white phosphorous, depleted uranium, and a new type of napalm.

    5. Violating U.S. law and the Constitution through widespread wiretapping of the phone calls and emails of Americans without a warrant.

    6. Violating the Constitution by using “signing statements” to defy hundreds of laws passed by Congress.

    7. Violating U.S. and state law by obstructing honest elections in 2000, 2002, 2004, and 2006.

    8. Violating U.S. law by using paid propaganda and disinformation, selectively and misleadingly leaking classified information, and exposing the identity of a covert CIA operative working on sensitive WMD proliferation for political retribution.

    9. Subverting the Constitution and abusing Presidential power by asserting a “Unitary Executive Theory” giving unlimited powers to the President, by obstructing efforts by Congress and the Courts to review and restrict Presidential actions, and by promoting and signing legislation negating the Bill of Rights and the Writ of Habeas Corpus.

    10. Gross negligence in failing to assist New Orleans residents after Hurricane Katrina, in ignoring urgent warnings of an Al Qaeda attack prior to Sept. 11, 2001, and in increasing air pollution causing global warming.

    11. Failure to fully cooperate with 9/11 Commission and joint congressional inquiry, and refusal to comply with Freedom of Information Act in other areas as well.

    “The world can’t wait until 2008.”

    Respectfully,

    Duane Johnson

    For anyone who is interested, I pulled much of the content of the letter from democrats.com, although I am not particularly a Democrat myself. For a great resource on the whys and hows of impeachment, check out impeachbush.tv. Oh yeah, and here’s a fun song by Peter Tracy.

    Update: I’m also considering the Pledge to Impeach campaign, though I’ll have to think that one over a bit.

  • 8 Comments
  • Filed under: Politics