Class: Gyunyu::Command::Export::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/gyunyu/command/export/option.rb

Defined Under Namespace

Classes: CustomFilterNotFound, FormatNotFound

Constant Summary collapse

FIELD_SEP =
','

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Option



13
14
15
16
17
18
19
20
21
22
# File 'lib/gyunyu/command/export/option.rb', line 13

def initialize( argv = [] )
  @lists         = []
  @filter        = nil
  @custom_filter = nil
  @fields        = []
  @format        = :csv
  @show_filter_list = false

  parser.parse( argv )
end

Instance Attribute Details

#custom_filterObject (readonly)

Returns the value of attribute custom_filter.



23
24
25
# File 'lib/gyunyu/command/export/option.rb', line 23

def custom_filter
  @custom_filter
end

#fieldsObject (readonly)

Returns the value of attribute fields.



23
24
25
# File 'lib/gyunyu/command/export/option.rb', line 23

def fields
  @fields
end

#filterObject (readonly)

Returns the value of attribute filter.



23
24
25
# File 'lib/gyunyu/command/export/option.rb', line 23

def filter
  @filter
end

#formatObject (readonly)

Returns the value of attribute format.



23
24
25
# File 'lib/gyunyu/command/export/option.rb', line 23

def format
  @format
end

#listsObject (readonly)

Returns the value of attribute lists.



23
24
25
# File 'lib/gyunyu/command/export/option.rb', line 23

def lists
  @lists
end

#show_filter_listObject (readonly)

Returns the value of attribute show_filter_list.



23
24
25
# File 'lib/gyunyu/command/export/option.rb', line 23

def show_filter_list
  @show_filter_list
end

Instance Method Details

#parserObject



33
34
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
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gyunyu/command/export/option.rb', line 33

def parser
  OptionParser.new do |opt|
    opt.on('-l', '--list LIST') { |list|
      if !@lists.include?( list )
        @lists << list
      end
    }
    opt.on('-f', '--filter FILTER') { |filter|
      @filter = filter if filter.size > 0
    }
    opt.on('-c', '--custom-filter FILTER') { |filter|
      mod = Module.nesting[1].const_get('CustomFilter')
      if mod.filter.has_key?( filter )
        @custom_filter = filter
      else
        raise CustomFilterNotFound
      end
    }
    opt.on('-s', '--custom-filter-list') { |c|
      @show_filter_list = true
    }
    opt.on('-d', '--field FIELD') { |field|
      if field.include?( FIELD_SEP )
        @fields = field.split( FIELD_SEP )
      elsif !@fields.include?( field )
        @fields << field
      end
    }
    opt.on('-o', '--format FORMAT') { |format|
      format.downcase!
      formats = Format.constants.map { |e|
        e.to_s.downcase
      }
      if formats.include?( format )
        @format = format
      else
        raise FormatNotFound
      end
    }
  end
end