Class: ICouchbase::DSL
- Inherits:
-
Object
- Object
- ICouchbase::DSL
- Defined in:
- lib/icouchbase/dsl.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(db) ⇒ DSL
constructor
A new instance of DSL.
- #normalize!(str) ⇒ Object
- #parse(input) ⇒ Object
Constructor Details
#initialize(db) ⇒ DSL
Returns a new instance of DSL.
23 24 25 |
# File 'lib/icouchbase/dsl.rb', line 23 def initialize(db) @db = db end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
21 22 23 |
# File 'lib/icouchbase/dsl.rb', line 21 def db @db end |
Class Method Details
.command(name, defaults, &block) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/icouchbase/dsl.rb', line 31 def self.command(name, defaults, &block) commands[name.to_s] = { :defaults => defaults, :block => block } end |
.commands ⇒ Object
27 28 29 |
# File 'lib/icouchbase/dsl.rb', line 27 def self.commands @commands ||= {} end |
Instance Method Details
#normalize!(str) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/icouchbase/dsl.rb', line 38 def normalize!(str) map = { '\n' => "\n", '\r' => "\r", '\t' => "\t", '\b' => "\b", '\a' => "\a" } str.gsub!(/\\[nrtba]/, map) end |
#parse(input) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/icouchbase/dsl.rb', line 49 def parse(input) tokens = [] # split input input.scan(/([^\s"']+)|"(([^"]|(?<=\\)")+)"/) do |m| tokens << (m[0] || m[1]) end tok = tokens.shift cmd = self.class.commands[tok] raise ParseError, "unknown command \"#{tok}\"" unless cmd = cmd[:defaults].dup # parse options while tok = tokens.shift # handle options terminator break if tok == "--" # is it first argument? if tok[0] != "-" tokens.unshift(tok) break end name = tok[1..-1] if name.nil? || name.empty? raise ParseError, "invalid option \"#{tok}\"" end name = name.to_sym unless .keys.include?(name) raise ParseError, "unknown option \"#{name}\"" end tok = tokens.shift unless tok raise ParseError, "option \"#{name}\" requires argument" end # guess type using default value [name] = case [name] when Fixnum, Bignum Integer(tok) when Float Float(tok) else tok end end # check arguments number num = cmd[:block].arity - 1 if tokens.size != num raise ParseError, "wrong number of arguments (#{tokens.size} for #{num})" end tokens.each{|t| normalize!(t)} [cmd[:block], , *tokens] end |