Class: Remont::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/remont/schema.rb

Constant Summary collapse

MISSING_PROCESSING_STATUS_ATTR =
"Processing status attribute isn't configured".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • opts (Hash) (defaults to: {})
  • block (Proc)
  • [Class] (Hash)

    a customizable set of options

  • [Proc] (Hash)

    a customizable set of options

  • [String, (Hash)

    a customizable set of options



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/remont/schema.rb', line 16

def initialize(opts = {}, &block)
  @model = opts.fetch(:model)
  @model_scope = opts.fetch(:scope, default_scope)
  @without_processed_scope = default_scope
  @process_timestamp_attribute = opts.fetch(:process_timestamp_attribute, Remont.config.process_timestamp_attribute)
  @before_cb = nil
  @after_cb = nil
  @attributes = []

  instance_eval(&block)
end

Instance Attribute Details

#attributesArray<Remont::Attribute> (readonly)

Returns:



6
7
8
# File 'lib/remont/schema.rb', line 6

def attributes
  @attributes
end

#process_timestamp_attributeNil, ... (readonly)

Returns:

  • (Nil, String, Symbol)


9
10
11
# File 'lib/remont/schema.rb', line 9

def process_timestamp_attribute
  @process_timestamp_attribute
end

Instance Method Details

#after(&block) ⇒ Remont::Schema

Parameters:

  • block (Proc)

Returns:



74
75
76
77
78
# File 'lib/remont/schema.rb', line 74

def after(&block)
  @after_cb = block

  self
end

#attributeRemont::Schema

Parameters:

  • name (Symbol)
  • opts (Hash)
  • processor (Proc)
  • [#call] (Hash)

    a customizable set of options

Returns:



58
59
60
61
62
# File 'lib/remont/schema.rb', line 58

def attribute(...)
  @attributes << Attribute.new(...)

  self
end

#before(&block) ⇒ Remont::Schema

Parameters:

  • block (Proc)

Returns:



66
67
68
69
70
# File 'lib/remont/schema.rb', line 66

def before(&block)
  @before_cb = block

  self
end

#process!Any

Returns:

  • (Any)


81
82
83
# File 'lib/remont/schema.rb', line 81

def process!
  records_for_processing.find_each { |record| processor.process!(record) }
end

#scope(&scope) ⇒ Remont::Schema

Parameters:

  • scope (Proc)

Returns:



47
48
49
50
51
# File 'lib/remont/schema.rb', line 47

def scope(&scope)
  @model_scope = scope

  self
end

#with_process_timestamp_attribute(attr_name) ⇒ Remont::Schema

Parameters:

  • attr_name (String, Symbol)

Returns:



39
40
41
42
43
# File 'lib/remont/schema.rb', line 39

def with_process_timestamp_attribute(attr_name)
  @process_timestamp_attribute = attr_name

  self
end

#without_processedRemont::Schema



29
30
31
32
33
34
35
# File 'lib/remont/schema.rb', line 29

def without_processed
  raise MISSING_PROCESSING_STATUS_ATTR if @process_timestamp_attribute.nil?

  @without_processed_scope = proc { |scope| scope.where(process_timestamp_attribute => nil) }

  self
end