Class: Embulk::DataSource

Inherits:
Hash
  • Object
show all
Includes:
Impl::IndifferentAccess
Defined in:
embulk-core/src/main/ruby/embulk/data_source.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Impl::IndifferentAccess

#convert!, #convert_key, included, #indifferent_access?, #indifferent_default, #indifferent_delete, #indifferent_fetch, #indifferent_key?, #indifferent_replace, #indifferent_update, #indifferent_value, #indifferent_values_at, #indifferent_writer, inject, inject!

Constructor Details

#initialize(hash = {}, default = nil, &block) ⇒ DataSource

Returns a new instance of DataSource



130
131
132
133
134
135
136
137
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 130

def initialize(hash={}, default=nil, &block)
  if default.nil?
    super(&block)
  else
    super(default)
  end
  hash.each {|key,value| self[key] = value }
end

Class Method Details

.from_java(java_data_source_impl) ⇒ Object



195
196
197
198
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 195

def self.from_java(java_data_source_impl)
  json = java_data_source_impl.toString
  new.merge!(JSON.parse(json))
end

.from_ruby_hash(hash) ⇒ Object



200
201
202
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 200

def self.from_ruby_hash(hash)
  new.merge!(hash)
end

Instance Method Details

#load_config(task_type) ⇒ Object



209
210
211
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 209

def load_config(task_type)
  Java::Injected::ModelManager.readObjectWithConfigSerDe(task_type.java_class, to_json.to_java)
end

#load_task(task_type) ⇒ Object



213
214
215
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 213

def load_task(task_type)
  Java::Injected::ModelManager.readObject(task_type.java_class, to_json.to_java)
end

#param(key, type, options = {}) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 139

def param(key, type, options={})
  if self.has_key?(key)
    v = self[key]
    value =
      case type
      when :integer
        begin
          Integer(v)
        rescue => e
          raise ConfigError.new e
        end
      when :float
        begin
          Float(v)
        rescue => e
          raise ConfigError.new e
        end
      when :string
        begin
          String(v).dup
        rescue => e
          raise ConfigError.new e
        end
      when :bool
        begin
          !!v  # TODO validation
        rescue => e
          raise ConfigError.new e
        end
      when :hash
        raise ConfigError.new "Invalid value for :hash" unless v.is_a?(Hash)
        DataSource.new.merge!(v)
      when :array
        raise ConfigError.new "Invalid value for :array" unless v.is_a?(Array)
        v.dup
      else
        unless type.respond_to?(:load)
          raise ArgumentError, "Unknown type #{type.to_s.dump}"
        end
        begin
          type.load(v)
        rescue => e
          raise ConfigError.new e
        end
      end

  elsif options.has_key?(:default)
    value = options[:default]

  else
    raise ConfigError.new "Required field #{key.to_s.dump} is not set"
  end

  return value
end

#to_javaObject



204
205
206
207
# File 'embulk-core/src/main/ruby/embulk/data_source.rb', line 204

def to_java
  json = to_json
  Java::Injected::ModelManager.readObject(Java::DataSourceImpl.java_class, json.to_java)
end