Class: ORI::Request

Inherits:
Object show all
Defined in:
lib/ori/request.rb

Overview

something.ri [something] request logic.

NOTE: This class DOES NOT validate particular options to be passed to get_list_methods.

Defined Under Namespace

Classes: ParseError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Request

Returns a new instance of Request.



18
19
20
21
# File 'lib/ori/request.rb', line 18

def initialize(attrs = {})
  @glm_options = {}
  attrs.each {|k, v| send("#{k}=", v)}
end

Instance Attribute Details

#glm_optionsObject

Options for Internals::get_list_methods.



10
11
12
# File 'lib/ori/request.rb', line 10

def glm_options
  @glm_options
end

#kindObject

:self, :list, :method or :error.



13
14
15
# File 'lib/ori/request.rb', line 13

def kind
  @kind
end

#messageObject

Message. E.g. for :error kind this is the message for the user.



16
17
18
# File 'lib/ori/request.rb', line 16

def message
  @message
end

Class Method Details

.parse(*args) ⇒ Object

Parse arguments into a new Request object.

parse()
parse(//)
parse(//, :all)
parse(//, :all => true, :access => "#")
parse(:puts)
parse("#puts")
parse("::puts")


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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ori/request.rb', line 50

def self.parse(*args)
  r = new(:glm_options => {:objs => []})

  begin
    if args.size < 1
      #   Fixnum.ri
      #   5.ri
      r.kind = :self
    else
      # At least 1 argument is present.
      arg1 = args.shift

      case arg1
      when Symbol, String
        #   ri :meth
        #   ri "#meth"
        #   ri "::meth"
        r.kind = :method
        if args.size > 0
          raise ParseError, "Unexpected arguments after #{arg1.inspect}"
        end

        # This is important -- look through all available methods.
        r.glm_options[:all] = true

        method_name = if arg1.to_s.match /\A(::|#)(.+)\z/
          r.glm_options[:access] = $1
          $2
        else
          arg1.to_s
        end

        r.glm_options[:re] = /\A#{Regexp.escape(method_name)}\z/

      when Regexp
        #   ri //
        #   ri //, :all
        #   ri /kk/, :option => value etc.
        r.kind = :list
        r.glm_options[:re] = arg1
        args.each do |arg|
          if arg.is_a? Hash
            if arg.has_key?(k = :join)
              r.glm_options[:objs] += [arg.delete(k)].flatten(1)
            end

            r.glm_options.merge! arg
          elsif [String, Symbol].include? arg.class
            r.glm_options.merge! arg.to_sym => true
          else
            raise ParseError, "Unsupported argument #{arg.inspect}"
          end
        end

        # Don't bother making `objs` unique, we're just the request parser.

      else
        raise ParseError, "Unsupported argument #{arg1.inspect}"
      end # case arg1
    end # if args.size < 1
  rescue ParseError => e
    r.kind = :error
    r.message = e.message
  end

  r
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/ori/request.rb', line 23

def error?
  @kind == :error
end

#list?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ori/request.rb', line 27

def list?
  @kind == :list
end

#method?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/ori/request.rb', line 31

def method?
  @kind == :method
end

#self?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/ori/request.rb', line 35

def self?
  @kind == :self
end