Class: Meta::Pool

Inherits:
Data
  • Object
show all
Includes:
Enumerable
Defined in:
lib/meta.rb

Overview

TODO: this class could be generalized

Instance Attribute Summary collapse

Attributes inherited from Data

#metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#average, #sample_variance, #standard_deviation, #sum

Methods inherited from Data

#==, #[], #has_tag?, #has_value?, #name, #name=

Constructor Details

#initialize(name = SecureRandom.uuid) ⇒ Pool

Returns a new instance of Pool.



72
73
74
75
# File 'lib/meta.rb', line 72

def initialize(name=SecureRandom.uuid)
  super(name)
  @pool = {}
end

Instance Attribute Details

#poolObject

include Data



71
72
73
# File 'lib/meta.rb', line 71

def pool
  @pool
end

Class Method Details

.json_create(o) ⇒ Object



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

def self.json_create(o)
  me = new(o["name"])
  me.pool=o["pool"]
  me
end

Instance Method Details

#add(element) ⇒ Object Also known as: <<



98
99
100
101
102
103
104
105
106
# File 'lib/meta.rb', line 98

def add(element)
  unless element.nil?
    if @pool.key? element.name #TODO I don't know if this is correct.
      @pool[element.name]..merge! element.
    else
      @pool[element.name]=element
    end
  end
end

#each(&block) ⇒ Object



91
92
93
# File 'lib/meta.rb', line 91

def each &block
  @pool.each_pair{|name, member| block.call(member)}
end

#empty?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/meta.rb', line 109

def empty?
  @pool.empty?
end

#get(name_or_tag_or_value, value = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/meta.rb', line 117

def get(name_or_tag_or_value, value=nil)
  # TODO implement recursive query or passing multiple values as hash, insercet or etc.....
  #       if name_or_tag_or_value.is_a? Hash
  #         name_or_tag_or_value.each_pair  do |tag, value|
  #
  #         end
  #       else
  if value
    get_by_tag_and_value(name_or_tag_or_value, value)
  else
    get_by_name(name_or_tag_or_value) || get_by_tag(name_or_tag_or_value) || get_by_value(name_or_tag_or_value) || get_down_to_childer(name_or_tag_or_value)
  end
  # end
end

#get_by_name(name) ⇒ Object

get



132
133
134
# File 'lib/meta.rb', line 132

def get_by_name(name)
  @pool[name]
end

#get_by_tag(tag) ⇒ Object

get_by_name



136
137
138
# File 'lib/meta.rb', line 136

def get_by_tag(tag)
  get_generic :tag, tag
end

#get_by_tag_and_value(tag, val) ⇒ Object

get_by_value



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/meta.rb', line 144

def get_by_tag_and_value(tag, val)
  ret_pool = Pool.new
  @pool.each_pair do |name, meta|
    if meta.has_tag?(tag) && meta[tag]==val
      ret_pool.add meta
    else
      @pool.each_pair do |name, element|
        ret_pool.add element.get_by_tag_and_value(tag, val) if element.respond_to?(:get_by_tag_and_value) && element.respond_to?(:pool)
      end
    end
  end
  ret_pool unless ret_pool.empty?
end

#get_by_value(val) ⇒ Object

get_by_tag



140
141
142
# File 'lib/meta.rb', line 140

def get_by_value(val)
  get_generic :value, val
end

#get_down_to_childer(x) ⇒ Object

get_by_tag_and_value



158
159
160
161
162
163
164
# File 'lib/meta.rb', line 158

def get_down_to_childer(x)
  ret_pool = Pool.new
  @pool.each_pair do |name, element|
    ret_pool.add element.get(x) if element.respond_to?(:get) && element.respond_to?(:pool)
  end
  ret_pool unless ret_pool.empty?
end

#namesObject



113
114
115
# File 'lib/meta.rb', line 113

def names
  @pool.keys
end

#to_json(*a) ⇒ Object



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

def to_json(*a)
  {
    "json_class"   => self.class.name,
    "name"         => name,
    "pool"     => pool
  }.to_json(*a)
end