Class: Jsapi::DSL::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/jsapi/dsl/base.rb

Direct Known Subclasses

Callback, Definitions, Operation, Schema

Instance Method Summary collapse

Constructor Details

#initialize(meta_model, pathname = nil, parent: nil, &block) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jsapi/dsl/base.rb', line 6

def initialize(meta_model, pathname = nil, parent: nil, &block)
  @meta_model = meta_model
  @pathname = pathname
  @parent = parent

  # Raise an error when pathname is attempted to be imported again
  if pathname && (ancestor = parent)
    while ancestor
      if ancestor.pathname == pathname
        raise Error, "Attempted #{pathname.to_path.inspect} to be imported again"
      end

      ancestor = ancestor.parent
    end
  end

  # Evaluate the file to be imported
  instance_eval(pathname.read, pathname.to_path) if pathname

  # Evaluate block
  if block
    if meta_model.reference?
      raise Error, "reference can't be specified together with a block"
    end

    instance_eval(&block)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject (private)



89
90
91
92
93
94
95
96
97
# File 'lib/jsapi/dsl/base.rb', line 89

def keyword(name, *params, &block)
  method = find_method(name)
  raise "unsupported keyword: #{name}" unless method

  define(name) do
    result = @meta_model.public_send(method, *params)
    Base.new(result, &block) if block
  end
end

Instance Method Details

#import(filename) ⇒ Object

Imports the file named filename relative to Jsapi.configation.path.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
# File 'lib/jsapi/dsl/base.rb', line 36

def import(filename)
  raise ArgumentError, "file name can't be blank" if filename.blank?

  pathname = Jsapi.configuration.pathname("#{filename}.rb")
  self.class.new(@meta_model, pathname, parent: self)
end

#import_relative(filename) ⇒ Object

Imports the file named filename relative to the current file’s path.

Raises:

  • (ArgumentError)


44
45
46
47
48
49
# File 'lib/jsapi/dsl/base.rb', line 44

def import_relative(filename)
  raise ArgumentError, "file name can't be blank" if filename.blank?

  pathname = (@pathname&.parent || Jsapi.configuration.pathname) + "#{filename}.rb"
  self.class.new(@meta_model, pathname, parent: self)
end

#respond_to_missing?(*args) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


51
52
53
# File 'lib/jsapi/dsl/base.rb', line 51

def respond_to_missing?(*args) # :nodoc:
  keyword?(args.first)
end