Class: Dslh
- Inherits:
-
Object
- Object
- Dslh
- Defined in:
- lib/dslh.rb,
lib/dslh/version.rb
Defined Under Namespace
Classes: Scope, ScopeBlock, ValidationError
Constant Summary collapse
- INDENT_SPACES =
' '
- VALID_OPTIONS =
[ :allow_duplicate, :allow_empty_args, :conv, :dump_old_hash_array_format, :exclude_key, :filename, :force_dump_braces, :ignore_methods, :initial_depth, :key_conv, :lineno, :root_identify, :schema, :schema_path, :scope_hook, :scope_vars, :time_inspecter, :use_braces_instead_of_do_end, :value_conv, :use_heredoc_for_multi_line, ]
- VERSION =
'0.4.11'
Class Method Summary collapse
- .deval(hash, options = {}, &block) ⇒ Object
- .eval(expr_or_options = nil, options = nil, &block) ⇒ Object
Instance Method Summary collapse
- #deval(hash) ⇒ Object
- #eval(expr = nil, &block) ⇒ Object
-
#initialize(options = {}) ⇒ Dslh
constructor
of class methods.
Constructor Details
#initialize(options = {}) ⇒ Dslh
of class methods
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/dslh.rb', line 75 def initialize( = {}) = .keys - VALID_OPTIONS unless .empty? raise ArgumentError, 'invalid option ' + .map {|i| i.inspect }.join(',') end @options = { :time_inspecter => method(:inspect_time), :dump_old_hash_array_format => false, :force_dump_braces => false, :use_braces_instead_of_do_end => false, :use_heredoc_for_multi_line => false, }.merge() @options[:key_conv] ||= (@options[:conv] || proc {|i| i.to_s }) @options[:value_conv] ||= @options[:conv] end |
Class Method Details
.deval(hash, options = {}, &block) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/dslh.rb', line 66 def deval(hash, = {}, &block) if [hash, ].any? {|i| not i.kind_of?(Hash) } raise TypeError, "wrong argument type #{.class} (expected Hash)" end self.new().deval(hash, &block) end |
.eval(expr_or_options = nil, options = nil, &block) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dslh.rb', line 44 def eval( = nil, = nil, &block) if and not .kind_of?(Hash) raise TypeError, "wrong argument type #{.class} (expected Hash)" end expr = nil ||= {} if case when String expr = when Hash .update() else raise TypeError, "wrong argument type #{.class} (expected String or Hash)" end end self.new().eval(expr, &block) end |
Instance Method Details
#deval(hash) ⇒ Object
182 183 184 185 186 187 |
# File 'lib/dslh.rb', line 182 def deval(hash) buf = StringIO.new depth = @options[:initial_depth] || 0 deval0(hash, depth, buf, true) buf.string end |
#eval(expr = nil, &block) ⇒ Object
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 |
# File 'lib/dslh.rb', line 94 def eval(expr = nil, &block) retval = {} scope = Scope.new scope.instance_variable_set(:@__options__, @options) scope.instance_variable_set(:@__hash__, retval) @options[:scope_hook].call(scope) if @options[:scope_hook] (@options[:scope_vars] || {}).each do |name, value| scope.instance_variable_set("@#{name}", value) end if @options[:ignore_methods] ignore_methods = Array(@options[:ignore_methods]) ignore_methods.each do |method_name| scope.instance_eval(<<-EOS) def #{method_name}(*args, &block) method_missing(#{method_name.to_s.inspect}, *args, &block) end EOS end end if expr eval_args = [expr] [:filename, :lineno].each do |k| eval_args << @options[k] if @options[k] end scope.instance_eval(*eval_args) else scope.instance_eval(&block) end if (schema = @options[:schema] || schema_path = @options[:schema_path]) begin require 'kwalify' rescue LoadError raise 'cannot load "kwalify". please install "kwalify"' end if schema and not schema.kind_of?(String) raise TypeError, "wrong schema type #{schema.class} (expected String)" end if schema_path and not schema_path.kind_of?(String) raise TypeError, "wrong schema_path type #{schema_path.class} (expected String)" end schema = schema_path ? Kwalify::Yaml.load_file(schema_path) : Kwalify::Yaml.load(schema) validator = Kwalify::Validator.new(schema) if @options[:root_identify] new_retval = {} retval.each do |k, v| new_retval[k] = v.map {|_id, attrs| attrs.merge('_id' => _id) } end errors = validator.validate(new_retval) errors.each do |e| path = e.path.split('/', 4)[1..-1] root_key = path.shift _id = path.shift.to_i if _id_orig = new_retval.fetch(root_key, {})[_id] _id = _id_orig['_id'] || _id end path = '/' + ([root_key, _id] + path).join('/') e.path.replace(path) end else errors = validator.validate(retval) end unless errors.empty? raise ValidationError.new(errors, retval) end end return retval end |