construct 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/construct.rb +32 -15
- metadata +29 -8
data/lib/construct.rb
CHANGED
@@ -1,28 +1,24 @@
|
|
1
1
|
class Construct
|
2
2
|
# Construct is extensible, persistent, structured configuration for
|
3
3
|
# Ruby and humans with text editors.
|
4
|
-
|
4
|
+
|
5
5
|
APP_NAME = 'Construct'
|
6
|
-
APP_VERSION = '0.1.
|
6
|
+
APP_VERSION = '0.1.7'
|
7
7
|
APP_AUTHOR = 'Kyle Kingsbury'
|
8
|
+
APP_CONTRIBUTORS = ['Kyle Kingsbury', 'Spencer Miles', 'John MacKenzie']
|
8
9
|
APP_EMAIL = 'aphyr@aphyr.com'
|
9
|
-
APP_URL = 'http://
|
10
|
-
APP_COPYRIGHT = 'Copyright (c) 2009 Kyle Kingsbury <aphyr@aphyr.com>. All rights reserved.'
|
10
|
+
APP_URL = 'http://github.com/aphyr/construct'
|
11
11
|
|
12
12
|
require 'yaml'
|
13
13
|
|
14
14
|
include Enumerable
|
15
15
|
|
16
|
-
class << self
|
17
|
-
attr_writer :schema
|
18
|
-
end
|
19
|
-
|
20
16
|
# Define a schema for a key on the class. The class schema is used as the
|
21
17
|
# defaults on initialization of a new instance.
|
22
18
|
def self.define(key, schema)
|
23
19
|
key = key.to_sym if String === key
|
24
|
-
|
25
|
-
end
|
20
|
+
self.schema[key] = schema
|
21
|
+
end
|
26
22
|
|
27
23
|
# Load a construct from a YAML string
|
28
24
|
def self.load(yaml)
|
@@ -30,6 +26,11 @@ class Construct
|
|
30
26
|
new(hash)
|
31
27
|
end
|
32
28
|
|
29
|
+
def self.load_file(filename)
|
30
|
+
hash = YAML::load_file(filename)
|
31
|
+
new(hash)
|
32
|
+
end
|
33
|
+
|
33
34
|
# Returns the class schema
|
34
35
|
def self.schema
|
35
36
|
@schema ||= {}
|
@@ -56,8 +57,8 @@ class Construct
|
|
56
57
|
if @data.include? key
|
57
58
|
@data[key]
|
58
59
|
elsif @schema.include? key and @schema[key].include? :default
|
59
|
-
@schema[key][:default]
|
60
|
-
end
|
60
|
+
@data[key] = Marshal.load(Marshal.dump(@schema[key][:default]))
|
61
|
+
end
|
61
62
|
end
|
62
63
|
|
63
64
|
# Assign a value to a key. Constructs accept only symbols as values,
|
@@ -73,7 +74,7 @@ class Construct
|
|
73
74
|
|
74
75
|
# Convert suitable hashes into Constructs
|
75
76
|
if value.is_a? Hash
|
76
|
-
if value.keys.all? { |k|
|
77
|
+
if value.keys.all? { |k|
|
77
78
|
k.is_a? String or k.is_a? Symbol
|
78
79
|
}
|
79
80
|
value = Construct.new(value)
|
@@ -112,7 +113,7 @@ class Construct
|
|
112
113
|
def each
|
113
114
|
keys.each do |key|
|
114
115
|
yield key, self[key]
|
115
|
-
end
|
116
|
+
end
|
116
117
|
end
|
117
118
|
|
118
119
|
# Returns true if the construct has a value set for, or the schema defines,
|
@@ -150,9 +151,25 @@ class Construct
|
|
150
151
|
end
|
151
152
|
end
|
152
153
|
|
154
|
+
# Flattens this construct (recursively) into a hash, merging schema with
|
155
|
+
# values. Useful for passing a Construct to a library that checks kind_of?
|
156
|
+
def to_hash
|
157
|
+
inject({}) do |hash, pair|
|
158
|
+
value = pair[1]
|
159
|
+
hash[pair[0]] = case value
|
160
|
+
when Construct
|
161
|
+
value.to_hash
|
162
|
+
else
|
163
|
+
value
|
164
|
+
end
|
165
|
+
|
166
|
+
hash
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
153
170
|
# Dumps the data (not the schema!) of this construct to YAML. Keys are
|
154
171
|
# expressed as strings.
|
155
|
-
#
|
172
|
+
#
|
156
173
|
# This gets a little complicated.
|
157
174
|
#
|
158
175
|
# If you define a schema where the default is a Construct
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: construct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 7
|
9
|
+
version: 0.1.7
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Kyle Kingsbury
|
@@ -9,10 +14,22 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2011-06-01 00:00:00 -07:00
|
13
18
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: bacon
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 1
|
30
|
+
version: "1.1"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
16
33
|
description:
|
17
34
|
email: aphyr@aphyr.com
|
18
35
|
executables: []
|
@@ -26,7 +43,7 @@ files:
|
|
26
43
|
- LICENSE
|
27
44
|
- README
|
28
45
|
has_rdoc: true
|
29
|
-
homepage: http://
|
46
|
+
homepage: http://github.com/aphyr/construct
|
30
47
|
licenses: []
|
31
48
|
|
32
49
|
post_install_message:
|
@@ -38,18 +55,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
55
|
requirements:
|
39
56
|
- - ">="
|
40
57
|
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 1
|
60
|
+
- 8
|
61
|
+
- 5
|
41
62
|
version: 1.8.5
|
42
|
-
version:
|
43
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
64
|
requirements:
|
45
65
|
- - ">="
|
46
66
|
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
47
69
|
version: "0"
|
48
|
-
version:
|
49
70
|
requirements: []
|
50
71
|
|
51
72
|
rubyforge_project: construct
|
52
|
-
rubygems_version: 1.3.
|
73
|
+
rubygems_version: 1.3.6
|
53
74
|
signing_key:
|
54
75
|
specification_version: 3
|
55
76
|
summary: Extensible, persistent, structured configuration for Ruby.
|