Class: Config

Inherits:
Object
  • Object
show all
Defined in:
lib/zarchitect/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, key = nil) ⇒ Config

Constructor requires yaml file



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/zarchitect/config.rb', line 5

def initialize(file, key = nil)
  @file = file
  GPI.print "Initializing config from #{file}.", GPI::CLU.check_option('v')
  @hash = Hash.new
  begin
    YAML.load_stream(File.open(file) { |f| f.read }) do |doc|
      #key = file.sub(File.extname(file), '').sub('_config/', '')
      #@hash[:key] = doc
      @hash = doc
      break
    end
  rescue StandardError
    GPI.print "Failed to load #{@file}."
    GPI.quit
  end
  unless key.nil?
    @hash["key"] = key
    unless @file == "_config/_index.yaml"
      @hash["index"] = false
    else
      @hash["index"] = true
    end
  end
end

Instance Method Details

#has_option?(str) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/zarchitect/config.rb', line 30

def has_option?(str)
  @hash.has_key?(str)
end

#read(key) ⇒ Object



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

def read(key)
  if has_option?(key)
    @hash[key] 
  else
    GPI.print "Option #{key} missing in  config #{@file}."
    GPI.quit
  end
end

#setupObject



34
35
36
37
38
39
40
41
42
# File 'lib/zarchitect/config.rb', line 34

def setup
  instance_eval {
    @hash.each_key do |k|
      define_singleton_method k do
        @hash[k]
      end
    end
  }
end

#validateObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/zarchitect/config.rb', line 57

def validate
  GPI.print "Validating #{@file}."
  unless @hash.has_key?("hidden")
    @hash["hidden"] = false
  end
  if @hash.has_key?("sort_type")
    unless ["alphanum", "date"].include?(@hash["sort_type"])
      GPI.print "Value of [sort_type] has to be 'date' or 'alphanum'."
      GPI.quit
    end
  else
    @hash["sort_type"] = "alphanum"
  end
  if @hash.has_key?("sort_order")
    unless ["default", "reverse"].include?(@hash["sort_order"])
      GPI.print "Value of [sort_order] has to be 'default' or 'reverse'."
      GPI.quit
    end
  else
    @hash["sort_order"] = "default"
  end
  unless @hash.has_key?("collection") 
    @hash["collection"] = false
    @hash["categorize"] = false
  end
  if @hash["collection"] == true
    unless @hash.has_key?("index_layout")
      GPI.print "The [index_layout] option is required."
      GPI.quit
    else
      unless @hash["index_layout"].class == String
        GPI.print "Value of [index_layout] is not a string."
        GPI.quit
      end
    end
    unless @hash.has_key?("index_view")
      GPI.print "The [index_view] option is required."
      GPI.quit
    else
      unless @hash["index_view"].class == String
        GPI.print "Value of [index_view] is not a string."
        GPI.quit
      end
    end
    unless @file == "_config/_index.yaml"
      if @hash.has_key?("directory")
        unless @hash["directory"].class == String
          GPI.print "Value of [directory] has to be a string."
          GPI.quit
        end
      else
        GPI.print "Collections require the [directory] option."
        GPI.quit
      end
    end
    unless @hash.has_key?("categorize")
      GPI.print ("Collections require the [categorize] option.")
      GPI.quit
    end
    if @hash["categorize"] == true
      unless @hash.has_key?("tags")
        GPI.print ("Collections with categories require the [tags] option.")
        GPI.quit
      end
      unless @hash.has_key?("categories")
        GPI.print ("Collections with categories require" +
                   " the [categories] option.")
        GPI.quit
      else
        unless @hash["categories"].class == Hash
          GPI.print "Categories option is required to be a hash."
          GPI.quit
        else
          @hash["categories"].each do |k,v|
            if k.class == String && v.class == String
              if k.match(/\A[a-zA-Z0-9_-]*\z/).nil?
                GPI.print "Invalid category key: #{k}. Only alphanumerics,"+
                  " dashes and underscores allowed!"
                  GPI.quit
              end
            else
              GPI.print "Keys and values of [categories] option have to be" +
                " strings."
              GPI.quit
            end
          end
        end
      end
    end
  else
    @hash["categorize"] = false
    @hash["tags"] = false
  end
  unless @file == "_config/_index.yaml"
    if @hash.has_key?("name")
      unless @hash["name"].class == String
        GPI.print "[name] is required to be a string."
        GPI.quit
      end
    else
      GPI.print "[name] option is missing."
      GPI.quit
    end
  else
    if @hash.has_key?("uses") 
      unless @hash["uses"].class == String
        GPI.print "[uses] should be a comma separated list of sections."
        GPI.quit
      end
    end
  end
