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.dup
  reset
end

Instance Method Details

#assign(hash) ⇒ Object



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

def assign(hash)
  marshal_load(hash)    
end

#bump(key) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/version_info/data.rb', line 44

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



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

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

#file_nameObject



16
17
18
# File 'lib/version_info/data.rb', line 16

def file_name
  storage.file_name      
end

#file_name=(value) ⇒ Object



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

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

#get_defaultsObject



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

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

#loadObject



25
26
27
28
# File 'lib/version_info/data.rb', line 25

def load
  storage.load
  self
end

#resetObject



35
36
37
38
# File 'lib/version_info/data.rb', line 35

def reset
  clear
  assign(get_defaults)
end

#saveObject



30
31
32
33
# File 'lib/version_info/data.rb', line 30

def save
  storage.save
  self
end

#segment_at(idx) ⇒ Object



91
92
93
94
95
# File 'lib/version_info/data.rb', line 91

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



97
98
99
# File 'lib/version_info/data.rb', line 97

def segments
  @segments
end

#set_version_info(tag_str) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/version_info/data.rb', line 80

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
# File 'lib/version_info/data.rb', line 12

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

#tagObject



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

def tag
  tag_format % to_hash
end

#tag_formatObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/version_info/data.rb', line 65

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



76
77
78
# File 'lib/version_info/data.rb', line 76

def tag_format=(value)
 @tag_format = value
end

#to_hashObject



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

def to_hash
  marshal_dump
end

#to_sObject



53
54
55
# File 'lib/version_info/data.rb', line 53

def to_s
  tag
end