Class: TagDb::TagReader

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging, Model, TextUtils::ValueHelper
Defined in:
lib/tagutils/tags/readers/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, more_attribs = {}) ⇒ TagReader

Returns a new instance of TagReader.



42
43
44
45
46
# File 'lib/tagutils/tags/readers/tag.rb', line 42

def initialize( text, more_attribs={} )
  ## todo/fix: how to add opts={} ???
  @text = text
  @more_attribs = more_attribs
end

Class Method Details

.from_file(path, more_attribs = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/tagutils/tags/readers/tag.rb', line 29

def self.from_file( path, more_attribs={} )
  ## note: assume/enfore utf-8 encoding (with or without BOM - byte order mark)
  ## - see textutils/utils.rb
  text = File.read_utf8( path )

  self.from_string( text, more_attribs )
end

.from_string(text, more_attribs = {}) ⇒ Object



37
38
39
# File 'lib/tagutils/tags/readers/tag.rb', line 37

def self.from_string( text, more_attribs={} )
  TagReader.new( text, more_attribs )
end

.from_zip(zip_file, entry_path, more_attribs = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/tagutils/tags/readers/tag.rb', line 19

def self.from_zip( zip_file, entry_path, more_attribs={} )
  ## get text content from zip
  entry = zip_file.find_entry( entry_path )

  text = entry.get_input_stream().read()
  text = text.force_encoding( Encoding::UTF_8 )

  self.from_string( text, more_attribs )
end

Instance Method Details

#initialize_old_removeeeee(include_path, opts = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/tagutils/tags/readers/tag.rb', line 51

def initialize_old_removeeeee( include_path, opts = {} )
  @include_path = include_path
  
  ## option: for now issue warning on update, that is, if key/record (country,region,city) already exists
  @strict    =  opts[:strict].present? ? true : false
end

#readObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tagutils/tags/readers/tag.rb', line 69

def read()
    reader = HashReader.from_string( @text )  # NOTE: for now HashReader does NOT accept 2nd more_attributes para - check back later

    grade = 1
  
    if @more_attribs[:grade].present?
      grade = @more_attribs[:grade].to_i
    end

    reader.each do |key, value|
     
       ### fix/todo: move to Tag.read method for reuse !!!! 
      
      
      ### split value by comma (e.g. northern america,southern america, etc.)
      logger.debug "adding grade #{grade} tags >>#{key}<< >>#{value}<<..."
      tag_pairs = value.split(',')
      tag_pairs.each do |pair|
      ## split key|name
        values = pair.split('|')
      
        key   = values[0]
        ### remove (optional comment) from key (e.g. carribean (islands))
        key = key.gsub( /\(.+\)/, '' )
        ## remove leading n trailing space
        key = key.strip
        
        name = values[1] || ''  # nb: name might be empty/missing
        name = name.strip

        tag_attribs = {}
      
        ## check if it exists
        ## todo/fix: add country_id for lookup?
        tag = Tag.find_by_key( key )
        if tag.present?
          logger.debug "update tag #{tag.id}-#{tag.key}:"
        else
          logger.debug "create tag:"
          tag = Tag.new
          tag_attribs[ :key ] = key
        end

        tag_attribs[ :name  ] = name
        tag_attribs[ :grade ] = grade

        logger.debug tag_attribs.to_json

        tag.update_attributes!( tag_attribs )
      end
  end # each key,value

end

#read_old_removeeee(name) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/tagutils/tags/readers/tag.rb', line 58

def read_old_removeeee( name )
  ## check for grade in name e.g. tag.1, tag.2 etc.

  if name =~ /^tag.*\.(\d)$/
     read_worker( name, :grade => $1.to_i )
  else
     read_worker( name )
  end
end

#strict?Boolean

fix: for now no way to pass in opts hash

Returns:

  • (Boolean)


49
# File 'lib/tagutils/tags/readers/tag.rb', line 49

def strict?()      @strict == true;     end