=begin
  unless @hash.has_key?("layout")
    GPI.print "The [layout] option is required."
    GPI.quit
  else
    unless @hash["layout"].class == String
      GPI.print "Value of [layout] is not a string."
      GPI.quit
    end
  end
  unless @hash.has_key?("view")
    GPI.print "The [view] option is required."
    GPI.quit
  else
    unless @hash["view"].class == String
      GPI.print "Value of [view] is not a string."
      GPI.quit
    end
  end
=end
  if @hash.has_key?("paginate")
    unless @hash["paginate"].class == Integer
      GPI.print "Value of [paginate] can only be an integer."
      GPI.quit
    else
      if @hash["paginate"] < 0
        GPI.print "Valur of [paginate] has to be equal or greater than 0."
        GPI.quit
      end
    end
  else
    if @hash["collection"] == true
      GPI.print "[paginate] option is required for collections."
      GPI.quit
    end
  end
end

#validate_postObject



53
54
55
# File 'lib/zarchitect/config.rb', line 53

def validate_post
  #TODO
end

#validate_zrconfObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/zarchitect/config.rb', line 206

def validate_zrconf
  GPI.print "Validating #{@file}."
  unless @hash.has_key?("symlink_files")
    @hash["symlink_files"] = true
  end
  unless @hash.has_key?("url")
    GPI.print "config key [url] missing in _config/_zarchitect.yaml."
    GPI.quit
  else
    unless @hash["url"].class == String
      GPI.print "Value of [url] is not a string."
      GPI.quit
    end
  end
  unless @hash.has_key?("site_name")
    GPI.print "config key [site_name] missing in _config/_zarchitect.yaml."
    GPI.quit
  else
    unless @hash["site_name"].class == String
      GPI.print "Value of key [site_name] is not a string."
      GPI.quit
    end
  end
  unless @hash.has_key?("thumbl")
    GPI.print "config key [thumbl] missing in _config/_zarchitect.yaml."
    GPI.quit
  else
    unless @hash["thumbl"].class == Array
      GPI.print "Value of key [thumbl] is not an Array."
      GPI.quit
    end
    unless @hash["thumbl"].count == 2
      GPI.print "Array [thumbl] requires two values."
      GPI.quit
    end
    unless @hash["thumbl"][0].class == Integer
      GPI.print "First value in [thumbl] ist not an integer."
      GPI.quit
    end
    unless @hash["thumbl"][1].class == Integer
      GPI.print "Second value in [thumbl] ist not an integer."
      GPI.quit
    end
  end
  unless @hash.has_key?("thumbs")
    GPI.print "config key [thumbs] missing in _config/_zarchitect.yaml."
    GPI.quit
  else
    unless @hash["thumbs"].class == Array
      GPI.print "Value of key [thumbs] is not an Array."
      GPI.quit
    end
    unless @hash["thumbs"].count == 2
      GPI.print "Array [thumbs] requires two values."
      GPI.quit
    end
    unless @hash["thumbs"][0].class == Integer
      GPI.print "First value in [thumbs] ist not an integer."
      GPI.quit
    end
    unless @hash["thumbs"][1].class == Integer
      GPI.print "Second value in [thumbs] ist not an integer."
      GPI.quit
    end
  end
  unless @hash.has_key?("rss_size")
    GPI.print "config key [rss_size] missing in _config/_zarchitect.yaml."
    GPI.quit
  else
    unless @hash["rss_size"].class == Integer
      GPI.print "Value of [rss_size] is not an integer."
      GPI.quit
    end
  end
  if @hash.has_key?("exclude_assets")
    unless @hash["exclude_assets"].class == Array
      GPI.print "Value of [rss_size] is not an array."
      GPI.quit
    end
  end
end