bosh_common 1.2657.0 → 1.2669.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/common/version.rb +1 -1
- data/lib/common/version/bosh_version.rb +14 -12
- data/lib/common/version/release_version.rb +24 -22
- data/lib/common/version/release_version_list.rb +23 -21
- data/lib/common/version/semi_semantic_version.rb +40 -43
- data/lib/common/version/stemcell_version.rb +14 -12
- data/lib/common/version/stemcell_version_list.rb +15 -0
- data/lib/common/version/version_list.rb +51 -40
- metadata +4 -7
- data/lib/common/properties.rb +0 -6
- data/lib/common/properties/errors.rb +0 -16
- data/lib/common/properties/property_helper.rb +0 -41
- data/lib/common/properties/template_evaluation_context.rb +0 -140
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce51b12ff3c1446b5d40f082e653ffc44abf1ce9
|
4
|
+
data.tar.gz: 0d3da12c5fcd10cab6ade40ff8c2fd1db4a6c813
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4890f1bbf755437dd9726a8766fda584a8e2691da9b25ddf059edce6ca31077f717b39e86053ab2701be32e33d922252d7d975856c15068b8454453155334941
|
7
|
+
data.tar.gz: b4954e3b63b2db1ec10676b4b08f85833b8e724d1f45c198d7d9fe007b59204181d5995e4ef4876268ca1be9dc67021b59a4543742f1deae9df62a771470e18a
|
data/lib/common/version.rb
CHANGED
@@ -1,22 +1,24 @@
|
|
1
1
|
require 'common/version/semi_semantic_version'
|
2
2
|
|
3
|
-
module Bosh::Common
|
4
|
-
|
3
|
+
module Bosh::Common
|
4
|
+
module Version
|
5
|
+
class BoshVersion < SemiSemanticVersion
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def self.parse(version)
|
8
|
+
raise ArgumentError, 'Invalid Version: nil' if version.nil?
|
9
|
+
version = version.to_s
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
#discard anything after a space, including the space, to support compound bosh versions
|
12
|
+
version = version.split(' ', 2)[0] if version =~ / /
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
self.new(SemiSemantic::Version.parse(version))
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
+
private
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
def default_post_release_segment
|
20
|
+
raise NotImplementedError, 'Bosh post-release versions unsupported'
|
21
|
+
end
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
@@ -1,35 +1,37 @@
|
|
1
1
|
require 'common/version/semi_semantic_version'
|
2
2
|
|
3
|
-
module Bosh::Common
|
4
|
-
|
3
|
+
module Bosh::Common
|
4
|
+
module Version
|
5
|
+
class ReleaseVersion < SemiSemanticVersion
|
5
6
|
|
6
|
-
|
7
|
+
DEFAULT_POST_RELEASE_SEGMENT = SemiSemantic::VersionSegment.parse('dev.1')
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def self.parse(version)
|
10
|
+
raise ArgumentError, 'Invalid Version: nil' if version.nil?
|
11
|
+
version = version.to_s
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
#convert old-style dev version suffix to new dev post-release segment
|
14
|
+
matches = /\A(?<release>.*)(\.(?<dev>[0-9]+)-dev)\z/.match(version)
|
15
|
+
unless matches.nil?
|
16
|
+
version = matches[:release] + "+dev." + matches[:dev]
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
self.new(SemiSemantic::Version.parse(version))
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def to_old_format
|
23
|
+
matches = /\A(?<release>.*)(\+dev\.(?<dev>[0-9]+))\z/.match(to_s)
|
24
|
+
if matches.nil?
|
25
|
+
return nil
|
26
|
+
end
|
27
|
+
matches[:release] + '.' + matches[:dev] + "-dev"
|
25
28
|
end
|
26
|
-
matches[:release] + '.' + matches[:dev] + "-dev"
|
27
|
-
end
|
28
29
|
|
29
|
-
|
30
|
+
private
|
30
31
|
|
31
|
-
|
32
|
-
|
32
|
+
def default_post_release_segment
|
33
|
+
DEFAULT_POST_RELEASE_SEGMENT
|
34
|
+
end
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
@@ -1,33 +1,35 @@
|
|
1
1
|
require 'semi_semantic/version'
|
2
2
|
require 'common/version/release_version'
|
3
|
+
require 'common/version/version_list'
|
3
4
|
|
4
|
-
module Bosh::Common
|
5
|
-
|
5
|
+
module Bosh::Common
|
6
|
+
module Version
|
7
|
+
class ReleaseVersionList < VersionList
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
end
|
9
|
+
# @param [Array<#version>] Collection of version strings
|
10
|
+
def self.parse(versions)
|
11
|
+
self.new(VersionList.parse(versions, ReleaseVersion).versions)
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
# @param [#version] ReleaseVersion from which to rebase the post-release segment
|
15
|
+
def rebase(version)
|
16
|
+
raise TypeError, "Failed to Rebase - Invalid Version Type: #{version.class}" unless version.is_a?(ReleaseVersion)
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
# Can only rebase versions with a post-release segment
|
19
|
+
if version.version.post_release.nil?
|
20
|
+
raise ArgumentError, "Failed to Rebase - Invalid Version: #{version.inspect}"
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
latest = latest_with_pre_release(version)
|
24
|
+
if latest
|
25
|
+
if latest.version.post_release.nil?
|
26
|
+
latest.default_post_release
|
27
|
+
else
|
28
|
+
latest.increment_post_release
|
29
|
+
end
|
26
30
|
else
|
27
|
-
|
31
|
+
version.default_post_release
|
28
32
|
end
|
29
|
-
else
|
30
|
-
version.default_post_release
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
@@ -1,62 +1,59 @@
|
|
1
1
|
require 'semi_semantic/version'
|
2
|
-
require 'common/version/version_list'
|
3
2
|
|
4
|
-
module Bosh::Common
|
5
|
-
|
3
|
+
module Bosh::Common
|
4
|
+
module Version
|
5
|
+
class UnavailableMethodError < StandardError; end
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
class SemiSemanticVersion
|
8
|
+
include Comparable
|
9
9
|
|
10
|
-
|
10
|
+
DEFAULT_POST_RELEASE_SEGMENT = SemiSemantic::VersionSegment.parse('build.1')
|
11
11
|
|
12
|
-
|
12
|
+
attr_reader :version
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def self.parse(version)
|
15
|
+
raise ArgumentError, 'Invalid Version: nil' if version.nil?
|
16
|
+
version = version.to_s
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
self.new(SemiSemantic::Version.parse(version))
|
19
|
+
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
def self.parse_and_compare(a, b)
|
22
|
+
self.parse(a) <=> self.parse(b)
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
raise ArgumentError, "Invalid Version Type: #{version.class}" unless version.is_a?(SemiSemantic::Version)
|
31
|
-
@version = version
|
32
|
-
@version.freeze
|
33
|
-
end
|
25
|
+
def initialize(version)
|
26
|
+
raise ArgumentError, "Invalid Version Type: #{version.class}" unless version.is_a?(SemiSemantic::Version)
|
27
|
+
@version = version
|
28
|
+
@version.freeze
|
29
|
+
end
|
34
30
|
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
def default_post_release
|
32
|
+
self.class.new(SemiSemantic::Version.new(@version.release, @version.pre_release, default_post_release_segment))
|
33
|
+
end
|
38
34
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
35
|
+
def increment_post_release
|
36
|
+
raise UnavailableMethodError, 'Failed to increment: post-release is nil' if @version.post_release.nil?
|
37
|
+
self.class.new(SemiSemantic::Version.new(@version.release, @version.pre_release, @version.post_release.increment))
|
38
|
+
end
|
43
39
|
|
44
|
-
|
45
|
-
|
46
|
-
|
40
|
+
def increment_release
|
41
|
+
self.class.new(SemiSemantic::Version.new(@version.release.increment))
|
42
|
+
end
|
47
43
|
|
48
|
-
|
49
|
-
|
50
|
-
|
44
|
+
def <=>(other)
|
45
|
+
@version <=> other.version
|
46
|
+
end
|
51
47
|
|
52
|
-
|
53
|
-
|
54
|
-
|
48
|
+
def to_s
|
49
|
+
@version.to_s
|
50
|
+
end
|
55
51
|
|
56
|
-
|
52
|
+
private
|
57
53
|
|
58
|
-
|
59
|
-
|
54
|
+
def default_post_release_segment
|
55
|
+
DEFAULT_POST_RELEASE_SEGMENT
|
56
|
+
end
|
60
57
|
end
|
61
58
|
end
|
62
59
|
end
|
@@ -1,22 +1,24 @@
|
|
1
1
|
require 'common/version/semi_semantic_version'
|
2
2
|
|
3
|
-
module Bosh::Common
|
4
|
-
|
3
|
+
module Bosh::Common
|
4
|
+
module Version
|
5
|
+
class StemcellVersion < SemiSemanticVersion
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def self.parse(version)
|
8
|
+
raise ArgumentError, 'Invalid Version: nil' if version.nil?
|
9
|
+
version = version.to_s
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
#replace underscores with periods to maintain reverse compatibility with stemcell versions
|
12
|
+
version = version.gsub('_', '.')
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
self.new(SemiSemantic::Version.parse(version))
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
+
private
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
def default_post_release_segment
|
20
|
+
raise NotImplementedError, 'Stemcell post-release versions unsupported'
|
21
|
+
end
|
20
22
|
end
|
21
23
|
end
|
22
24
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'semi_semantic/version'
|
2
|
+
require 'common/version/stemcell_version'
|
3
|
+
require 'common/version/version_list'
|
4
|
+
|
5
|
+
module Bosh::Common
|
6
|
+
module Version
|
7
|
+
class StemcellVersionList < VersionList
|
8
|
+
|
9
|
+
# @param [Array<#version>] Collection of version strings
|
10
|
+
def self.parse(versions)
|
11
|
+
self.new(VersionList.parse(versions, StemcellVersion).versions)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,45 +1,56 @@
|
|
1
1
|
require 'semi_semantic/version'
|
2
2
|
|
3
|
-
module Bosh::Common
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
3
|
+
module Bosh::Common
|
4
|
+
module Version
|
5
|
+
class VersionList
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
attr_reader :versions
|
9
|
+
|
10
|
+
alias :latest :max
|
11
|
+
|
12
|
+
# @param [Array<#version>] Collection of version strings
|
13
|
+
# @param [class] Version type to parse as (ex: SemiSemanticVersion, ReleaseVersion, StemcellVersion, BoshVersion)
|
14
|
+
def self.parse(versions, version_type)
|
15
|
+
raise TypeError, "Failed to Parse - Invalid Version Type: '#{version_type.inspect}'" unless version_type <= SemiSemanticVersion
|
16
|
+
self.new(versions.map { |v| version_type.parse(v) })
|
17
|
+
end
|
18
|
+
|
19
|
+
# @param [Array<#version>] Collection of SemiSemanticVersion objects
|
20
|
+
def initialize(versions)
|
21
|
+
raise TypeError, "Invalid Version Array: '#{versions.inspect}'" unless versions.kind_of?(Array)
|
22
|
+
@versions = versions
|
23
|
+
end
|
24
|
+
|
25
|
+
# Gets the latest version with the same release and pre-release version as the specified version
|
26
|
+
# @param [#version] SemiSemanticVersion object
|
27
|
+
def latest_with_pre_release(version)
|
28
|
+
raise TypeError, "Invalid Version Type: #{version.class}" unless version.kind_of?(SemiSemanticVersion)
|
29
|
+
@versions.select { |v|
|
30
|
+
v.version.release == version.version.release && v.version.pre_release == version.version.pre_release
|
31
|
+
}.max
|
32
|
+
end
|
33
|
+
|
34
|
+
# Gets the latest version with the same release version as the specified version
|
35
|
+
# @param [#version] SemiSemanticVersion object
|
36
|
+
def latest_with_release(version)
|
37
|
+
raise TypeError, "Invalid Version Type: #{version.class}" unless version.kind_of?(SemiSemanticVersion)
|
38
|
+
@versions.select { |v|
|
39
|
+
v.version.release == version.version.release
|
40
|
+
}.max
|
41
|
+
end
|
42
|
+
|
43
|
+
def each(&block)
|
44
|
+
@versions.each(&block)
|
45
|
+
end
|
46
|
+
|
47
|
+
def ==(other)
|
48
|
+
@versions == other.versions
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
@versions.map{ |v| v.to_s }.to_s
|
53
|
+
end
|
43
54
|
end
|
44
55
|
end
|
45
56
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bosh_common
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2669.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- VMware
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: semi_semantic
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
version: 1.1.0
|
27
27
|
description: |-
|
28
28
|
BOSH common
|
29
|
-
|
29
|
+
bf0b96
|
30
30
|
email: support@cloudfoundry.com
|
31
31
|
executables: []
|
32
32
|
extensions: []
|
@@ -39,10 +39,6 @@ files:
|
|
39
39
|
- lib/common/exec.rb
|
40
40
|
- lib/common/exec/error.rb
|
41
41
|
- lib/common/exec/result.rb
|
42
|
-
- lib/common/properties.rb
|
43
|
-
- lib/common/properties/errors.rb
|
44
|
-
- lib/common/properties/property_helper.rb
|
45
|
-
- lib/common/properties/template_evaluation_context.rb
|
46
42
|
- lib/common/retryable.rb
|
47
43
|
- lib/common/ssl.rb
|
48
44
|
- lib/common/thread_formatter.rb
|
@@ -53,6 +49,7 @@ files:
|
|
53
49
|
- lib/common/version/release_version_list.rb
|
54
50
|
- lib/common/version/semi_semantic_version.rb
|
55
51
|
- lib/common/version/stemcell_version.rb
|
52
|
+
- lib/common/version/stemcell_version_list.rb
|
56
53
|
- lib/common/version/version_list.rb
|
57
54
|
homepage: https://github.com/cloudfoundry/bosh
|
58
55
|
licenses:
|
data/lib/common/properties.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# Copyright (c) 2009-2012 VMware, Inc.
|
2
|
-
|
3
|
-
module Bosh::Common
|
4
|
-
|
5
|
-
class TemplateEvaluationFailed < StandardError; end
|
6
|
-
|
7
|
-
class UnknownProperty < StandardError
|
8
|
-
attr_reader :name
|
9
|
-
|
10
|
-
def initialize(name)
|
11
|
-
@name = name
|
12
|
-
super("Can't find property `#{name}'")
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
module Bosh::Common
|
2
|
-
module PropertyHelper
|
3
|
-
# Copies property with a given name from src to dst.
|
4
|
-
# @param [Hash] dst Property destination
|
5
|
-
# @param [Hash] src Property source
|
6
|
-
# @param [String] name Property name (dot-separated)
|
7
|
-
# @param [Object] default Default value (if property is not in src)
|
8
|
-
def copy_property(dst, src, name, default = nil)
|
9
|
-
keys = name.split(".")
|
10
|
-
src_ref = src
|
11
|
-
dst_ref = dst
|
12
|
-
|
13
|
-
keys.each do |key|
|
14
|
-
src_ref = src_ref[key]
|
15
|
-
break if src_ref.nil? # no property with this name is src
|
16
|
-
end
|
17
|
-
|
18
|
-
keys[0..-2].each do |key|
|
19
|
-
dst_ref[key] ||= {}
|
20
|
-
dst_ref = dst_ref[key]
|
21
|
-
end
|
22
|
-
|
23
|
-
dst_ref[keys[-1]] ||= {}
|
24
|
-
dst_ref[keys[-1]] = src_ref.nil? ? default : src_ref
|
25
|
-
end
|
26
|
-
|
27
|
-
# @param [Hash] collection Property collection
|
28
|
-
# @param [String] name Dot-separated property name
|
29
|
-
def lookup_property(collection, name)
|
30
|
-
keys = name.split(".")
|
31
|
-
ref = collection
|
32
|
-
|
33
|
-
keys.each do |key|
|
34
|
-
ref = ref[key]
|
35
|
-
return nil if ref.nil?
|
36
|
-
end
|
37
|
-
|
38
|
-
ref
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,140 +0,0 @@
|
|
1
|
-
# Copyright (c) 2009-2012 VMware, Inc.
|
2
|
-
|
3
|
-
module Bosh::Common
|
4
|
-
# Helper class to evaluate templates. Used by Director, CLI and Agent.
|
5
|
-
class TemplateEvaluationContext
|
6
|
-
include PropertyHelper
|
7
|
-
|
8
|
-
# @return [String] Template name
|
9
|
-
attr_reader :name
|
10
|
-
|
11
|
-
# @return [Integer] Template instance index
|
12
|
-
attr_reader :index
|
13
|
-
|
14
|
-
# @return [Hash] Template properties
|
15
|
-
attr_reader :properties
|
16
|
-
|
17
|
-
# @return [Hash] Raw template properties (no openstruct)
|
18
|
-
attr_reader :raw_properties
|
19
|
-
|
20
|
-
# @return [Hash] Template spec
|
21
|
-
attr_reader :spec
|
22
|
-
|
23
|
-
# @param [Hash] spec Template spec
|
24
|
-
def initialize(spec)
|
25
|
-
unless spec.is_a?(Hash)
|
26
|
-
raise TemplateEvaluationFailed,
|
27
|
-
"Invalid spec provided for template evaluation context, " +
|
28
|
-
"Hash expected, #{spec.class} given"
|
29
|
-
end
|
30
|
-
|
31
|
-
if spec["job"].is_a?(Hash)
|
32
|
-
@name = spec["job"]["name"]
|
33
|
-
else
|
34
|
-
@name = nil
|
35
|
-
end
|
36
|
-
|
37
|
-
@index = spec["index"]
|
38
|
-
@spec = openstruct(spec)
|
39
|
-
@raw_properties = spec["properties"] || {}
|
40
|
-
@properties = openstruct(@raw_properties)
|
41
|
-
end
|
42
|
-
|
43
|
-
# @return [Binding] Template binding
|
44
|
-
def get_binding
|
45
|
-
binding.taint
|
46
|
-
end
|
47
|
-
|
48
|
-
# Property lookup helper
|
49
|
-
#
|
50
|
-
# @overload p(name, default_value)
|
51
|
-
# Returns property value or default value if property not set
|
52
|
-
# @param [String] name Property name
|
53
|
-
# @param [Object] default_value Default value
|
54
|
-
# @return [Object] Property value
|
55
|
-
#
|
56
|
-
# @overload p(names, default_value)
|
57
|
-
# Returns first property from the list that is set or default value if
|
58
|
-
# none of them are set
|
59
|
-
# @param [Array<String>] names Property names
|
60
|
-
# @param [Object] default_value Default value
|
61
|
-
# @return [Object] Property value
|
62
|
-
#
|
63
|
-
# @overload p(names)
|
64
|
-
# Looks up first property from the list that is set, raises an error
|
65
|
-
# if none of them are set.
|
66
|
-
# @param [Array<String>] names Property names
|
67
|
-
# @return [Object] Property value
|
68
|
-
# @raise [Bosh::Common::UnknownProperty]
|
69
|
-
#
|
70
|
-
# @overload p(name)
|
71
|
-
# Looks up property and raises an error if it's not set
|
72
|
-
# @param [String] name Property name
|
73
|
-
# @return [Object] Property value
|
74
|
-
# @raise [Bosh::Common::UnknownProperty]
|
75
|
-
def p(*args)
|
76
|
-
names = Array(args[0])
|
77
|
-
|
78
|
-
names.each do |name|
|
79
|
-
result = lookup_property(@raw_properties, name)
|
80
|
-
return result unless result.nil?
|
81
|
-
end
|
82
|
-
|
83
|
-
return args[1] if args.length == 2
|
84
|
-
raise UnknownProperty.new(names)
|
85
|
-
end
|
86
|
-
|
87
|
-
# Run a block of code if all given properties are defined
|
88
|
-
# @param [Array<String>] names Property names
|
89
|
-
# @yield [Object] property values
|
90
|
-
def if_p(*names)
|
91
|
-
values = names.map do |name|
|
92
|
-
value = lookup_property(@raw_properties, name)
|
93
|
-
return ActiveElseBlock.new(self) if value.nil?
|
94
|
-
value
|
95
|
-
end
|
96
|
-
|
97
|
-
yield *values
|
98
|
-
InactiveElseBlock.new
|
99
|
-
end
|
100
|
-
|
101
|
-
# @return [Object] Object representation where all hashes are unrolled
|
102
|
-
# into OpenStruct objects. This exists mostly for backward
|
103
|
-
# compatibility, as it doesn't provide good error reporting.
|
104
|
-
def openstruct(object)
|
105
|
-
case object
|
106
|
-
when Hash
|
107
|
-
mapped = object.inject({}) { |h, (k,v)| h[k] = openstruct(v); h }
|
108
|
-
OpenStruct.new(mapped)
|
109
|
-
when Array
|
110
|
-
object.map { |item| openstruct(item) }
|
111
|
-
else
|
112
|
-
object
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
private
|
117
|
-
class ActiveElseBlock
|
118
|
-
def initialize(template_context)
|
119
|
-
@context = template_context
|
120
|
-
end
|
121
|
-
|
122
|
-
def else
|
123
|
-
yield
|
124
|
-
end
|
125
|
-
|
126
|
-
def else_if_p(*names, &block)
|
127
|
-
@context.if_p(*names, &block)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
class InactiveElseBlock
|
132
|
-
def else
|
133
|
-
end
|
134
|
-
|
135
|
-
def else_if_p(*names)
|
136
|
-
InactiveElseBlock.new
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|