Class: ParseDecision::Plugin::Product

Inherits:
Plugin
  • Object
show all
Defined in:
lib/parse_decision/plugin/product.rb

Overview

####################################################################### Product info plugin

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#apply_template, #apply_templates, #write_to_file

Constructor Details

#initializeProduct

Returns a new instance of Product.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/parse_decision/plugin/product.rb', line 21

def initialize()
  $LOG.debug "Product::initialize"
  @fnameTemplate  = "@INDEX@-@[email protected]"
  @searchStr1   = "<PRODUCTS><PRODUCT"
  @searchStr2   = "<PARAMS><_DATA_SET"

  @ruleStartStr   = "<Rules>"
  @gdlStartStr  = "<Decision GuidelineId"
  @stopStr    = "</Decision>"

  @data       = []
  @openTag    = "<@TAG@_DATA>\n"
  @closeTag   = "</@TAG@_DATA>\n"
  @openTag    = "<PRODUCT_DATA>\n"
  @closeTag   = "</PRODUCT_DATA>\n"
  @lineCount    = 0
  @chunkSize    = 1000
  @product    = ""

  set_outfile( "" )
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



19
20
21
# File 'lib/parse_decision/plugin/product.rb', line 19

def data
  @data
end

Instance Method Details

#accept_as_product_data(context, ln) ⇒ Object



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
# File 'lib/parse_decision/plugin/product.rb', line 69

def accept_as_product_data(context, ln)
  if ln.include?(@searchStr1)
    context.state = :productXml
    @data.clear
    set_outfile( "" )

    # Generate the name of the output file
    match = ln.match /\sName="([^"]+)/
    product = "UnkProduct"
    if(match && match.length > 1)
      product = match[1]
    end
    @product = context.createValidName(product)
    set_outfile( apply_templates(@fnameTemplate, {"@INDEX@"=>context.indexStr, "@PROD@"=>@product}) )

    @data << ln

    create_data_file context

    @data.clear
    return true
  end

  return false
end

#accept_product_ppms(context, ln) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/parse_decision/plugin/product.rb', line 107

def accept_product_ppms(context, ln)
  if ln.include?(@searchStr2)
    context.state = :productPpms

    # XML Tidy doesn't like underscores at the beginning attribute names, take care of it here.
    ln.gsub!(/_DATA_SET/, "DATA_SET")
    ln.gsub!(/_Name/, "Name")
    ln.gsub!(/_Value/, "Value")

    @data << ln
    return true
  end
end

#accept_product_rules(context, ln) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/parse_decision/plugin/product.rb', line 121

def accept_product_rules(context, ln)
  if( ln.include?(@ruleStartStr) || ln.include?(@gdlStartStr) )
    context.state = :productRules

    @data << ln
    return true
  end
end

#close_out_data_file(context) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/parse_decision/plugin/product.rb', line 161

def close_out_data_file(context)
  puts "Closing product file #{@outfile}." if context.verbose
  File.open(context.outputPath(@outfile), "a") do |f|
    write_to_file(f,@data)
    write_to_file(f,@closeTag)      # apply_template(@closeTag, "@TAG@", context.createValidName(@product))
  end
  @lineCount = 0
  @data.clear
end

#create_data_file(context) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/parse_decision/plugin/product.rb', line 95

def create_data_file(context)
  puts "Creating product file: #{@outfile}" if context.verbose
  File.open(context.outputPath(@outfile), "w") do |f|
    write_to_file(f,@openTag)

    # Write the product PPM's Xpath (which was stored in the context)
    write_to_file(f,context[:productXpath] ) if ! context[:productXpath].nil?

    write_to_file(f,@data)
  end
end

#execute(context, ln) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/parse_decision/plugin/product.rb', line 47

def execute(context, ln)
    #$LOG.debug "Product::execute"

  case context.state
  when :app
    return accept_as_product_data context, ln

  when :productXml
    return accept_product_ppms context, ln

  when :productPpms
    return accept_product_rules context, ln

  when :productRules
    return process_product_rules context, ln

  else
    return false

  end # case
end

#process_product_rules(context, ln) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/parse_decision/plugin/product.rb', line 130

def process_product_rules(context, ln)
  if !ln.include?(@stopStr)
    @data << ln
    @lineCount += 1

    write_data_chunk context

    return true
  else

    @data << ln
    @lineCount += 1

    close_out_data_file context
    context.state = :app

    return true
  end
end

#set_outfile(outfile) ⇒ Object



43
44
45
# File 'lib/parse_decision/plugin/product.rb', line 43

def set_outfile( outfile )
  @outfile = outfile
end

#write_data_chunk(context) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/parse_decision/plugin/product.rb', line 150

def write_data_chunk(context)
  if(@lineCount > @chunkSize)
    puts "Writing rule data chunk." if context.verbose
    File.open(context.outputPath(@outfile), "a") do |f|
      write_to_file(f,@data)
    end
    @lineCount = 0
    @data.clear
  end
end