Class: DR::Formatter

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/dr/formatter/simple_formatter.rb

Defined Under Namespace

Modules: Helpers

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

wrap

Constructor Details

#initialize(meta = {}, **opts) ⇒ Formatter

Returns a new instance of Formatter.



45
46
47
48
# File 'lib/dr/formatter/simple_formatter.rb', line 45

def initialize(meta={}, **opts)
  @meta=meta
  @opts=opts
end

Instance Attribute Details

#metaObject

Returns the value of attribute meta.



44
45
46
# File 'lib/dr/formatter/simple_formatter.rb', line 44

def meta
  @meta
end

#optsObject

Returns the value of attribute opts.



44
45
46
# File 'lib/dr/formatter/simple_formatter.rb', line 44

def opts
  @opts
end

Instance Method Details

#expand(msg, **opts) ⇒ Object



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
# File 'lib/dr/formatter/simple_formatter.rb', line 95

def expand(msg, **opts)
  recursive=opts[:recursive]
  #if recursive is :first, then we only expand once ore
  if recursive.is_a?(Integer)
    opts[:recursive]=recursive-1
    recursive=false if recursive <= 0
  end
  case msg
  when Hash
    Array(opts[:merge]).each do |key|
      if msg.key?(key)
        msg=msg.merge(msg[key])
        msg.delete(key)
      end
    end
    # we localize after merging potential out types
    localize(msg, **opts) do |lmsg|
      # localization do not count as a recursive step
      return expand(lmsg, **opts)
    end
    if recursive
      msg_exp={}
      msg.each do |k,v|
        msg_exp[k]=expand(v,**opts)
      end
      #expand may have introduced nil values
      clean_nil=opts.fetch(:clean_nil,true)
      msg_exp.delete_if {|_k,v| v==nil} if clean_nil
      msg_exp
    else
      msg
    end
  when Symbol
    opts[:symbol]||=:never
    msg=metainfo_from_symbol(msg,**opts)
    recursive ? expand(msg, **opts) : msg
  when Array
    msg=msg.map {|i| expand(i,**opts)} if recursive
    opts[:join] ? join(msg, **opts) : msg
  when String
    (nmsg=try_get_symbol(msg,**opts)) and return nmsg
    if block_given?
      yield(msg, **opts)
    else
      msg
    end
  when nil
    nil
  else
    if block_given?
      yield(msg, **opts)
    else
      #expand(msg.to_s,**opts)
      msg
    end
  end
end

#join(*args, **opts) ⇒ Object



54
55
56
57
58
# File 'lib/dr/formatter/simple_formatter.rb', line 54

def join(*args, **opts)
  opts=@opts.merge(opts)
  args=Array(args).map {|i| try_expand_symbol(i,**opts)}
  self.class.localize(*args, **opts)
end

#localize(msg, **opts) ⇒ Object



50
51
52
# File 'lib/dr/formatter/simple_formatter.rb', line 50

def localize(msg, **opts)
  self.class.localize(msg, **@opts.merge(opts))
end