Class: Ooz::Base::Field::Select

Inherits:
Ooz::Base::Field show all
Defined in:
lib/ooz/base/field/select.rb,
lib/ooz/base/field/select.rb,
lib/ooz/base/field/select/option.rb,
lib/ooz/model/field/select/option.rb

Direct Known Subclasses

Model::Field::Select

Defined Under Namespace

Classes: Option

Instance Attribute Summary

Attributes inherited from Model

#root

Attributes inherited from Common::BaseModel

#doc

Class Method Summary collapse

Methods inherited from Model

#initialize, #ooze

Methods inherited from Common::BaseModel

#as_json, #as_update, build, #changed?, #consolidate!, #initialize, #new_change?, #print, #to_json, #undo!, #update_last_change!

Methods inherited from Common::BaseClass

overridable_const, passthrough, passthrough_arr, passthrough_cls

Constructor Details

This class inherits a constructor from Ooz::Base::Model

Class Method Details

.merge_options(master, more, silent: false) ⇒ Object

Note:

it prevents douplicated values

Merges options in ‘more` into `master` options

Parameters:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ooz/base/field/select.rb', line 21

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