Class: NumberedList::List

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

Defined Under Namespace

Classes: BadType, DuplicateKey

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ List

Returns a new instance of List.



85
86
87
88
89
90
# File 'lib/numbered_list.rb', line 85

def initialize(name)
  if !self.class.types.include?(name.to_s.upcase)
    raise BadType, "'#{name}' Class #{name.class} is not a valid #{self.class.to_s}"
  end
  @item = self.class.find_item(name.to_s)
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



8
9
10
# File 'lib/numbered_list.rb', line 8

def item
  @item
end

Class Method Details

.add_item(item) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/numbered_list.rb', line 41

def self.add_item(item)

  if list.has_key?(item.name)
    raise DuplicateKey, "The key #{item.name} has already been registered."
  end
  @items ||= {}
  @items[item.name] = item
end

.add_item_builder(item) ⇒ Object



50
51
52
53
54
55
# File 'lib/numbered_list.rb', line 50

def self.add_item_builder(item)

  self.define_singleton_method(item.builder_method_name) do
    self.new(item.name)
  end
end

.add_item_query(item) ⇒ Object



57
58
59
60
61
# File 'lib/numbered_list.rb', line 57

def self.add_item_query(item)
  define_method(item.query_method_name) do
    name == item.name
  end
end

.add_marshall_classObject



75
76
77
78
79
80
81
82
83
# File 'lib/numbered_list.rb', line 75

def self.add_marshall_class
  klass = self.const_set("Marshall", Class.new(NumberedList::MarshallBase))
  original_class = self
  klass.class_eval do
    define_method(:value_class) do
      return original_class
    end
  end
end

.as_objsObject



102
103
104
# File 'lib/numbered_list.rb', line 102

def self.as_objs
  self.types.collect { |t| self.new(t) }
end

.description(label) ⇒ Object



98
99
100
# File 'lib/numbered_list.rb', line 98

def self.description(label)
  find_item(label).description
end

.find_item(key) ⇒ Object



93
94
95
# File 'lib/numbered_list.rb', line 93

def self.find_item(key)
  list && list.has_key?(key) ? list[key] : raise(BadType, "Unable to find the #{key}/#{key.class} for #{self.to_s} #{self.types} NumberedItem")
end

.item(item_values) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/numbered_list.rb', line 63

def self.item(item_values)

  item = NumberedList::ItemValue.with(item_values)

  add_item(item)
  add_item_builder(item)
  add_item_query(item)
  class_exists = "#{self}::Marshall".constantize rescue nil #https://stackoverflow.com/a/32869926

  add_marshall_class unless class_exists
end

.listObject



37
38
39
# File 'lib/numbered_list.rb', line 37

def self.list
  @items || {}
end

.reset_list!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/numbered_list.rb', line 18

def self.reset_list!

  list.values.each do |item|

    if respond_to?(item.builder_method_name)

      (
      class << self;
        self;
      end).send(:remove_method, item.builder_method_name)

    end #https://www.ruby-forum.com/topic/116025
    if respond_to?(item.query_method_name)
      remove_method item.query_method_name
    end
  end
  @items = {}
end

.typesObject



118
119
120
# File 'lib/numbered_list.rb', line 118

def self.types
  list.keys
end

Instance Method Details

#==(other) ⇒ Object



106
107
108
# File 'lib/numbered_list.rb', line 106

def ==(other)
  other.to_s == self.to_s
end

#to_sObject



110
111
112
# File 'lib/numbered_list.rb', line 110

def to_s
  name
end

#to_strObject



114
115
116
# File 'lib/numbered_list.rb', line 114

def to_str
  to_s
end