Class: DataWorks::Works

Inherits:
Object
  • Object
show all
Includes:
Visualization
Defined in:
lib/data_works/works.rb

Instance Method Summary collapse

Methods included from Visualization

#visualize

Constructor Details

#initializeWorks

Returns a new instance of Works.



6
7
8
9
10
11
12
13
# File 'lib/data_works/works.rb', line 6

def initialize
  # we keep a registry of all models that we create
  @data = {}
  # keep a registry of the 'current default' model of a given type
  @current_default = {}
  # keep a registry of the 'limiting scope' for parentage
  @bounding_models = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/data_works/works.rb', line 15

def method_missing(method_name, *args, &block)
  method_name = method_name.to_s
  if method_name.starts_with? 'add_'
    add_model(method_name, *args)
  else
    get_model(method_name, *args)
  end
end

Instance Method Details

#clear_current_default_for(model) ⇒ Object



39
40
41
# File 'lib/data_works/works.rb', line 39

def clear_current_default_for(model)
  @current_default.delete(model)
end

#clear_restriction_for(model) ⇒ Object



55
56
57
# File 'lib/data_works/works.rb', line 55

def clear_restriction_for(model)
  @bounding_models.delete(model)
end

#find_or_add(model_name) ⇒ Object



24
25
26
27
# File 'lib/data_works/works.rb', line 24

def find_or_add(model_name)
  record = find(model_name, 1)
  record ? record : send("add_#{model_name}")
end

#set_current_default(for_model:, to:) ⇒ Object



35
36
37
# File 'lib/data_works/works.rb', line 35

def set_current_default(for_model:, to:)
  @current_default[for_model] = to
end

#set_restriction(for_model:, to:, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/data_works/works.rb', line 43

def set_restriction(for_model:, to:, &block)
  if block_given?
    @bounding_models[for_model] = to
    block_return = block.call
    clear_restriction_for(for_model)
    block_return
  elsif
    @bounding_models[for_model] = to
    to
  end
end

#was_added(model_name, model) ⇒ Object



29
30
31
32
33
# File 'lib/data_works/works.rb', line 29

def was_added(model_name, model)
  model_name = model_name.to_sym
  @data[model_name] ||= []
  @data[model_name] << model
end