Class: Higo::Configurable
- Inherits:
-
Object
- Object
- Higo::Configurable
- Defined in:
- lib/higo/configurable.rb
Class Method Summary collapse
-
.configure(path: nil, &block) ⇒ Configurable
Takes any arbitrary code block which defines getter and setters and returns a Higo::Configurable object that returns instance variables, getter, setter and predicate methods.
- .inherited(mod) ⇒ Object
Instance Method Summary collapse
-
#initialize(file) ⇒ Configurable
constructor
A new instance of Configurable.
-
#method_missing(meth, *args, &block) ⇒ Object
If an existing class defines, method_missing that incantation is run, otherwise all unknown methods are defined as getter, setters and predicate methods.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(file) ⇒ Configurable
Returns a new instance of Configurable.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/higo/configurable.rb', line 34 def initialize(file) if file values = if file =~ /http/ Transport::Web.transport(path: file).finish elsif file =~ /.?(txt|json)/ Transport::File.transport(path: file).finish else raise 'Unable to read this type of file' end values.each do |key,value| self.class.__send__(:attr_accessor, key) self.class.__send__(:define_method, "#{key}?", proc { !!value }) instance_variable_set("@#{key}", value) end else super() end self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
If an existing class defines, method_missing that incantation is run, otherwise all unknown methods are defined as getter, setters and predicate methods
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/higo/configurable.rb', line 60 def method_missing(meth, *args, &block) # check for a variable and then delegate to that method missing implementation # should instruct user to call super() as well klass = self.class super(meth, *args, &block) if respond_to?(meth) ivar = convert_method_to_symbol(meth) data = args.size == 1 ? args.first : args klass.__send__(:attr_accessor, ivar) klass.__send__(:define_method, "#{ivar}?", proc { !!data }) instance_variable_set("@#{ivar}", data) self end |
Class Method Details
.configure(path: nil, &block) ⇒ Configurable
Takes any arbitrary code block which defines getter and setters and returns a Higo::Configurable object that returns instance variables, getter, setter and predicate methods
24 25 26 27 28 |
# File 'lib/higo/configurable.rb', line 24 def self.configure(path:nil, &block) configuration = generate_configurable(path) configuration.instance_eval(&block) if block_given? return configuration end |
.inherited(mod) ⇒ Object
30 31 32 |
# File 'lib/higo/configurable.rb', line 30 def self.inherited(mod) # If the class is inherited write to a variable end |
Instance Method Details
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
54 55 56 |
# File 'lib/higo/configurable.rb', line 54 def respond_to_missing?(method_name, include_private = false) super unless self.instance_variables.include?(convert_method_to_symbol(method_name)) end |