Class: Propro::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/propro/export.rb

Constant Summary collapse

TAG_SPECIFY =
'@specify'
TAG_REQUIRE =
'@require'
EXPORT_BEGIN =
'export '
COMMENT_RANGE =
/#(.*)\Z/
DQUO =
'"'
SQUO =
'\''
EQ =
'='
ZERO_STRING =
''
INTEGER_RE =
/\A\-{0,1}[0-9]+\Z/
DECIMAL_RE =
/\A\-{0,1}[0-9]+\.[0-9]+\Z/
SPACE_RE =
/ /
YES =
'yes'
NO =
'no'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Export

Returns a new instance of Export.



57
58
59
60
61
62
63
64
# File 'lib/propro/export.rb', line 57

def initialize(name, opts = {})
  @name         = name.to_s.upcase
  @default      = opts[:default]
  @is_literal   = opts[:is_literal]
  @is_specified = opts[:is_specified]
  @is_required  = opts[:is_required]
  @comment      = opts[:comment]
end

Class Method Details

.parse(line) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/propro/export.rb', line 17

def self.parse(line)
  is_literal   = false
  is_specified = false
  is_required  = false
  comment      = nil
  line         = line.sub(EXPORT_BEGIN, ZERO_STRING)
  name, value  = line.split(EQ, 2)

  if value =~ COMMENT_RANGE
    metacomment = $1
    is_specified = true if metacomment.sub!(TAG_SPECIFY, ZERO_STRING)
    is_required  = true if metacomment.sub!(TAG_REQUIRE, ZERO_STRING)
    metacomment.strip!

    if metacomment != ZERO_STRING
      comment = metacomment
    end
  end

  value.sub!(COMMENT_RANGE, ZERO_STRING)
  value.strip!

  case value[0]
  when DQUO
    value[0]  = ZERO_STRING
    value[-1] = ZERO_STRING
  when SQUO
    is_literal = true
    value[0]   = ZERO_STRING
    value[-1]  = ZERO_STRING
  end

  new name,
    default:      value,
    is_literal:   is_literal,
    is_specified: is_specified,
    is_required:  is_required,
    comment:      comment
end

Instance Method Details

#defaultObject



82
83
84
# File 'lib/propro/export.rb', line 82

def default
  cast(@default)
end

#is_literal?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/propro/export.rb', line 86

def is_literal?
  @is_literal
end

#is_required?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/propro/export.rb', line 94

def is_required?
  @is_required
end

#is_specified?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/propro/export.rb', line 90

def is_specified?
  @is_specified
end

#keyObject



66
67
68
# File 'lib/propro/export.rb', line 66

def key
  @key ||= @name.downcase.to_sym
end

#to_rubyObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/propro/export.rb', line 70

def to_ruby
  args = []
  args << key.inspect
  args << default.inspect
  args << "lit: true" if @is_literal
  if @comment
    "set #{args.join(', ')} # #{@comment}"
  else
    "set #{args.join(', ')}"
  end
end