Class: Dao::Mode

Inherits:
String
  • Object
show all
Defined in:
lib/dao/mode.rb

Constant Summary collapse

HTTP =

setup mode singletons and their aliases

( READ = %w[ get options head ] ) + ( WRITE = %w[ post put delete trace connect ] )

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add(mode) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/dao/mode.rb', line 18

def add(mode)
  mode = new(mode.to_s)

  unless list.include?(mode)
    list.push(mode)
    singleton_class =
      class << Mode
        self
      end
    singleton_class.module_eval do
      attr_accessor mode
    end
    Mode.send("#{ mode }=", mode)
    mode
  end
end

.alias(a, b) ⇒ Object



46
47
48
49
50
51
# File 'lib/dao/mode.rb', line 46

def Mode.alias(a, b)
  a, b = Mode.for(a), Mode.for(b)
  a.aliases.push(b) unless a.aliases.include?(b)
  b.aliases.push(a) unless b.aliases.include?(a)
  (a.aliases + b.aliases).uniq
end

.defaultObject



35
36
37
# File 'lib/dao/mode.rb', line 35

def default
  @default ||= Mode.for(:read)
end

.for(mode) ⇒ Object



7
8
9
10
11
12
# File 'lib/dao/mode.rb', line 7

def for(mode)
  return mode if mode.is_a?(Mode)
  mode = mode.to_s
  return Mode.send(mode) if Mode.respond_to?(mode)
  Mode.new(mode)
end

.listObject



14
15
16
# File 'lib/dao/mode.rb', line 14

def list
  @list ||= []
end

Instance Method Details

#===(other) ⇒ Object



58
59
60
# File 'lib/dao/mode.rb', line 58

def ===(other)
  case_of?(other)
end

#aliasesObject

instance methods



42
43
44
# File 'lib/dao/mode.rb', line 42

def aliases
  @aliases ||= []
end

#case_of?(other) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/dao/mode.rb', line 53

def case_of?(other)
  a, b = self, Mode.for(other)
  a == b or a.aliases.include?(b) or b.aliases.include?(a)
end