Class: Megam::PredefCollection
- Inherits:
-
Object
- Object
- Megam::PredefCollection
- Includes:
- Enumerable
- Defined in:
- lib/megam/core/predef_collection.rb
Instance Attribute Summary collapse
-
#iterator ⇒ Object
readonly
Returns the value of attribute iterator.
Class Method Summary collapse
-
.json_create(o) ⇒ Object
{ “json_claz”:“Megam::PredefCollection”, “results”:[{ “id”:“PRE362554468593565696”, “name”:“rabbitmq”, “provider”:“chef”, “provider_role”:“rabbitmq”, “build_monkey”:“none”, “json_claz”:“Megam::Predef” },{ “id”:“PRE362554470090932224”, “name”:“mobhtml5”, “provider”:“chef”, “provider_role”:“sencha”, “build_monkey”:“none”, “json_claz”:“Megam::Predef” }] }.
Instance Method Summary collapse
- #<<(*args) ⇒ Object (also: #push)
- #[](index) ⇒ Object
- #[]=(index, arg) ⇒ Object
- #all_predefs ⇒ Object
- #each ⇒ Object
- #each_index ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ PredefCollection
constructor
A new instance of PredefCollection.
- #insert(predef) ⇒ Object
- #lookup(predef) ⇒ Object
-
#to_hash ⇒ Object
Transform the ruby obj -> to a Hash.
-
#to_json(*a) ⇒ Object
Serialize this object as a hash: called from JsonCompat.
Constructor Details
#initialize ⇒ PredefCollection
Returns a new instance of PredefCollection.
21 22 23 24 25 |
# File 'lib/megam/core/predef_collection.rb', line 21 def initialize @predefs = Array.new @predefs_by_name = Hash.new @insert_after_idx = nil end |
Instance Attribute Details
#iterator ⇒ Object (readonly)
Returns the value of attribute iterator.
20 21 22 |
# File 'lib/megam/core/predef_collection.rb', line 20 def iterator @iterator end |
Class Method Details
.json_create(o) ⇒ Object
{ “json_claz”:“Megam::PredefCollection”, “results”:[{ “id”:“PRE362554468593565696”, “name”:“rabbitmq”, “provider”:“chef”, “provider_role”:“rabbitmq”, “build_monkey”:“none”, “json_claz”:“Megam::Predef” },{ “id”:“PRE362554470090932224”, “name”:“mobhtml5”, “provider”:“chef”, “provider_role”:“sencha”, “build_monkey”:“none”, “json_claz”:“Megam::Predef” }] }
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/megam/core/predef_collection.rb', line 139 def self.json_create(o) collection = self.new() o["results"].each do |predefs_list| predefs_array = predefs_list.kind_of?(Array) ? predefs_list : [ predefs_list ] predefs_array.each do |predef| collection.insert(predef) end end collection end |
Instance Method Details
#<<(*args) ⇒ Object Also known as: push
41 42 43 44 45 46 47 48 |
# File 'lib/megam/core/predef_collection.rb', line 41 def <<(*args) args.flatten.each do |a| is_megam_predef(a) @predefs << a @predefs_by_name[a.name] = @predefs.length - 1 end self end |
#[](index) ⇒ Object
31 32 33 |
# File 'lib/megam/core/predef_collection.rb', line 31 def [](index) @predefs[index] end |
#[]=(index, arg) ⇒ Object
35 36 37 38 39 |
# File 'lib/megam/core/predef_collection.rb', line 35 def []=(index, arg) is_megam_predef(arg) @predefs[index] = arg @predefs_by_name[arg.name] = index end |
#all_predefs ⇒ Object
27 28 29 |
# File 'lib/megam/core/predef_collection.rb', line 27 def all_predefs @predefs end |
#each ⇒ Object
72 73 74 75 76 |
# File 'lib/megam/core/predef_collection.rb', line 72 def each @predefs.each do |predef| yield predef end end |
#each_index ⇒ Object
78 79 80 81 82 |
# File 'lib/megam/core/predef_collection.rb', line 78 def each_index @predefs.each_index do |i| yield i end end |
#empty? ⇒ Boolean
84 85 86 |
# File 'lib/megam/core/predef_collection.rb', line 84 def empty? @predefs.empty? end |
#insert(predef) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/megam/core/predef_collection.rb', line 53 def insert(predef) is_megam_predef(predef) if @insert_after_idx # in the middle of executing a run, so any predefs inserted now should # be placed after the most recent addition done by the currently executing # predef @predefs.insert(@insert_after_idx + 1, predef) # update name -> location mappings and register new predef @predefs_by_name.each_key do |key| @predefs_by_name[key] += 1 if @predefs_by_name[key] > @insert_after_idx end @predefs_by_name[predef.name] = @insert_after_idx + 1 @insert_after_idx += 1 else @predefs << predef @predefs_by_name[predef.name] = @predefs.length - 1 end end |
#lookup(predef) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/megam/core/predef_collection.rb', line 88 def lookup(predef) lookup_by = nil if predef.kind_of?(Megam::Predef) lookup_by = predef.name elsif predef.kind_of?(String) lookup_by = predef else raise ArgumentError, "Must pass a Megam::Predef or String to lookup" end res = @predefs_by_name[lookup_by] unless res raise ArgumentError, "Cannot find a predef matching #{lookup_by} (did you define it first?)" end @predefs[res] end |
#to_hash ⇒ Object
Transform the ruby obj -> to a Hash
105 106 107 108 109 110 111 |
# File 'lib/megam/core/predef_collection.rb', line 105 def to_hash index_hash = Hash.new self.each do |predef| index_hash[predef.name] = predef.to_s end index_hash end |
#to_json(*a) ⇒ Object
Serialize this object as a hash: called from JsonCompat. Verify if this called from JsonCompat during testing.
115 116 117 |
# File 'lib/megam/core/predef_collection.rb', line 115 def to_json(*a) for_json.to_json(*a) end |