construct 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +31 -8
  2. data/lib/construct.rb +21 -5
  3. metadata +2 -2
data/README CHANGED
@@ -13,7 +13,8 @@ making documentation easy. That's where Construct comes in.
13
13
 
14
14
  config = Construct.new
15
15
 
16
- Hey, we have a configuration object. Let's describe a web site. Keys are always symbols, but can be accessed via methods just like an OpenStruct.
16
+ Hey, we have a configuration object. Let's describe a web site. Keys are always
17
+ symbols, but can be accessed via methods just like an OpenStruct.
17
18
 
18
19
  config.name = "Aphyr"
19
20
  config.base_url = "http://aphyr.com"
@@ -23,7 +24,8 @@ And getting at them is easy, too.
23
24
  config.name
24
25
  => "Aphyr"
25
26
 
26
- If the key you want is already a method, just use config#[] and config#[]=. Construct converts strings to symbols for you..
27
+ If the key you want is already a method, just use config#[] and config#[]=.
28
+ Construct converts strings to symbols for you..
27
29
 
28
30
  How about a database connection? Those parameters all belong together.
29
31
 
@@ -40,10 +42,10 @@ methods to easily access nested options.
40
42
 
41
43
  Databases need a host. Let's define a host field.
42
44
 
43
- config.db.schema[:host] = {
45
+ config.db.define(:host,
44
46
  :default => '127.0.0.1',
45
47
  :desc => 'The host the database adapter connects to.'
46
- }
48
+ )
47
49
  config.db.host
48
50
  => '127.0.0.1'
49
51
 
@@ -53,7 +55,11 @@ You can override this, naturally.
53
55
  config.db.host
54
56
  => 'db.aphyr.com'
55
57
 
56
- And it serializes neatly to YAML. Keys are expressed as strings, to save typing and make things look clean.
58
+ The schema is accessible as a hash via #schema, so you can easily pump out
59
+ configuration docs, HTML forms, whatever.
60
+
61
+ Construct serializes neatly to YAML. Keys are expressed as strings, to save
62
+ typing and make things look clean.
57
63
 
58
64
  config.to_yaml =>
59
65
  ---
@@ -67,12 +73,29 @@ Hey, this is easy! Now let's load that configuration from a string.
67
73
 
68
74
  config = Construct.load yaml
69
75
 
70
- Maybe you need to implement extra logic in your config--perhaps you can specify either a db hash as shown above, or a connection string. Just subclass Construct::Construct or define methods directly on the construct.
76
+ Maybe you need to implement extra logic in your config--perhaps you can specify
77
+ either a db hash as shown above, or a connection string. Just subclass
78
+ Construct::Construct or define methods directly on the construct.
71
79
 
72
- class DBConfig < Construct::Construct
80
+ class DBConfig < Construct
73
81
  def db_str
74
82
  self[:db_str] || make_db_string_from_hash(db)
75
83
  end
76
84
  end
77
85
 
78
- Problem solved. Now get back to having fun instead of worrying about configuration!
86
+ Defining schemas at the class level, as opposed to creating a Construct
87
+ instance and operating on its schema is easy. Just use Construct.define to
88
+ operate on the class schema. When you create an instance of that class, the class schema is used as a default for the instance.
89
+
90
+ class UserConfig < Construct
91
+ define :state,
92
+ :desc => "The user's home state"
93
+ :default => "Oregon"
94
+ end
95
+ end
96
+
97
+ conf = UserConfig.new
98
+ conf.state # => "Oregon"
99
+
100
+ Problem solved. Now get back to having fun instead of worrying about
101
+ configuration!
data/lib/construct.rb CHANGED
@@ -3,7 +3,7 @@ class Construct
3
3
  # Ruby and humans with text editors.
4
4
 
5
5
  APP_NAME = 'Construct'
6
- APP_VERSION = '0.1.0'
6
+ APP_VERSION = '0.1.2'
7
7
  APP_AUTHOR = 'Kyle Kingsbury'
8
8
  APP_EMAIL = 'aphyr@aphyr.com'
9
9
  APP_URL = 'http://aphyr.com'
@@ -18,12 +18,28 @@ class Construct
18
18
  yaml_as "tag:aphyr.com,2009:construct"
19
19
  yaml_as "tag:yaml.org,2002:map"
20
20
 
21
+ class << self
22
+ attr_writer :schema
23
+ end
24
+
25
+ # Define a schema for a key on the class. The class schema is used as the
26
+ # defaults on initialization of a new instance.
27
+ def self.define(key, schema)
28
+ key = key.to_sym if String === key
29
+ @schema[key] = schema
30
+ end
31
+
21
32
  # Load a construct from a YAML string
22
33
  def self.load(yaml)
23
34
  hash = YAML::load(yaml)
24
35
  new(hash)
25
36
  end
26
37
 
38
+ # Returns the class schema
39
+ def self.schema
40
+ @schema ||= {}
41
+ end
42
+
27
43
  attr_accessor :schema, :data
28
44
 
29
45
  def initialize(data = {}, schema = {})
@@ -31,7 +47,7 @@ class Construct
31
47
  data.each do |key, value|
32
48
  self[key] = value
33
49
  end
34
- @schema = schema
50
+ @schema = self.class.schema.merge(schema)
35
51
  end
36
52
 
37
53
  def ==(other)
@@ -43,7 +59,7 @@ class Construct
43
59
 
44
60
  if @data.include? key
45
61
  @data[key]
46
- elsif @schema.include? key
62
+ elsif @schema.include? key and @schema[key].include? :default
47
63
  @schema[key][:default]
48
64
  end
49
65
  end
@@ -94,7 +110,7 @@ class Construct
94
110
  # Returns true if the construct has a value set for, or the schema defines,
95
111
  # the key.
96
112
  def include?(*args)
97
- @data.include?(*args) or @schema.include?(*args)
113
+ @data.include?(*args) or (@schema.include?(*args) and @schema[*args].include? :default)
98
114
  end
99
115
 
100
116
  # Returns the keys, both set in the construct and specified in the schema.
@@ -117,7 +133,7 @@ class Construct
117
133
  elsif include? meth
118
134
  self[meth]
119
135
  else
120
- raise NoMethodError.new('no such key in construct')
136
+ raise NoMethodError.new("no such key #{meth} in construct")
121
137
  end
122
138
  end
123
139
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: construct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Kingsbury
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-12 00:00:00 -05:00
12
+ date: 2009-07-15 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15