Class: Liquor::Function
- Inherits:
-
Object
- Object
- Liquor::Function
- Defined in:
- lib/liquor/function.rb
Instance Attribute Summary collapse
-
#call(arg = nil, kw = {}, loc = {}) ⇒ Object
readonly
Returns the value of attribute call.
-
#mandatory_named_args ⇒ Object
readonly
Returns the value of attribute mandatory_named_args.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#optional_named_args ⇒ Object
readonly
Returns the value of attribute optional_named_args.
-
#unnamed_arg ⇒ Object
readonly
Returns the value of attribute unnamed_arg.
Instance Method Summary collapse
- #alias(new_name) ⇒ Object
- #check_type(name, arg, expected_type, loc) ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Function
constructor
A new instance of Function.
Constructor Details
#initialize(name, options = {}, &block) ⇒ Function
Returns a new instance of Function.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/liquor/function.rb', line 8 def initialize(name, ={}, &block) @name = name.to_s = .dup @unnamed_arg = .delete(:unnamed_arg) @mandatory_named_args = (.delete(:mandatory_named_args) || {}).to_hash @optional_named_args = (.delete(:optional_named_args) || {}).to_hash @body = block if @body.nil? raise "Cannot define a function without body" elsif .any? raise "Unknown function options: #{.keys.join ", "}" end end |
Instance Attribute Details
#call(arg = nil, kw = {}, loc = {}) ⇒ Object (readonly)
Returns the value of attribute call.
6 7 8 |
# File 'lib/liquor/function.rb', line 6 def call @call end |
#mandatory_named_args ⇒ Object (readonly)
Returns the value of attribute mandatory_named_args.
5 6 7 |
# File 'lib/liquor/function.rb', line 5 def mandatory_named_args @mandatory_named_args end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/liquor/function.rb', line 3 def name @name end |
#optional_named_args ⇒ Object (readonly)
Returns the value of attribute optional_named_args.
5 6 7 |
# File 'lib/liquor/function.rb', line 5 def optional_named_args @optional_named_args end |
#unnamed_arg ⇒ Object (readonly)
Returns the value of attribute unnamed_arg.
4 5 6 |
# File 'lib/liquor/function.rb', line 4 def unnamed_arg @unnamed_arg end |
Instance Method Details
#alias(new_name) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/liquor/function.rb', line 24 def alias(new_name) self.class.new(new_name, { unnamed_arg: @unnamed_arg, mandatory_named_args: @mandatory_named_args, optional_named_args: @optional_named_args, }, &@body) end |
#check_type(name, arg, expected_type, loc) ⇒ Object
32 33 34 35 36 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 |
# File 'lib/liquor/function.rb', line 32 def check_type(name, arg, expected_type, loc) actual_type = Runtime.type arg check_failed = false case expected_type when :any check_failed = (actual_type == :invalid) when :null, :boolean, :string, :integer, :tuple, :external check_failed = (actual_type != expected_type) when Array check_failed = !expected_type.include?(actual_type) else raise "Invalid type specifier: #{expected_type.inspect}" end if check_failed && Array(expected_type).include?(:tuple) && Runtime.indexable?(arg) check_failed = false end if check_failed expected = Array(expected_type).map(&:capitalize).join(", ") Runtime.soft_error(ArgumentTypeError, "#{expected} value expected, #{Runtime.pretty_type arg} found", Array(expected_type).first, { function: @name, argument: name }.merge(loc)) else arg end end |