Class: EnumeratedAttribute::MethodDefinitionDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/enumerated_attribute/method_definition_dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_obj, descriptor) ⇒ MethodDefinitionDSL

Returns a new instance of MethodDefinitionDSL.



18
19
20
21
22
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 18

def initialize(class_obj, descriptor)
  @class_obj = class_obj
  @attr_name = descriptor.name
  @attr_descriptor = descriptor #this is the enums array
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methId, *args, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 39

def method_missing(methId, *args, &block)
  meth_name = methId.id2name
  
  meth_def = nil
  if args.size > 0
    arg = args.first
    if arg.instance_of?(EnumeratedAttribute::MethodDefinition)
      if arg.has_method_name?
        raise_method_syntax_error(meth_name, arg.method_name)
      end
      meth_def = arg
      meth_def.method_name = meth_name
    else
      meth_def = MethodDefinition.new(meth_name, arg)
    end
  elsif block_given?
    meth_def = MethodDefinition.new(meth_name, block)
  else
    raise_method_syntax_error(meth_name)
  end
  evaluate_method_definition(meth_def)
end

Instance Attribute Details

#decrementor_nameObject (readonly)

Returns the value of attribute decrementor_name.



16
17
18
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 16

def decrementor_name
  @decrementor_name
end

#incrementor_nameObject (readonly)

Returns the value of attribute incrementor_name.



16
17
18
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 16

def incrementor_name
  @incrementor_name
end

#initial_valueObject (readonly)

Returns the value of attribute initial_value.



16
17
18
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 16

def initial_value
  @initial_value
end

#pluralized_nameObject (readonly)

Returns the value of attribute pluralized_name.



16
17
18
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 16

def pluralized_name
  @pluralized_name
end

Instance Method Details

#decrementor(value) ⇒ Object Also known as: dec



69
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 69

def decrementor(value); @decrementor_name = value; end

#defineObject

we’ll by pass this - they can use it if it helps make code more readable - not enforced - should it be??



25
26
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 25

def define
end

#enums_accessor(value) ⇒ Object Also known as: enums, plural



71
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 71

def enums_accessor(value); @pluralized_name = value; end

#incrementor(value) ⇒ Object Also known as: inc



70
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 70

def incrementor(value); @incrementor_name = value; end

#init(value) ⇒ Object



62
63
64
65
66
67
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 62

def init(value)
  if (!@attr_descriptor.empty? && !@attr_descriptor.include?(value.to_sym))
    raise(InvalidDefinition, "'#{value}' in method 'init' is not an enumeration value for :#{@attr_name} attribute", caller) 
  end
  @initial_value = value
end

#is(*args) ⇒ Object



34
35
36
37
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 34

def is(*args)
  arg = args.first if args.length > 0
  MethodDefinition.new(nil, arg)
end

#is_not(*args) ⇒ Object Also known as: isnt



28
29
30
31
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 28

def is_not(*args)
  arg = args.first if args.length > 0
  MethodDefinition.new(nil, arg, true)
end

#label(hash) ⇒ Object Also known as: labels

Raises:



77
78
79
80
81
82
83
84
# File 'lib/enumerated_attribute/method_definition_dsl.rb', line 77

def label(hash)
	raise(InvalidDefinition, "label or labels keyword should be followed by a hash of :enum_value=>'label'", caller) unless hash.is_a?(Hash)
	hash.each do |k,v|
		raise(InvalidDefinition, "#{k} is not an enumeration value for :#{@attr_name} attribute", caller) unless (k.is_a?(Symbol) && @attr_descriptor.include?(k))
		raise(InvalidDefinition, "#{v} is not a string. Labels should be strings", caller) unless v.is_a?(String)
		@attr_descriptor.set_label(k, v)
	end
end