class Hash

  1. lib/sequel/extensions/core_extensions.rb
  2. lib/sequel/extensions/pg_hstore.rb
  3. lib/sequel/extensions/pg_json.rb
  4. show all
Superclass: Object

Sequel extends Hash to add methods to implement the SQL DSL.

Methods

Public Instance

  1. &
  2. case
  3. hstore
  4. pg_json
  5. pg_jsonb
  6. sql_expr
  7. sql_negate
  8. sql_or
  9. |
  10. ~

Public Instance methods

&(ce)

Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the conditions in this hash and the condition specified by the given argument.

{a: 1} & :b # SQL: ((a = 1) AND b)
{a: true} & ~:b # SQL: ((a IS TRUE) AND NOT b)
[show source]
    # File lib/sequel/extensions/core_extensions.rb
105 def &(ce)
106   ::Sequel::SQL::BooleanExpression.new(:AND, self, ce)
107 end
case(*args)

Return a Sequel::SQL::CaseExpression with this hash as the conditions and the given default value.

{{a: [2,3]}=>1}.case(0) # SQL: CASE WHEN (a IN (2, 3)) THEN 1 ELSE 0 END
{a: 1, b: 2}.case(:d, :c) # SQL: CASE c WHEN a THEN 1 WHEN b THEN 2 ELSE d END
[show source]
    # File lib/sequel/extensions/core_extensions.rb
133 def case(*args)
134   ::Sequel::SQL::CaseExpression.new(to_a, *args)
135 end
hstore()

Create a new HStore using the receiver as the input hash. Note that the HStore created will not use the receiver as the backing store, since it has to modify the hash. To get the new backing store, use:

hash.hstore.to_hash
[show source]
    # File lib/sequel/extensions/pg_hstore.rb
333 def hstore
334   Sequel::Postgres::HStore.new(self)
335 end
pg_json()

Return a Sequel::Postgres::JSONHash proxy to the receiver. This is mostly useful as a short cut for creating JSONHash objects that didn't come from the database.

[show source]
    # File lib/sequel/extensions/pg_json.rb
610 def pg_json
611   Sequel::Postgres::JSONHash.new(self)
612 end
pg_jsonb()

Return a Sequel::Postgres::JSONHash proxy to the receiver. This is mostly useful as a short cut for creating JSONHash objects that didn't come from the database.

[show source]
    # File lib/sequel/extensions/pg_json.rb
617 def pg_jsonb
618   Sequel::Postgres::JSONBHash.new(self)
619 end
sql_expr()

Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the conditions. Rarely do you need to call this explicitly, as Sequel generally assumes that hashes specify this type of condition.

{a: true}.sql_expr # SQL: (a IS TRUE)
{a: 1, b: [2, 3]}.sql_expr # SQL: ((a = 1) AND (b IN (2, 3)))
[show source]
    # File lib/sequel/extensions/core_extensions.rb
143 def sql_expr
144   ::Sequel::SQL::BooleanExpression.from_value_pairs(self)
145 end
sql_negate()

Return a Sequel::SQL::BooleanExpression created from this hash, matching none of the conditions.

{a: true}.sql_negate # SQL: (a IS NOT TRUE)
{a: 1, b: [2, 3]}.sql_negate # SQL: ((a != 1) AND (b NOT IN (2, 3)))
[show source]
    # File lib/sequel/extensions/core_extensions.rb
152 def sql_negate
153   ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :AND, true)
154 end
sql_or()

Return a Sequel::SQL::BooleanExpression created from this hash, matching any of the conditions.

{a: true}.sql_or # SQL: (a IS TRUE)
{a: 1, b: [2, 3]}.sql_or # SQL: ((a = 1) OR (b IN (2, 3)))
[show source]
    # File lib/sequel/extensions/core_extensions.rb
161 def sql_or
162   ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :OR)
163 end
|(ce)

Return a Sequel::SQL::BooleanExpression created from this hash, matching all of the conditions in this hash or the condition specified by the given argument.

{a: 1} | :b # SQL: ((a = 1) OR b)
{a: true} | ~:b # SQL: ((a IS TRUE) OR NOT b)
[show source]
    # File lib/sequel/extensions/core_extensions.rb
115 def |(ce)
116   ::Sequel::SQL::BooleanExpression.new(:OR, self, ce)
117 end
~()

Return a Sequel::SQL::BooleanExpression created from this hash, not matching all of the conditions.

~{a: true} # SQL: (a IS NOT TRUE)
~{a: 1, b: [2, 3]} # SQL: ((a != 1) OR (b NOT IN (2, 3)))
[show source]
    # File lib/sequel/extensions/core_extensions.rb
124 def ~
125   ::Sequel::SQL::BooleanExpression.from_value_pairs(self, :OR, true)
126 end