Class: NexusSeed::Builder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nexus_seed/builder/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/nexus_seed/builder/base.rb', line 7

def initialize(options = {})
  @options = options
  @find_by_params_field = options[:find_by_params_field].nil? ? :find_by_params : options[:find_by_params_field]
end

Instance Method Details

#after_save(_instance, _params = nil) ⇒ Object



88
89
90
# File 'lib/nexus_seed/builder/base.rb', line 88

def after_save(_instance, _params = nil)
  {}
end

#before_save(_instance, _params = nil) ⇒ Object



84
85
86
# File 'lib/nexus_seed/builder/base.rb', line 84

def before_save(_instance, _params = nil)
  {}
end

#build(params) ⇒ Object



22
23
24
# File 'lib/nexus_seed/builder/base.rb', line 22

def build(params)
  build_raw(params)[:domain]
end

#build_raw(params) ⇒ Object

Raises:

  • (StandardError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nexus_seed/builder/base.rb', line 26

def build_raw(params)
  # merge defaults with params
  default_params = defaults(params)
  merged_params = if default_params.nil?
    params
  else
    default_params.merge(params)
  end
  final_params = override_params(merged_params)

  instance = klass.new(final_params)

  # set the custom find_by_params if provided
  @find_by_params = if @options.key?(:find_by_params)
    @options[:find_by_params]
  else
    find_by_params(instance, final_params)
  end

  raise StandardError, "Error: find_by_params must not be nil" if @find_by_params.nil?

  before_data = before_save(instance, final_params)

  find_by_query = if @find_by_params.is_a?(Hash)
    @find_by_params
  else
    @find_by_params = @find_by_params.is_a?(Array) ? @find_by_params : [@find_by_params]

    query_hash = {}

    @find_by_params.each do |e|
      query_hash[e] = instance[e]
    end

    query_hash
  end

  existing = klass.find_by(find_by_query)

  result = if existing.nil?
    instance.save!
    instance = klass.find_by(find_by_query)
    instance
  else
    existing
  end

  NexusSeed::Builder.add_seed(result) if ENV['NEXUS_SEED_DESTROY'] == 'true'

  after_data = after_save(result, final_params)

  {
    domain: result,
    **before_data,
    **after_data,
  }
end

#defaults(params = {}) ⇒ Object



12
# File 'lib/nexus_seed/builder/base.rb', line 12

def defaults(params = {}); end

#find_by_params(instance = nil, params = nil) ⇒ Object



20
# File 'lib/nexus_seed/builder/base.rb', line 20

def find_by_params(instance = nil, params = nil); end

#model_classObject



18
# File 'lib/nexus_seed/builder/base.rb', line 18

def model_class; end

#override_params(params = {}) ⇒ Object



14
15
16
# File 'lib/nexus_seed/builder/base.rb', line 14

def override_params(params = {})
  params
end