Class: RubyLess::TypedMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_less/typed_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TypedMethod

Returns a new instance of TypedMethod.



5
6
7
8
# File 'lib/ruby_less/typed_method.rb', line 5

def initialize(*args)
  @name = args.shift
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/ruby_less/typed_method.rb', line 3

def args
  @args
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/ruby_less/typed_method.rb', line 3

def name
  @name
end

Instance Method Details

#add_argument(arg, klass_or_opts = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/ruby_less/typed_method.rb', line 10

def add_argument(arg, klass_or_opts = nil)
  if klass_or_opts
    @args << TypedString.new(arg.to_s, klass_or_opts)
  elsif arg.kind_of?(TypedString)
    @arg << arg
  else
    raise Exception.new("Cannot add #{arg} to TypedMethod '#{name}' (no type and not a TypedString)")
  end
end

#last_is_hash?Boolean

Returns:



31
32
33
# File 'lib/ruby_less/typed_method.rb', line 31

def last_is_hash?
  @args.last.kind_of?(TypedString) && !@args.last.hash.empty?
end

#set_hash(key, value, klass_or_opts) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby_less/typed_method.rb', line 20

def set_hash(key, value, klass_or_opts)
  if last_is_hash?
    hash = @args.last
  else
    hash = TypedString.new
    @args << hash
  end

  hash.set_hash(key, TypedString.new(value, klass_or_opts))
end

#to_sObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_less/typed_method.rb', line 35

def to_s
  if @args.empty?
    @name
  elsif last_is_hash?
    args = @args[0..-2].map(&:to_s)
    hash = @args.last
    hash.rebuild_hash
    hash = hash.to_s[1..-2]
    args += [hash]
    "#{@name}(#{args.join(', ')})"
  else
    "#{@name}(#{@args.join(', ')})"
  end
end