Class: Linkage::Dataset
- Inherits:
-
Object
- Object
- Linkage::Dataset
- Defined in:
- lib/linkage/dataset.rb
Overview
Delegator around Sequel::Dataset with some extra functionality.
Instance Attribute Summary collapse
-
#field_set ⇒ Object
readonly
Returns the value of attribute field_set.
-
#linkage_options ⇒ Object
Returns the value of attribute linkage_options.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
Instance Method Summary collapse
- #clone(new_options = {}) ⇒ Object
- #database_type ⇒ Object
- #dataset_for_group(group) ⇒ Object
- #each_group(min = 2) ⇒ Object
- #group_by_matches(raw = true) ⇒ Object
-
#group_match(*args) ⇒ Object
Set objects to use for group matching.
-
#group_match_more(*args) ⇒ Object
Add additional objects to use for group matching.
-
#initialize(*args) ⇒ Dataset
constructor
A new instance of Dataset.
-
#link_with(dataset, &block) ⇒ Linkage::Configuration
Setup a linkage with another dataset.
- #obj ⇒ Object
- #obj=(value) ⇒ Object
- #schema ⇒ Object
Constructor Details
#initialize(*args) ⇒ Dataset
Returns a new instance of Dataset.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/linkage/dataset.rb', line 7 def initialize(*args) if args.length == 1 @dataset = args[0] @db = @dataset.db @table_name = @dataset.first_source_table if !@db.kind_of?(Sequel::Collation) @db.extend(Sequel::Collation) end else uri, table, = args ||= {} @table_name = table.to_sym @db = Sequel.connect(uri, ) @db.extend(Sequel::Collation) @dataset = @db[@table_name] end @field_set = FieldSet.new(self) @linkage_options = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
169 170 171 172 173 174 175 176 |
# File 'lib/linkage/dataset.rb', line 169 def method_missing(name, *args, &block) result = @dataset.send(name, *args, &block) if result.kind_of?(Sequel::Dataset) new_obj = result result = clone(:new_obj => result) end result end |
Instance Attribute Details
#field_set ⇒ Object (readonly)
Returns the value of attribute field_set.
4 5 6 |
# File 'lib/linkage/dataset.rb', line 4 def field_set @field_set end |
#linkage_options ⇒ Object
Returns the value of attribute linkage_options.
5 6 7 |
# File 'lib/linkage/dataset.rb', line 5 def @linkage_options end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
4 5 6 |
# File 'lib/linkage/dataset.rb', line 4 def table_name @table_name end |
Instance Method Details
#clone(new_options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/linkage/dataset.rb', line 82 def clone( = {}) = {} = {} .each_pair do |k, v| case k when :group_match [k] = v else [k] = v end end new_obj = [:new_obj] result = super() result. = @linkage_options.merge() if new_obj result.obj = new_obj else result.obj = obj.clone() end result end |
#database_type ⇒ Object
46 47 48 |
# File 'lib/linkage/dataset.rb', line 46 def database_type @db.database_type end |
#dataset_for_group(group) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/linkage/dataset.rb', line 127 def dataset_for_group(group) filters = [] group_match = @linkage_options[:group_match] || [] group.values.each_pair do |key, value| # find a matched expression with this alias found = false group_match.each do |m| expr = m[:meta_object].to_expr if (m[:alias] && m[:alias] == key) || expr == key found = true filters << {expr => value} break end end if !found raise "this dataset isn't compatible with the given group" end end filter(*filters) end |
#each_group(min = 2) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/linkage/dataset.rb', line 107 def each_group(min = 2) group_match = @linkage_options[:group_match] || [] ruby_types = group_match.inject({}) do |hsh, m| key = m[:alias] || m[:meta_object].to_expr hsh[key] = m[:meta_object].ruby_type hsh end = {:database_type => database_type, :ruby_types => ruby_types } @dataset.group_and_count(*match_expressions).having{count >= min}.each do |row| count = row.delete(:count) group = Group.new(row, .merge(:count => count)) yield group end end |
#group_by_matches(raw = true) ⇒ Object
122 123 124 125 |
# File 'lib/linkage/dataset.rb', line 122 def group_by_matches(raw = true) expr = raw ? raw_match_expressions : match_expressions group(*expr) end |
#group_match(*args) ⇒ Object
Set objects to use for group matching. Accepts either MetaObject or a hash with options (valid options are :meta_object, :alias, and :cast).
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/linkage/dataset.rb', line 56 def group_match(*args) args.collect! do |arg| case arg when Linkage::MetaObject { :meta_object => arg } when Hash if !arg.has_key?(:meta_object) raise ArgumentError, "Invalid option hash, missing :meta_object key" end (arg.keys - [:meta_object, :alias, :cast]).each do |invalid_key| warn "Invalid key in option hash: #{invalid_key}" end arg else raise ArgumentError, "expected Hash or MetaObject, got #{arg.class}" end end clone(:group_match => args) end |
#group_match_more(*args) ⇒ Object
Add additional objects to use for group matching.
77 78 79 80 |
# File 'lib/linkage/dataset.rb', line 77 def group_match_more(*args) args = @linkage_options[:group_match] + args if @linkage_options[:group_match] group_match(*args) end |
#link_with(dataset, &block) ⇒ Linkage::Configuration
Setup a linkage with another dataset
40 41 42 43 44 |
# File 'lib/linkage/dataset.rb', line 40 def link_with(dataset, &block) conf = Configuration.new(self, dataset) conf.configure(&block) conf end |
#obj ⇒ Object
29 30 31 |
# File 'lib/linkage/dataset.rb', line 29 def obj @dataset end |
#obj=(value) ⇒ Object
33 34 35 |
# File 'lib/linkage/dataset.rb', line 33 def obj=(value) @dataset = value end |
#schema ⇒ Object
148 149 150 |
# File 'lib/linkage/dataset.rb', line 148 def schema @db.schema(@table_name) end |