~ubuntu-branches/ubuntu/vivid/ruby-sequel/vivid

« back to all changes in this revision

Viewing changes to lib/sequel/extensions/set_overrides.rb

  • Committer: Package Import Robot
  • Author(s): Dmitry Borodaenko, Dmitry Borodaenko, Cédric Boutillier
  • Date: 2013-08-10 18:38:17 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20130810183817-iqanz804j32i5myi
Tags: 4.1.1-1
[ Dmitry Borodaenko ]
* New upstream release.
* Standards-Version upgraded to 3.9.4 (no changes).
* Added Build-Depend on ruby-sqlite3.

[ Cédric Boutillier ]
* debian/control: remove obsolete DM-Upload-Allowed flag.
* use canonical URI in Vcs-* fields.
* debian/copyright: use DEP5 copyright-format/1.0 official URL for Format
  field.
* Update debian/watch. Thanks Bart Martens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# The set_overrides extension adds the Dataset#set_overrides and
 
2
# Dataset#set_defaults methods which provide a crude way to
 
3
# control the values used in INSERT/UPDATE statements if a hash
 
4
# of values is passed to Dataset#insert or Dataset#update.
 
5
# It is only recommended to use this for backwards compatibility.
 
6
#
 
7
# You can load this extension into specific datasets:
 
8
#
 
9
#   ds = DB[:table]
 
10
#   ds.extension(:set_overrides)
 
11
#
 
12
# Or you can load it into all of a database's datasets, which
 
13
# is probably the desired behavior if you are using this extension:
 
14
#
 
15
#   DB.extension(:set_overrides)
 
16
 
 
17
module Sequel
 
18
  module SetOverrides
 
19
    Dataset::NON_SQL_OPTIONS.concat([:defaults, :overrides])
 
20
    Dataset.def_mutation_method(:set_defaults, :set_overrides, :module=>self)
 
21
 
 
22
    # Set overrides/defaults for insert hashes
 
23
    def insert_sql(*values)
 
24
      if values.size == 1 && (vals = values.first).is_a?(Hash)
 
25
        super(merge_defaults_overrides(vals))
 
26
      else
 
27
        super
 
28
      end
 
29
    end
 
30
 
 
31
    # Set the default values for insert and update statements.  The values hash passed
 
32
    # to insert or update are merged into this hash, so any values in the hash passed
 
33
    # to insert or update will override values passed to this method.  
 
34
    #
 
35
    #   DB[:items].set_defaults(:a=>'a', :c=>'c').insert(:a=>'d', :b=>'b')
 
36
    #   # INSERT INTO items (a, c, b) VALUES ('d', 'c', 'b')
 
37
    def set_defaults(hash)
 
38
      clone(:defaults=>(@opts[:defaults]||{}).merge(hash))
 
39
    end
 
40
 
 
41
    # Set values that override hash arguments given to insert and update statements.
 
42
    # This hash is merged into the hash provided to insert or update, so values
 
43
    # will override any values given in the insert/update hashes.
 
44
    #
 
45
    #   DB[:items].set_overrides(:a=>'a', :c=>'c').insert(:a=>'d', :b=>'b')
 
46
    #   # INSERT INTO items (a, c, b) VALUES ('a', 'c', 'b')
 
47
    def set_overrides(hash)
 
48
      clone(:overrides=>hash.merge(@opts[:overrides]||{}))
 
49
    end
 
50
 
 
51
    # Set overrides/defaults for update hashes
 
52
    def update_sql(values = {})
 
53
      if values.is_a?(Hash)
 
54
        super(merge_defaults_overrides(values))
 
55
      else
 
56
        super
 
57
      end
 
58
    end
 
59
 
 
60
    private
 
61
 
 
62
    # Return new hashe with merged defaults and overrides.
 
63
    def merge_defaults_overrides(vals)
 
64
      vals = @opts[:defaults].merge(vals) if @opts[:defaults]
 
65
      vals = vals.merge(@opts[:overrides]) if @opts[:overrides]
 
66
      vals
 
67
    end
 
68
  end
 
69
 
 
70
  Dataset.register_extension(:set_overrides, SetOverrides)
 
71
end