Module: Struct::Default_Value::SubclassClassMethods

Defined in:
lib/struct/default_value/subclassclassmethods.rb

Defined Under Namespace

Classes: Default

Instance Method Summary collapse

Instance Method Details

#_default_value_for_index(instance, index) ⇒ Object (private)


65
66
67
68
69
# File 'lib/struct/default_value/subclassclassmethods.rb', line 65

def _default_value_for_index(instance, index)
  autonym = members.fetch index

  @defaults.has_key?(autonym) ? @defaults[autonym].get(instance) : nil
end

#default(autonym, *values) {|instance| ... } ⇒ nil (private)

Yields:

  • (instance)

Yield Parameters:

Yield Returns:

  • (nil)

30
31
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/struct/default_value/subclassclassmethods.rb', line 30

def default(autonym, *values, &block)
  autonym = autonym.to_sym
  
  if respond_to? :alias_member
    if @aliases.has_key? autonym
      raise NameError
    end
  else

    unless members.include? autonym
      raise NameError, "#{members} are not included #{autonym}"
    end
  end
    
  value = values.first
  case values.length
  when 0
    if block_given?
      @defaults[autonym] = Default.new nil, block
    else
      raise ArgumentError
    end
  when 1
    if block_given?
      raise ArgumentError
    else
      @defaults[autonym] = Default.new value
    end
  else
    raise ArgumentError
  end

  nil
end