Module: SequencescapeExcel::List::ClassMethods

Defined in:
app/sequencescape_excel/sequencescape_excel/list.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#list_for(*args) ⇒ Object

Set up the list It will: - create a list of keys - create a struct class based on the name - creates a method which returns a list of keys for the items in each key rubocop:todo Metrics/MethodLength



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/sequencescape_excel/sequencescape_excel/list.rb', line 63

def list_for(*args) # rubocop:todo Metrics/AbcSize
  options = args.extract_options!

  model = args.first.to_s.classify

  list_model = "#{model}Items"

  define_method :keys do
    @keys ||= options[:keys]
  end

  options[:keys].each do |key|
    define_method key.to_s.pluralize do
      items.fetch(key).keys
    end
  end

  alias_method args.first, :values

  return if const_defined?(list_model)

  list_model_const =
    Object.const_set(
      list_model,
      Struct.new(*options[:keys]) do
        def fetch(key)
          members.include?(key) ? self[key] : {}
        end
      end
    )

  define_method :list_model do
    list_model_const
  end
end