Sha256: eeb7da7473ad199ee9b5b95e1aacde837168721b41e8860c48ad3aa67d9632ac

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

module Ooz
  module Base
    class Field
      class Select < Field
      end
    end
  end
end

require_relative "select/option"

module Ooz
  module Base
    class Field
      class Select
        class << self
          # Merges options in `more` into `master` options
          # @param master [Ooz::Model::Field::Select]
          # @param more [Ooz::Model::Field::Select, Array<Hash>]
          # @note it prevents douplicated values
          def merge_options(master, more, silent: false)
            if !master.is_a?(Ooz::Model::Field::Select)
              raise ArgumentError.new("Expecting 'master' to be Ooz::Model::Field::Select. Given: #{master.class}")
            elsif !more.is_a?(Ooz::Model::Field::Select) && !more.is_a?(Array)
              raise ArgumentError.new("Expecting 'more' to be Ooz::Model::Field::Select or Array<Hash>. Given: #{more.class}")
            end
            opts1 = master.doc["options"]
            opts2 = more.is_a?(Array)? more : more.doc["options"]

            to_add = opts2.reject do |opt|
              opts1.any? {|o| o["value"] == opt["value"]}
            end

            # Some feedback
            if !silent && more.is_a?(Ooz::Model::Field::Select) && !to_add.empty?
              puts "Merging more options to the field '#{more.label}':\n"
              str_opts = to_add.map do |opt|
                "'#{opt["value"]}' => '#{opt["name"]}'"
              end.join("\n  • ")

              puts "  • " + str_opts + "\n"
            end

            opts1.push(*to_add)
          end
        end
        
        overridable_const :select_option, Base::Field::Select::Option
        passthrough :multiple, :flat, :data_type, :other, :other_desc, to: :doc
        passthrough_arr :options, to: :doc, klass: :select_option
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ooze-parser-0.1.11 lib/ooz/base/field/select.rb
ooze-parser-0.1.10 lib/ooz/base/field/select.rb
ooze-parser-0.1.9 lib/ooz/base/field/select.rb