Class: Multext::MSD
- Inherits:
-
Object
- Object
- Multext::MSD
- Defined in:
- lib/multext/msd.rb
Instance Attribute Summary collapse
-
#category_code_to_category ⇒ Object
Returns the value of attribute category_code_to_category.
-
#category_to_category_code ⇒ Object
Returns the value of attribute category_to_category_code.
-
#parse_dictionary ⇒ Object
Returns the value of attribute parse_dictionary.
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #filter(params) ⇒ Object
-
#initialize ⇒ MSD
constructor
A new instance of MSD.
- #parse(str, language) ⇒ Object
Constructor Details
#initialize ⇒ MSD
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/multext/msd.rb', line 6 def initialize json = File.read("#{File.dirname(__FILE__)}/../../meta/structure.json") @raw = JSON.parse(json) @parse_dictionary = {} @category_code_to_category = {} @category_to_category_code = {} @value = {} @raw.each do |item| @category_code_to_category[item['category_code']] = item['category'] @category_to_category_code[item['category']] = item['category_code'] @parse_dictionary[item['language']] = {} if @parse_dictionary[item['language']].nil? @parse_dictionary[item['language']][item['category_code']] = {} if @parse_dictionary[item['language']][item['category_code']].nil? @parse_dictionary[item['language']][item['category_code']][item['position']] = {} if @parse_dictionary[item['language']][item['category_code']][item['position']].nil? @parse_dictionary[item['language']][item['category_code']][item['position']][item['value_code']] = item['attribute'] if @parse_dictionary[item['language']][item['category_code']][item['position']][item['value_code']].nil? @value[item['category_code']] = {} if @value[item['category_code']].nil? @value[item['category_code']][item['attribute']] = item['value'] if @value[item['category_code']][item['attribute']] .nil? end end |
Instance Attribute Details
#category_code_to_category ⇒ Object
Returns the value of attribute category_code_to_category.
4 5 6 |
# File 'lib/multext/msd.rb', line 4 def category_code_to_category @category_code_to_category end |
#category_to_category_code ⇒ Object
Returns the value of attribute category_to_category_code.
4 5 6 |
# File 'lib/multext/msd.rb', line 4 def category_to_category_code @category_to_category_code end |
#parse_dictionary ⇒ Object
Returns the value of attribute parse_dictionary.
4 5 6 |
# File 'lib/multext/msd.rb', line 4 def parse_dictionary @parse_dictionary end |
#raw ⇒ Object
Returns the value of attribute raw.
4 5 6 |
# File 'lib/multext/msd.rb', line 4 def raw @raw end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'lib/multext/msd.rb', line 4 def value @value end |
Instance Method Details
#filter(params) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/multext/msd.rb', line 32 def filter(params) @raw.find_all do |item| bool = true params.each do |arr| name = arr.first.to_s.downcase value = arr.last if (value.respond_to? 'map') value = value.map { |ele| ele.to_s.downcase } bool = value.include?(item[name].to_s.downcase) else bool = item[name].to_s.downcase == value.to_s.downcase end break unless bool end bool end end |
#parse(str, language) ⇒ Object
54 55 56 57 58 59 60 61 62 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 |
# File 'lib/multext/msd.rb', line 54 def parse(str, language) result = { language: language, category: { name: nil, code: nil }, attributes: [] } str.split('').each_with_index do |char, i| if i == 0 result[:category][:code] = char result[:category][:name] = @category_code_to_category[char] next end next if char == '-' attribute = { position: i, value: { code: char } } attribute[:name] = @parse_dictionary[language.to_s][result[:category][:code]][i.to_s][attribute[:value][:code]] attribute[:value][:name] = @value[result[:category][:code]][attribute[:name]] result[:attributes] << attribute end result end |