Class: TagDb::TagReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include_path, opts = {}) ⇒ TagReader

Returns a new instance of TagReader.



23
24
25
26
27
28
29
# File 'lib/tagutils/tags/readers/tag.rb', line 23

def initialize( 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

Instance Attribute Details

#include_pathObject (readonly)

Returns the value of attribute include_path.



19
20
21
# File 'lib/tagutils/tags/readers/tag.rb', line 19

def include_path
  @include_path
end

Instance Method Details

#read(name) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/tagutils/tags/readers/tag.rb', line 32

def read( 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

#read_worker(name, more_attribs = {}) ⇒ Object



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tagutils/tags/readers/tag.rb', line 43

def read_worker( name, more_attribs={} )

    reader = HashReaderV2.new( name, include_path )

    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

#strict?Boolean

Returns:

  • (Boolean)


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

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