Class: Codily::Root
- Inherits:
-
Object
- Object
- Codily::Root
- Defined in:
- lib/codily/root.rb
Defined Under Namespace
Classes: AlreadyDefined
Instance Attribute Summary collapse
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
-
#service_filter ⇒ Object
readonly
Returns the value of attribute service_filter.
-
#service_versions ⇒ Object
readonly
Returns the value of attribute service_versions.
Instance Method Summary collapse
- #add_element(element) ⇒ Object
- #all_elements ⇒ Object
- #as_dsl_hash ⇒ Object
- #as_hash ⇒ Object
- #find_element(element) ⇒ Object
-
#initialize(debug: false, service_filter: nil) ⇒ Root
constructor
A new instance of Root.
- #list_element(klass) ⇒ Object
- #run_block(&block) ⇒ Object
- #run_string(str, file = '(eval)', line = 1) ⇒ Object
- #service(name, &block) ⇒ Object
- #service_version_get(name_or_id) ⇒ Object
-
#service_version_set(service_name, service_id, versions) ⇒ Object
XXX: is it okay having this here?.
- #services ⇒ Object
Constructor Details
#initialize(debug: false, service_filter: nil) ⇒ Root
Returns a new instance of Root.
8 9 10 11 12 13 14 15 |
# File 'lib/codily/root.rb', line 8 def initialize(debug: false, service_filter: nil) @debug = debug @elements = {} @service_filter = service_filter @service_versions = {} @service_map_name_to_id = {} end |
Instance Attribute Details
#debug ⇒ Object
Returns the value of attribute debug.
20 21 22 |
# File 'lib/codily/root.rb', line 20 def debug @debug end |
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
17 18 19 |
# File 'lib/codily/root.rb', line 17 def elements @elements end |
#service_filter ⇒ Object (readonly)
Returns the value of attribute service_filter.
18 19 20 |
# File 'lib/codily/root.rb', line 18 def service_filter @service_filter end |
#service_versions ⇒ Object (readonly)
Returns the value of attribute service_versions.
19 20 21 |
# File 'lib/codily/root.rb', line 19 def service_versions @service_versions end |
Instance Method Details
#add_element(element) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/codily/root.rb', line 70 def add_element(element) h = (@elements[element.class] ||= {}) raise AlreadyDefined, "#{element.class.name}(#{element.key}) is already defined: (#{h.keys.inspect})" if h.key?(element.key) if debug puts "DEBUG: #{self.class}/#{'%x' % self.__id__}(add_element): #{element.class}(#{element.key.inspect}) #{element.as_hash.inspect}" end h[element.key] = element end |
#all_elements ⇒ Object
87 88 89 |
# File 'lib/codily/root.rb', line 87 def all_elements @elements.each_value.flat_map(&:values) end |
#as_dsl_hash ⇒ Object
26 27 28 |
# File 'lib/codily/root.rb', line 26 def as_dsl_hash as_hash end |
#as_hash ⇒ Object
22 23 24 |
# File 'lib/codily/root.rb', line 22 def as_hash {} end |
#find_element(element) ⇒ Object
83 84 85 |
# File 'lib/codily/root.rb', line 83 def find_element(element) list_element(element.class)[element.key] end |
#list_element(klass) ⇒ Object
79 80 81 |
# File 'lib/codily/root.rb', line 79 def list_element(klass) @elements[klass] ||= {} end |
#run_block(&block) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/codily/root.rb', line 57 def run_block(&block) raise ArgumentError, 'block not given' unless block instance_eval &block self end |
#run_string(str, file = '(eval)', line = 1) ⇒ Object
64 65 66 67 |
# File 'lib/codily/root.rb', line 64 def run_string(str, file = '(eval)', line = 1) instance_eval str, file, line self end |
#service(name, &block) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/codily/root.rb', line 47 def service(name, &block) if service_filter return nil unless service_filter.any? { |_| _ === name } end raise AlreadyDefined if services.key?(name) add_element(Elements::Service.new(self, {name: name}, &block)) end |
#service_version_get(name_or_id) ⇒ Object
39 40 41 |
# File 'lib/codily/root.rb', line 39 def service_version_get(name_or_id) @service_versions[@service_map_name_to_id[name_or_id] || name_or_id] end |
#service_version_set(service_name, service_id, versions) ⇒ Object
XXX: is it okay having this here?
31 32 33 34 35 36 37 |
# File 'lib/codily/root.rb', line 31 def service_version_set(service_name, service_id, versions) @service_map_name_to_id[service_name] = service_id dev_version = versions.reverse_each.find { |_| !_.locked } dev = dev_version && dev_version.number active = (versions.reverse_each.find(&:active) || versions.reverse_each.find(&:locked)).number @service_versions[service_id] = {dev: dev, active: active, name: service_name, id: service_id} end |
#services ⇒ Object
43 44 45 |
# File 'lib/codily/root.rb', line 43 def services list_element(Elements::Service) end |