Class: AdminModule::Stages

Inherits:
Object
  • Object
show all
Defined in:
lib/admin_module/stages.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page_factory) ⇒ Stages

Returns a new instance of Stages.



17
18
19
# File 'lib/admin_module/stages.rb', line 17

def initialize page_factory
  @page_factory = page_factory
end

Instance Attribute Details

#page_factoryObject (readonly)

Returns the value of attribute page_factory.



15
16
17
# File 'lib/admin_module/stages.rb', line 15

def page_factory
  @page_factory
end

Instance Method Details

#create(data) ⇒ Object

Create stage configuration data for a new stage



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/admin_module/stages.rb', line 50

def create data
  # When creating a stage, we need to set its name and save it so
  # an ID is created in the database to tie the tasks to.
  #
  # Foreign key errors will result otherwise.
  name = data[:name]

  stages_page
    .add
    .set_name(name)
    .save

  # Now, populate the rest of the data.
  stages_page
    .modify(name)
    .set_stage_data(data)
    .save
end

#delete(name) ⇒ Object

Delete a stage



72
73
74
75
76
77
# File 'lib/admin_module/stages.rb', line 72

def delete name
  assert_stage_exists name

  stages_page
    .delete name
end

#export(file_path) ⇒ Object

Export data for all stages to a file



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/admin_module/stages.rb', line 92

def export file_path
  stages = list
  export_data = {}

  stages.each do |stage|
    export_data[stage] = read stage
  end

  File.open(file_path, 'w') do |f|
    f.write export_data.to_yaml
  end

rescue Exception => e
  if e.message.include? 'No such file or directory'
    raise IOError, "No such directory - #{file_path}"
  else
    raise e
  end
end

#import(file_path, allow_creation = false) ⇒ Object

Import stage data from a file

If the stage name doesn’t currently exist, a new stage will be created. If the stage name exists, it will be updated. If allow_creation is false (default) a stage will NOT be created if it doesn’t exist, existing stages will be updated.

Raises:

  • (IOError)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/admin_module/stages.rb', line 120

def import file_path, allow_creation = false
  raise IOError, "File not found: #{file_path}" unless File.exist? file_path

  import_data = {}
  File.open(file_path, 'r') do |f|
    # Read array of stage hashes
    # FIXME is this REALLY an array?
    import_data = YAML.load(f)
  end

  existing_stages = list

  import_data.each do |name, data|
    if existing_stages.include? name
      update name, data
    else
      if allow_creation
        create data
      else
        puts "Stage '#{name}' does not exist. Skipping import."
      end
    end
  end
end

#listObject

Return a list of stage names



24
25
26
# File 'lib/admin_module/stages.rb', line 24

def list
  stages_page.get_stages
end

#read(name) ⇒ Object

Retrieve stage configuration data for an existing stage



31
32
33
34
35
# File 'lib/admin_module/stages.rb', line 31

def read name
  stages_page
    .modify(name)
    .get_stage_data
end

#rename(src, dest) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/admin_module/stages.rb', line 79

def rename src, dest
  assert_stage_exists src
  assert_stage_does_not_exist dest

  stages_page
    .modify(src)
    .set_name(dest)
    .save
end

#update(name, data) ⇒ Object

Update stage configuration data for an existing stage



40
41
42
43
44
45
# File 'lib/admin_module/stages.rb', line 40

def update name, data
  stages_page
    .modify(name)
    .set_stage_data(data)
    .save
end