Class: Jerakia::Datasource::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/jerakia/datasource.rb

Direct Known Subclasses

Consul_kv, Dummy, File, Http

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookup, opts) ⇒ Instance

Returns a new instance of Instance.



17
18
19
20
21
22
# File 'lib/jerakia/datasource.rb', line 17

def initialize(lookup, opts)
  @response = Jerakia::Response.new(lookup)
  @options = self.class.set_options(opts)
  @request = lookup.request 
  @features = []
end

Class Attribute Details

.featuresObject (readonly)

Returns the value of attribute features.



9
10
11
# File 'lib/jerakia/datasource.rb', line 9

def features
  @features
end

.optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/jerakia/datasource.rb', line 10

def options
  @options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/jerakia/datasource.rb', line 13

def options
  @options
end

#requestObject (readonly)

Returns the value of attribute request.



14
15
16
# File 'lib/jerakia/datasource.rb', line 14

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



15
16
17
# File 'lib/jerakia/datasource.rb', line 15

def response
  @response
end

Class Method Details

.feature(name) ⇒ Object



34
35
36
# File 'lib/jerakia/datasource.rb', line 34

def self.feature(name)
  @features << name.to_sym unless @features.include?(name.to_sym)
end

.option(name, arguments = {}, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jerakia/datasource.rb', line 42

def self.option(name, arguments = {}, &block)
  self.options={} if self.options.nil?
  @options[name] = Proc.new do |opt|
    if arguments[:default]
      opt = arguments[:default] if opt.nil?
    end
    if arguments[:required]
      raise Jerakia::DatasourceArgumentError, "Must specify #{name} parameter" if opt.nil?
    end

    if arguments[:type]
      unless opt.is_a?(arguments[:type]) || opt.nil?
        raise Jerakia::DatasourceArgumentError, "#{name} must be a #{arguments[:type].to_s}, got #{opt.class.to_s}"
      end
    end

    if block_given?
      unless opt.nil?
        raise Jerakia::DatasourceArgumentError, "Validation failed for #{name}" unless yield opt
      end
    end
    opt
  end
end

.set_options(args) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/jerakia/datasource.rb', line 67

def self.set_options(args)
  opts = {}
  options.keys.each do |k|
    opts[k] = options[k].call(args[k])
  end
  validate_options(args)
  opts
end

.validate_options(args) ⇒ Object



76
77
78
79
80
# File 'lib/jerakia/datasource.rb', line 76

def self.validate_options(args)
  args.keys.each do |k|
    raise Jerakia::DatasourceArgumentError, "Unknown option #{k}" unless options.keys.include?(k)
  end
end

Instance Method Details

#answer(data = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/jerakia/datasource.rb', line 24

def answer(data=nil)
  if block_given?
    while @response.want?
      yield @response
    end
  else
    @response.submit(data) if response.want? and not data.nil?
  end
end

#features?(name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/jerakia/datasource.rb', line 38

def features?(name)
  @features.include?(name)
end