Class: FunWith::Configurations::Config

Inherits:
Object
  • Object
show all
Extended by:
ConfigAPI
Includes:
ConfigOverriddenMethods
Defined in:
lib/fun_with/configurations/config.rb

Instance Method Summary collapse

Methods included from ConfigAPI

from_file, from_hash, from_yaml, key_check

Methods included from ConfigOverriddenMethods

override_method

Constructor Details

#initialize(key_to_self = nil, parent = nil, &block) ⇒ Config

Returns a new instance of Config.



6
7
8
9
10
11
# File 'lib/fun_with/configurations/config.rb', line 6

def initialize( key_to_self = nil, parent = nil, &block )
  @key_to_self = key_to_self
  @parent      = parent
  @config_vars = {}
  self.instance_exec( &block ) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fun_with/configurations/config.rb', line 13

def method_missing( method, *args, &block )
  method = method.to_s.gsub( /=$/, '' ).to_sym
  
  if block_given?
    self[method] = Config.new(method, self) unless self[method].is_a?(Config)
    self[method].instance_exec( &block )
  elsif args.length == 1
    self[method] = args.first
  elsif args.length > 1
    self[method] = args
  else
    self[method]   
  end
end

Instance Method Details

#[](sym) ⇒ Object



28
29
30
31
32
# File 'lib/fun_with/configurations/config.rb', line 28

def []( sym )
  sym = sym.to_sym if sym.is_a?(String)
  self.class.key_check( sym )
  @config_vars[ sym ]
end

#[]=(sym, val) ⇒ Object



34
35
36
37
38
# File 'lib/fun_with/configurations/config.rb', line 34

def []=( sym, val )
  sym = sym.to_sym if sym.is_a?(String)
  self.class.key_check( sym )
  @config_vars[ sym ] = val
end

#each(*args, &block) ⇒ Object



91
92
93
# File 'lib/fun_with/configurations/config.rb', line 91

def each( *args, &block )
  @config_vars.each( *args, &block )
end

#fwc {|_self| ... } ⇒ Object

Useful when assigning selective settings from a config tree. Example:

RailsSite.config.constant_contact.fwc do |config|

@key      = config.oauth.key
@secret   = config.oauth.secret
@redir    = config.oauth.redirect_url
@user     = config.user
@password = config.password

end

Yields:

  • (_self)

Yield Parameters:



188
189
190
191
# File 'lib/fun_with/configurations/config.rb', line 188

def fwc( &block )
  yield self if block_given?
  self
end

#fwc_assign!(obj = self.fwc_configured_object) ⇒ Object

Assigns the topmost set of configuration symbols as instance variables. Translates any subconfigurations into a hash.

Example:

config.set.subset.subsubset.fwc_assign!( obj )

Say that the referenced config had keys :name, :height, :gravatar, :public_key, then the object would be assigned the corresponding instance_vars, @name, @height, etc.



129
130
131
132
133
134
135
# File 'lib/fun_with/configurations/config.rb', line 129

def fwc_assign!( obj = self.fwc_configured_object )
  for k, v in self
    obj.instance_variable_set( "@#{k}", v.is_a?( Config ) ? v.to_hash : v )
  end
  
  obj
end

#fwc_configured_objectObject

Every subtree / subconfig in the config hierarchy will share the same configured object.



159
160
161
162
163
164
165
# File 'lib/fun_with/configurations/config.rb', line 159

def fwc_configured_object
  if self.fwc_root?
    @configured_object
  else
    self.fwc_root.fwc_configured_object
  end
end

#fwc_configured_object=(obj) ⇒ Object

used for assigning the configured



168
169
170
171
172
173
174
# File 'lib/fun_with/configurations/config.rb', line 168

def fwc_configured_object=( obj )
  if self.fwc_root?
    @configured_object = obj
  else
    self.fwc_root.fwc_configured_object = obj
  end
end

#fwc_overridden_methodsObject



116
117
118
# File 'lib/fun_with/configurations/config.rb', line 116

def fwc_overridden_methods
  self.class.fwc_overridden_methods
end

#fwc_parentObject



154
155
156
# File 'lib/fun_with/configurations/config.rb', line 154

def fwc_parent
  @parent
end

#fwc_rootObject

returns the topmost configuration, obviously



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fun_with/configurations/config.rb', line 138

def fwc_root
  root = self
  
  while true
    if root.fwc_parent.is_a?(Config)
      root = root.fwc_parent
    else
      return root
    end
  end
end

#fwc_root?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/fun_with/configurations/config.rb', line 150

def fwc_root?
  self == self.fwc_root
end

#fwc_save(file = nil) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/fun_with/configurations/config.rb', line 194

def fwc_save( file = nil )
  raise "NOT TESTED!"
  
  root = self.fwc_root
  file = (file || root.fwc_configuration_file).fwf_filepath
  
  file.write( root.to_s )
end

#promote_configuration(*keys) ⇒ Object

Say you had a configuration that had multiple entries, and you wanted to select from among them at runtime. Example: config:

important_folder:
  development: "/this/directory",
  test:        "/that/directory",
  production:  "~/another/directory"

You could do config.important_folder every time you want to access that setting. Or you can do config.important_folder.promote_configuration(:development) and have the development subconfiguration replace the important_folder: configuration

You can promote a sub-sub-sub-config by sending an array of symbols. But I hope it never comes to that.



54
55
56
57
58
59
60
61
# File 'lib/fun_with/configurations/config.rb', line 54

def promote_configuration( *keys )
  replace_with = self.try.config_method_chain_result( keys )
  if replace_with.success?
    @parent[@key_to_self] = replace_with.config
  else
    raise ChainError.new( "config failed to promote_configuration #{keys.inspect}" )
  end
end

#to_hashObject



108
109
110
111
112
113
114
# File 'lib/fun_with/configurations/config.rb', line 108

def to_hash
  (hash = {}).tap do
    for k, v in @config_vars
      hash[k] = v.is_a?(Config) ? v.to_hash : v
    end
  end
end

#to_ruby_code(indent = 0) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/fun_with/configurations/config.rb', line 71

def to_ruby_code( indent = 0 )
  (code = "").tap do
    if indent == 0
      code << "FunWith::Configurations::Config.new do\n"
      code << self.to_ruby_code( 2 )
      code << "end\n"
    else
      for k, v in @config_vars
        if v.is_a?( Config )
          code << (" " * indent) + "#{k} do\n"
          code << v.to_ruby_code( indent + 2 )
          code << (" " * indent) + "end\n"
        else
          code << (" " * indent) + "#{k} #{v.inspect}\n"
        end
      end
    end
  end
end

#to_s(style = :hash) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fun_with/configurations/config.rb', line 95

def to_s( style = :hash )
  case style
  when :hash
    self.to_hash.inspect
  when :ruby
    self.to_ruby_code
  when :yaml
    Psych.dump( self.to_hash )
  else
    super
  end
end

#try(*keys) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/fun_with/configurations/config.rb', line 63

def try( *keys )
  (t = TryObject.new( self )).tap do
    for key in keys
      t[key]
    end
  end
end