Class: VersionInfo::Data

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/version_info/data.rb

Instance Method Summary collapse

Constructor Details

#initialize(segments) ⇒ Data

Returns a new instance of Data.



6
7
8
9
10
# File 'lib/version_info/data.rb', line 6

def initialize(segments)
  super()
  @segments = segments
  reset
end

Instance Method Details

#assign(hash) ⇒ Object



43
44
45
# File 'lib/version_info/data.rb', line 43

def assign(hash)
  marshal_load(hash)    
end

#bump(key) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/version_info/data.rb', line 47

def bump(key)
  idx = segments.index(key.to_sym) + 1
  return unless idx
  segments[idx..2].each do |sgm| 
   send("#{sgm}=", 0) if send(sgm)
  end
  send("#{key}=", 1 + send(key).to_i)
end

#clearObject



104
105
106
107
# File 'lib/version_info/data.rb', line 104

def clear
  segments.each{|key| delete_field(key) if @table.has_key?(key)}
  @tag_format = nil
end

#file_nameObject



19
20
21
# File 'lib/version_info/data.rb', line 19

def file_name
  @file_name ||= Dir.pwd + '/' + storage.default_file_name
end

#file_name=(value) ⇒ Object



23
24
25
26
# File 'lib/version_info/data.rb', line 23

def file_name=(value)
  @file_name = value
  @storage = nil #recreate storage
end

#get_defaultsObject



109
110
111
# File 'lib/version_info/data.rb', line 109

def get_defaults
  segments.inject({}){|h, k| h[k] = 0; h}
end

#loadObject



28
29
30
31
# File 'lib/version_info/data.rb', line 28

def load
  storage.load
  self
end

#resetObject



38
39
40
41
# File 'lib/version_info/data.rb', line 38

def reset
  clear
  assign(get_defaults)
end

#saveObject



33
34
35
36
# File 'lib/version_info/data.rb', line 33

def save
  storage.save
  self
end

#segment_at(idx) ⇒ Object



94
95
96
97
98
# File 'lib/version_info/data.rb', line 94

def segment_at(idx)
  @segments << :build if (@segments.size == 3) && (idx>=3)
  (@segments.size..idx).each{|n| @segments << "vinfo#{n}".to_sym}
  @segments[idx]
end

#segmentsObject



100
101
102
# File 'lib/version_info/data.rb', line 100

def segments
  @segments
end

#set_version_info(tag_str) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/version_info/data.rb', line 83

def set_version_info(tag_str)
  clear
  values = tag_str.to_s.split(/\.|\+|\-/)
  values.each_with_index do |val, idx|
    val = val.to_s.chomp
    val = val.match(/(^\d+)$/) ? val.to_i : val
    self.send("#{segment_at(idx)}=", val )
  end
  self
end

#storageObject



12
13
14
15
16
17
# File 'lib/version_info/data.rb', line 12

def storage
  unless @storage
    @storage ||= STORAGE_CLASS[VersionInfo.file_format.to_sym].new(self)
  end
  @storage
end

#tagObject



64
65
66
# File 'lib/version_info/data.rb', line 64

def tag
  tag_format % to_hash
end

#tag_formatObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/version_info/data.rb', line 68

def tag_format
  unless @tag_format
    fmts = segments.map { |k| "%<#{k}>s"}
    fmt_join = fmts.map { |k| "." }
    fmt_join[2] = '+' if fmts.size > 2 #build uses '+'. See semver.org
    fmt_join[-1] = '' if fmt_join.size > 0 #remove last char
    @tag_format = fmts.zip(fmt_join).flatten.join
  end
  @tag_format
end

#tag_format=(value) ⇒ Object



79
80
81
# File 'lib/version_info/data.rb', line 79

def tag_format=(value)
 @tag_format = value
end

#to_hashObject



60
61
62
# File 'lib/version_info/data.rb', line 60

def to_hash
  marshal_dump
end

#to_sObject



56
57
58
# File 'lib/version_info/data.rb', line 56

def to_s
  tag
end