Module: Isq::YamlAdapters
- Included in:
- MathConcept, Quantity
- Defined in:
- lib/isq/yaml_adapters.rb
Instance Method Summary collapse
- #definition_from_yaml(model, value) ⇒ Object
- #definition_to_yaml(model, doc) ⇒ Object
- #designations_from_yaml(model, value) ⇒ Object
- #designations_to_yaml(model, doc) ⇒ Object
- #note_from_yaml(model, value) ⇒ Object
- #note_to_yaml(model, doc) ⇒ Object
- #symbols_from_yaml(model, value) ⇒ Object
- #symbols_to_yaml(model, doc) ⇒ Object
Instance Method Details
#definition_from_yaml(model, value) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/isq/yaml_adapters.rb', line 5 def definition_from_yaml(model, value) return unless value.is_a?(Hash) lang = value.keys.first.to_s text = value.values.first model.definition = Isq::LangString.new(text.to_s, language: lang) end |
#definition_to_yaml(model, doc) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/isq/yaml_adapters.rb', line 13 def definition_to_yaml(model, doc) return unless model.definition lang = model.definition.language || "en" doc["def"] = { lang => model.definition.to_s } end |
#designations_from_yaml(model, value) ⇒ Object
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 |
# File 'lib/isq/yaml_adapters.rb', line 35 def designations_from_yaml(model, value) unless value.is_a?(Array) model.designations = [] return [] end designations = value.each_with_index.map do |entry, i| lang_data = entry["designation"] next unless lang_data.is_a?(Hash) lang = lang_data.keys.first.to_s text_data = lang_data[lang] || lang_data[lang.to_sym] next unless text_data.is_a?(Hash) d = Isq::Designation.new( id: "term-#{model.id}-#{i}", lang: lang, term_form_type: "smart:fullForm", index_as: Array(text_data["index_as"]), ) d.text = Isq::LangString.new(text_data["text"].to_s, language: lang) d end.compact model.designations = designations model.pref_label = designations.first&.text designations end |
#designations_to_yaml(model, doc) ⇒ Object
64 65 66 67 68 |
# File 'lib/isq/yaml_adapters.rb', line 64 def designations_to_yaml(model, doc) doc["designations"] = model.designations.map do |d| { "designation" => { d.lang => { "text" => d.text.to_s, "index_as" => Array(d.index_as) } } } end end |
#note_from_yaml(model, value) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/isq/yaml_adapters.rb', line 20 def note_from_yaml(model, value) return unless value.is_a?(Hash) lang = value.keys.first.to_s text = value.values.first model.note = Isq::LangString.new(text.to_s, language: lang) end |
#note_to_yaml(model, doc) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/isq/yaml_adapters.rb', line 28 def note_to_yaml(model, doc) return unless model.note lang = model.note.language || "en" doc["remarks"] = { lang => model.note.to_s } end |
#symbols_from_yaml(model, value) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/isq/yaml_adapters.rb', line 70 def symbols_from_yaml(model, value) unless value.is_a?(Array) model.symbols = [] model.notation = [] return [] end symbols = value.each_with_index.map do |sym, i| s = Isq::SymbolTerm.new( id: "sym-#{model.id}-#{i}", lang: "en", term_form_type: "smart:symbol", ) s.text = Isq::LangString.new(sym.to_s, language: "en") s end model.symbols = symbols model.notation = symbols.map { |s| s.text.to_s } symbols end |
#symbols_to_yaml(model, doc) ⇒ Object
92 93 94 |
# File 'lib/isq/yaml_adapters.rb', line 92 def symbols_to_yaml(model, doc) doc["symbols"] = model.symbols.map { |s| s.text.to_s } end |