Class: Ingestor::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/ingestor/dsl.rb

Defined Under Namespace

Classes: InvalidBlockSpecification

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Dsl

Returns a new instance of Dsl.



4
5
6
7
8
9
10
11
12
# File 'lib/ingestor/dsl.rb', line 4

def initialize(*args)
  @options = {}
  
  includes_header(false)
  compressed(false)
  parser :plain_text
  parser_options({})
  working_directory '/tmp/ingestor'
end

Instance Method Details

#after(&block) ⇒ Object

Processing performed on the record AFTER being passing to [processor]



88
89
90
91
92
93
# File 'lib/ingestor/dsl.rb', line 88

def after(&block)
  if !block_given? || block.arity != 1
    raise InvalidBlockSpecification, "after proc should have an arity of 1 (~ActiveRecord: record)"
  end      
  @options[:after] = block
end

#before(&block) ⇒ Object

Processing performed on the attributes before being passed to [finder]



80
81
82
83
84
85
# File 'lib/ingestor/dsl.rb', line 80

def before(&block)
  if !block_given? || block.arity != 1
    raise InvalidBlockSpecification, "before proc should have an arity of 1 (Array: values)"
  end      
  @options[:before] = block
end

#buildObject



106
107
108
# File 'lib/ingestor/dsl.rb', line 106

def build
  @proxy = Ingestor::Proxy.new(@file, @options)
end

#compressed(v) ⇒ Object

if the remote file is compressed, this will decompress it.



47
# File 'lib/ingestor/dsl.rb', line 47

def compressed(v);                @options[:compressed] = v;end

#file=(v) ⇒ Object

the file to retrieve



19
# File 'lib/ingestor/dsl.rb', line 19

def file=(v);                     @file = v;end

#finder(&block) ⇒ Object

Takes an array of values (a line/entry/node) and should return an ActiveModel type object

You do not need to set the attributes here, than is handled by #processor

update or create: finder{|attrs| User.where(id: attrs).first || User.new}

create: finder{|attrs| User.new}



60
61
62
63
64
65
# File 'lib/ingestor/dsl.rb', line 60

def finder(&block)
  if !block_given? || block.arity != 1
    raise InvalidBlockSpecification, "finder proc should have an arity of 1 (Array: values)"
  end      
  @options[:finder] = block
end

#includes_header(v) ⇒ Object

skip first line?



44
# File 'lib/ingestor/dsl.rb', line 44

def includes_header(v);           @options[:includes_header] = v;end

#map_attributes(&block) ⇒ Object

This method is called for each entry in the document Block should receive ‘values’ (array for plain text, hash for all others) and return a hash

of ActiveModel attribute name to value


99
100
101
102
103
104
# File 'lib/ingestor/dsl.rb', line 99

def map_attributes(&block)
  if !block_given? || block.arity != 1
    raise InvalidBlockSpecification, "after proc should have an arity of 1 (Hash|Array: values)"
  end      
  @options[:map_attributes] = block
end

#optionsObject



14
15
16
# File 'lib/ingestor/dsl.rb', line 14

def options
  @options
end

#parser(v) ⇒ Object

set parser, default :plain_text



34
35
36
# File 'lib/ingestor/dsl.rb', line 34

def parser(v)
  @options[:parser] = v
end

#parser_options(v) ⇒ Object

set options



39
40
41
# File 'lib/ingestor/dsl.rb', line 39

def parser_options(v)
  @options[:parser_options] = v
end

#processor(&block) ⇒ Object

How to process an entry in a file. The default takes the values and passes them to the record returned

by your finder and calls update attributes

Proc should receive two parameters

attrs - Hash, mapped attributs for this record
record - ~ActiveRecord:Base, record found by #finder


72
73
74
75
76
77
# File 'lib/ingestor/dsl.rb', line 72

def processor(&block)
  if !block_given? || block.arity != 2
    raise InvalidBlockSpecification, "processor proc should have an arity of 2 (Array: values, ~ActiveRecord: record)"
  end      
  @options[:processor] = block
end

#proxyObject



110
111
112
# File 'lib/ingestor/dsl.rb', line 110

def proxy
  @proxy
end

#sample(v) ⇒ Object

When set to true sample will get the file and print out the first set of raw values



23
24
25
# File 'lib/ingestor/dsl.rb', line 23

def sample(v)
  @options[:sample] = v
end

#working_directory(v) ⇒ Object

where the file will be moved locally for processing when it is compressed or a remote file. local files will not use working directory



29
30
31
# File 'lib/ingestor/dsl.rb', line 29

def working_directory(v)
  @options[:working_directory] = v
end