Module: Ruote::RubyDsl

Defined in:
lib/ruote/reader/ruby_dsl.rb

Overview

:nodoc:

Defined Under Namespace

Classes: BranchContext

Class Method Summary collapse

Class Method Details

.create_branch(name, attributes, &block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ruote/reader/ruby_dsl.rb', line 124

def self.create_branch(name, attributes, &block)

  name = name[1..-1] while name[0, 1] == '_'

  h = attributes.each_with_object({}) { |a, h1|

    if a.is_a?(Hash)
      h1.merge!(a)
    else
      h1[a] = nil
    end

  }.remap { |(k, v), h1|

    k = k.is_a?(Regexp) ? k.inspect : k.to_s
    h1[k] = to_json(v)
  }

  c = BranchContext.new(name, h)
  c.instance_eval(&block) if block

  c.to_a
end

.to_json(v) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ruote/reader/ruby_dsl.rb', line 148

def self.to_json(v)

  case v
    when Symbol; v.to_s
    when Regexp; v.inspect
    when Array; v.collect { |e| to_json(e) }
    when Hash; v.remap { |(k, v), h| h[to_json(k)] = to_json(v) }
    when Proc; v.to_raw_source + "\n"
    else v
  end
end