Class: Puppet::Network::Format
- Includes:
- Confiner
- Defined in:
- lib/puppet/network/format.rb
Overview
A simple class for modeling encoding formats for moving instances around the network.
Instance Attribute Summary collapse
-
#charset ⇒ Object
Returns the value of attribute charset.
-
#extension ⇒ Object
Returns the value of attribute extension.
-
#intern_method ⇒ Object
Returns the value of attribute intern_method.
-
#intern_multiple_method ⇒ Object
Returns the value of attribute intern_multiple_method.
-
#mime ⇒ Object
Returns the value of attribute mime.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#render_method ⇒ Object
Returns the value of attribute render_method.
-
#render_multiple_method ⇒ Object
Returns the value of attribute render_multiple_method.
-
#required_methods ⇒ Object
Returns the value of attribute required_methods.
-
#weight ⇒ Object
Returns the value of attribute weight.
Instance Method Summary collapse
- #init_attribute(name, default) ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Format
constructor
A new instance of Format.
- #intern(klass, text) ⇒ Object
- #intern_multiple(klass, text) ⇒ Object
- #render(instance) ⇒ Object
- #render_multiple(instances) ⇒ Object
- #required_methods_present?(klass) ⇒ Boolean
- #supported?(klass) ⇒ Boolean
- #to_s ⇒ Object
Methods included from Confiner
#confine, #confine_collection, #suitable?
Constructor Details
#initialize(name, options = {}, &block) ⇒ Format
Returns a new instance of Format.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/puppet/network/format.rb', line 19 def initialize(name, = {}, &block) @name = name.to_s.downcase.intern = # This must be done early the values can be used to set required_methods define_method_names method_list = { :intern_method => "from_#{name}", :intern_multiple_method => "from_multiple_#{name}", :render_multiple_method => "to_multiple_#{name}", :render_method => "to_#{name}" } init_attribute(:mime, "text/#{name}") init_attribute(:weight, 5) init_attribute(:required_methods, method_list.keys) init_attribute(:extension, name.to_s) init_attribute(:charset, nil) method_list.each do |method, value| init_attribute(method, value) end raise ArgumentError, _("Unsupported option(s) %{options_list}") % { options_list: .keys } unless .empty? = nil instance_eval(&block) if block_given? end |
Instance Attribute Details
#charset ⇒ Object
Returns the value of attribute charset.
10 11 12 |
# File 'lib/puppet/network/format.rb', line 10 def charset @charset end |
#extension ⇒ Object
Returns the value of attribute extension.
10 11 12 |
# File 'lib/puppet/network/format.rb', line 10 def extension @extension end |
#intern_method ⇒ Object
Returns the value of attribute intern_method.
10 11 12 |
# File 'lib/puppet/network/format.rb', line 10 def intern_method @intern_method end |
#intern_multiple_method ⇒ Object
Returns the value of attribute intern_multiple_method.
10 11 12 |
# File 'lib/puppet/network/format.rb', line 10 def intern_multiple_method @intern_multiple_method end |
#mime ⇒ Object
Returns the value of attribute mime.
9 10 11 |
# File 'lib/puppet/network/format.rb', line 9 def mime @mime end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/puppet/network/format.rb', line 9 def name @name end |
#render_method ⇒ Object
Returns the value of attribute render_method.
10 11 12 |
# File 'lib/puppet/network/format.rb', line 10 def render_method @render_method end |
#render_multiple_method ⇒ Object
Returns the value of attribute render_multiple_method.
10 11 12 |
# File 'lib/puppet/network/format.rb', line 10 def render_multiple_method @render_multiple_method end |
#required_methods ⇒ Object
Returns the value of attribute required_methods.
10 11 12 |
# File 'lib/puppet/network/format.rb', line 10 def required_methods @required_methods end |
#weight ⇒ Object
Returns the value of attribute weight.
10 11 12 |
# File 'lib/puppet/network/format.rb', line 10 def weight @weight end |
Instance Method Details
#init_attribute(name, default) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/puppet/network/format.rb', line 12 def init_attribute(name, default) value = .delete(name) value = default if value.nil? self.send(name.to_s + "=", value) end |
#intern(klass, text) ⇒ Object
51 52 53 54 |
# File 'lib/puppet/network/format.rb', line 51 def intern(klass, text) return klass.send(intern_method, text) if klass.respond_to?(intern_method) raise NotImplementedError, "#{klass} does not respond to #{intern_method}; can not intern instances from #{mime}" end |
#intern_multiple(klass, text) ⇒ Object
56 57 58 59 |
# File 'lib/puppet/network/format.rb', line 56 def intern_multiple(klass, text) return klass.send(intern_multiple_method, text) if klass.respond_to?(intern_multiple_method) raise NotImplementedError, "#{klass} does not respond to #{intern_multiple_method}; can not intern multiple instances from #{mime}" end |
#render(instance) ⇒ Object
65 66 67 68 |
# File 'lib/puppet/network/format.rb', line 65 def render(instance) return instance.send(render_method) if instance.respond_to?(render_method) raise NotImplementedError, "#{instance.class} does not respond to #{render_method}; can not render instances to #{mime}" end |
#render_multiple(instances) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/puppet/network/format.rb', line 70 def render_multiple(instances) # This method implicitly assumes that all instances are of the same type. return instances[0].class.send(render_multiple_method, instances) if instances[0].class.respond_to?(render_multiple_method) raise NotImplementedError, _("%{klass} does not respond to %{method}; can not render multiple instances to %{mime}") % { klass: instances[0].class, method: render_multiple_method, mime: mime } end |
#required_methods_present?(klass) ⇒ Boolean
77 78 79 80 81 82 83 84 85 |
# File 'lib/puppet/network/format.rb', line 77 def required_methods_present?(klass) [:intern_method, :intern_multiple_method, :render_multiple_method].each do |name| return false unless required_method_present?(name, klass, :class) end return false unless required_method_present?(:render_method, klass, :instance) true end |
#supported?(klass) ⇒ Boolean
87 88 89 |
# File 'lib/puppet/network/format.rb', line 87 def supported?(klass) suitable? and required_methods_present?(klass) end |
#to_s ⇒ Object
91 92 93 |
# File 'lib/puppet/network/format.rb', line 91 def to_s "Puppet::Network::Format[#{name}]" end |