Class: WorkingSet::AbilityArray

Inherits:
Array
  • Object
show all
Defined in:
lib/working_set/ability_array.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dump(array) ⇒ Object

Convert SymbolArray to DB format (string)


23
24
25
26
27
# File 'lib/working_set/ability_array.rb', line 23

def dump(array)
  string = [array].flatten.map(&:to_s).sort.join(', ')
  # puts "#{string.inspect} <= #{array.inspect}".red
  string
end

.load(string) ⇒ Object

Convert DB format (string) to SymbolArray “sow, treat(thing, other_thing), care” => [“sow”, “treat(thing, other_thing)”, “care”]


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/working_set/ability_array.rb', line 6

def load(string)
  array = new
  return array if string.blank?
  tree = WorkingSet.parse(string, root: :abilities_list)
  if tree && tree.list
    array << tree.list.first_ability.text_value
    if (other_abilities = tree.list.other_abilities) && other_abilities.present?
      other_abilities.elements.each do |other_ability|
        array << other_ability.ability.text_value
      end
    end
  end
  # puts "#{string.inspect} => #{array.inspect}".blue
  array
end

Instance Method Details

#check!Object

Checks that all abilities are valid


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/working_set/ability_array.rb', line 31

def check!
  each do |string|
    ability = nil
    begin
      ability = WorkingSet.parse(string, root: :ability)
    rescue WorkingSet::SyntaxError => e
      raise InvalidExpression, "Cannot parse invalid ability: #{string.inspect}: #{e.message}"
    end

    unless ability_item = Nomen::Ability.find(ability.ability_name.text_value)
      raise InvalidExpression, "Unknown ability: #{ability.ability_name.text_value}"
    end
    parameters = []
    if ability.ability_parameters.present? && ability.ability_parameters.parameters.present?
      ps = ability.ability_parameters.parameters
      parameters << ps.first_parameter
      ps.other_parameters.elements.each do |other_parameter|
        parameters << other_parameter.parameter
      end if ps.other_parameters
    end
    if ability_item.parameters
      if parameters.any?
        lists = []
        ability_item.parameters.each_with_index do |parameter, index|
          if parameter == :variety
            item = find_nomenclature_item(:varieties, parameters[index].text_value)
          elsif parameter == :issue_nature
            item = find_nomenclature_item(:issue_natures, parameters[index].text_value)
          else
            raise StandardError, "What parameter type: #{parameter}?"
          end
          unless item
            raise InvalidExpression, "Parameter #{parameter} (#{parameters[index].text_value}) is unknown in its nomenclature"
          end
        end
      else
        raise InvalidExpression, "Argument expected for ability #{ability_item.name}"
      end
    else
      if parameters.any?
        raise InvalidExpression, "No argument expected for ability #{ability_item.name}"
      end
    end
  end
  true
end