Class: Linkage::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/linkage/dataset.rb

Overview

Delegator around Sequel::Dataset with some extra functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = args
    options ||= {}

    @table_name = table.to_sym
    @db = Sequel.connect(uri, options)
    @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_setObject (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_optionsObject

Returns the value of attribute linkage_options.



5
6
7
# File 'lib/linkage/dataset.rb', line 5

def linkage_options
  @linkage_options
end

#table_nameObject (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(new_options = {})
  new_linkage_options = {}
  new_obj_options = {}
  new_options.each_pair do |k, v|
    case k
    when :group_match
      new_linkage_options[k] = v
    else
      new_obj_options[k] = v
    end
  end
  new_obj = new_options[:new_obj]

  result = super()
  result.linkage_options = @linkage_options.merge(new_linkage_options)

  if new_obj
    result.obj = new_obj
  else
    result.obj = obj.clone(new_options)
  end

  result
end

#database_typeObject



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
  options = {: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, options.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).

Examples:

dataset.group_match(meta_object_1,
  {:meta_object => meta_object_2, :alias => :foo})


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

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

#objObject



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

#schemaObject



148
149
150
# File 'lib/linkage/dataset.rb', line 148

def schema
  @db.schema(@table_name)
end