Class: Q_Object

Inherits:
Object
  • Object
show all
Defined in:
lib/q-language/object.rb

Overview

Copyright © 2010-2011 Jesse Sielaff

Direct Known Subclasses

QImplicit, QObject

Constant Summary collapse

Subclasses =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Q_Object

Returns a new instance of Q_Object.



7
8
9
# File 'lib/q-language/object.rb', line 7

def initialize (value)
  @__value__ = value
end

Class Method Details

.class_name(arg_type) ⇒ Object

Returns the class name corresponding to the given argument type string. Strips preceding ‘every_’ and trailing ‘_number’. For example, ‘string’ and ‘string_2’ result in ‘String’, ‘some_class’ results in ‘SomeClass’, and ‘every_number’ results in ‘Number’.



96
97
98
99
100
101
102
# File 'lib/q-language/object.rb', line 96

def Q_Object.class_name (arg_type)
  arg_type.to_s
    .capitalize
    .gsub(/_[0-9]+$/,'')
    .gsub(/(?:^|_|([0-9]))(.)/) { "#{$1}#{$2.upcase}" }
    .gsub(/^Every/,'')
end

.inherited(subclass) ⇒ Object

Adds the subclass to the Q_Object::Subclasses array.



44
45
46
47
# File 'lib/q-language/object.rb', line 44

def Q_Object.inherited (subclass)
  Subclasses << subclass
  subclass.const_set(:MethodArguments, {})
end

.method_added(name) ⇒ Object

Adds a key-value pair to the MethodArguments hash for the current Class. The key is the new method name. The value is a Hash containing a :reqs_left Array and a :reqs_right Array, each of which contains one Q class per item from the left or right side of the queue that will be passed as an argument to the new method. If the Hash contains a true key-value pair, the new method requires a block argument. If the Hash contains a qclass key-value pair, the new method will be passed all items of that Q class from the queue as a splat argument.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/q-language/object.rb', line 58

def Q_Object.method_added (name)
  # For compatibility with Ruby < 1.9.2
  return unless UnboundMethod.public_method_defined? :parameters
  
  method_args = self::MethodArguments[name] = { reqs_left: [], reqs_right: [] }
  
  instance_method(name).parameters.each do |arg_type, object_type|
    case arg_type
      when :block then method_args[:block] = true
      when :rest then method_args[:splat] = (object_type) ? const_get(:"Q#{class_name(object_type)}") : nil
      when :req then (method_args.has_key?(:splat) ? method_args[:reqs_right] : method_args[:reqs_left]) << const_get(:"Q#{class_name(object_type)}")
    end
  end
  
  # Splat arg serves as a divider:
  # 
  # def foo (number, *, string)
  #    Args to the left come from the start of the queue;
  #    args to the right come from the end of the queue.
  #    The rest of the objects are left in the queue.
  # 
  # def foo (number, *every_number, number)
  #    If a type name is given to the splat arg, it grabs
  #    every object of that type (except as needed by the
  #    required args).
end

.method_namesObject

Returns an Array of the unique names of every method defined in a Q class.



87
88
89
# File 'lib/q-language/object.rb', line 87

def Q_Object.method_names
  Subclasses.inject([]) {|ary, klass| ary | klass::MethodArguments.keys }
end

Instance Method Details

#envObject

• User method Returns the Q_Environment in which the Q_Object is operating.



18
19
20
# File 'lib/q-language/object.rb', line 18

def env
  @__environment__
end

#to_qObject

Returns the Q_Object.



24
25
26
# File 'lib/q-language/object.rb', line 24

def to_q
  self
end

#to_qliteralObject

Returns the Q literal string for the Q_Object’s internally-stored ruby value.



31
32
33
# File 'lib/q-language/object.rb', line 31

def to_qliteral
  value.to_qliteral
end

#valueObject

• User method Returns the Q_Object’s internally-stored ruby value.



38
39
40
# File 'lib/q-language/object.rb', line 38

def value
  @__value__
end