Class: Jsapi::DSL::Base
- Inherits:
-
Object
- Object
- Jsapi::DSL::Base
- Defined in:
- lib/jsapi/dsl/base.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#import(filename) ⇒ Object
Imports the file named
filenamerelative toJsapi.configation.path. -
#import_relative(filename) ⇒ Object
Imports the file named
filenamerelative to the current file’s path. -
#initialize(meta_model, pathname = nil, parent: nil, &block) ⇒ Base
constructor
A new instance of Base.
-
#respond_to_missing?(param1) ⇒ Boolean
:nodoc:.
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(, pathname = nil, parent: nil, &block) @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 .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_missing ⇒ Object (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.
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.
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:
82 83 84 |
# File 'lib/jsapi/dsl/base.rb', line 82 def respond_to_missing?(param1, *) # :nodoc: keyword?(param1) end |