Class: Jsapi::DSL::Base

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

Direct Known Subclasses

Definitions, Operation, Path, Schema

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jsapi/dsl/base.rb', line 37

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)



122
123
124
125
126
127
128
129
130
131
# File 'lib/jsapi/dsl/base.rb', line 122

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

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

Instance Method Details

#import(filename) ⇒ Object

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

Raises:

  • (ArgumentError)


67
68
69
70
71
72
# File 'lib/jsapi/dsl/base.rb', line 67

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)


75
76
77
78
79
80
# File 'lib/jsapi/dsl/base.rb', line 75

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?(param1) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


82
83
84
# File 'lib/jsapi/dsl/base.rb', line 82

def respond_to_missing?(param1, *) # :nodoc:
  keyword?(param1)
end