Class: Humanized::Humanizer

Inherits:
Object show all
Defined in:
lib/humanized/humanizer.rb

Overview

The most important method you may use is #[].

Constant Summary collapse

IS_STRING =
lambda{|x| x.kind_of? String }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Humanizer

Creates a new Humanizer

Parameters:

  • components (Hash)

    a customizable set of options



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
205
206
207
208
# File 'lib/humanized/humanizer.rb', line 158

def initialize(*args)
  
  components = args.last.kind_of?(Hash) ? args.pop : {}
  
  used_keys = Set.new
  
  args.each do | mod |
    if mod.kind_of? Module
      extend mod
    end
  end
  
  if args.first.kind_of? Humanized::Humanizer
    
    humanizer = args.first
    
    modules = (class << humanizer; included_modules ; end ) - self.class.included_modules
    
    modules.each do | mod |
      
      extend mod
      
    end
    
    (class << self ; self ; end).each_component do |name, options|
      if humanizer.respond_to? name
        if components.key? name
          self.send("#{name}=".to_sym, options[:initializer].call(components[name],humanizer.respond_to?(name) ? humanizer.send(name) : nil ))
        else
          self.send("#{name}=".to_sym, humanizer.send(name))
        end
      else
        self.send("#{name}=".to_sym, options[:initializer].call(components[name],nil))
      end
      used_keys << name
    end
  else
    
    (class << self ; self ; end).each_component do |name, options|
      self.send("#{name}=".to_sym, options[:initializer].call(components[name], nil))
      used_keys << name
    end
  
  end
  
  components.each do | k, v |
    warn "Unused key #{k.inspect} with value #{v.inspect} in Humanizer.initialize" unless used_keys.include? k
  end
  
  
end

Class Method Details

.each_componentObject Also known as: components



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/humanized/humanizer.rb', line 96

def each_component
  klass = self
  components = Set.new
  while( klass )
    a = klass.instance_variable_get("@components")
    if a
      a.each do |name, options|
        unless components.include?(name)
          yield(name, options) if block_given?
          components << name
        end
      end
    end
    klass = klass.superclass
  end
  return components
end

Instance Method Details

#[](*args) ⇒ String

Creates a String from the input. This will be the most used method in application code. It expects a Query as argument. Anything that is not a Query will be converted into a Query using the “_”-method. This enables you to pass any object to this method. The result is mainly determined by result of the “_”-method. For

Parameters:

Returns:



224
225
226
227
# File 'lib/humanized/humanizer.rb', line 224

def [](*args)
  it = args._
  return it.to_humanized(self)
end

#[]=(it, *rest) ⇒ Object

Stores a translation



230
231
232
233
# File 'lib/humanized/humanizer.rb', line 230

def []=(it, *rest)
  last = rest.pop
  @source.store(it._(*rest).first,last)
end

#interpolate(str, vars = {}) ⇒ Object



235
236
237
238
239
# File 'lib/humanized/humanizer.rb', line 235

def interpolate(str,vars={})
  return @compiler.compile(str).call(self,vars)
rescue Exception => e
  return handle_interpolation_exception(e, str, vars)
end

#new(components) ⇒ Object

Creates a new Humanizer which uses the interpolater, compiler and source of this Humanizer unless other values for them were specified.

See Also:



213
214
215
# File 'lib/humanized/humanizer.rb', line 213

def new(components)
  self.class.new(self, components)
end