Module: SimplifyApi
- Defined in:
- lib/simplify_api/simplify_api.rb,
lib/simplify_api/version.rb
Overview
SimplifyApi
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
'0.2.0'
Class Method Summary collapse
Instance Method Summary collapse
- #initialize(opts = {}) ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
method_missing Called in case an unexisting method is called.
-
#respond_to_missing?(method_name, *args) ⇒ Boolean
respond_to_mssing? Called to check if an instance respond to a message.
- #to_h ⇒ Object
- #to_json ⇒ Object
- #valid? ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
method_missing
Called in case an unexisting method is called.
To an assignment call it will create the instance variable.
To an getter of a specified attribute, return the default value.
Every other call will be passed to super.
73 74 75 76 77 78 79 80 81 |
# File 'lib/simplify_api/simplify_api.rb', line 73 def method_missing(method_name, *args, &block) if method_name.match(/(.*)\=$/) create_and_set_instance_variable($1.to_s, args[0]) else return klass_attr.dig(method_name.to_sym, :params, :default) if klass_attr.key?(method_name.to_sym) super(method_name, args, block) end end |
Class Method Details
.included(base) ⇒ Object
5 6 7 8 9 |
# File 'lib/simplify_api/simplify_api.rb', line 5 def self.included(base) base.singleton_class.send(:attr_accessor, :attributes) base.attributes = {} base.extend(ClassMethods) end |
Instance Method Details
#initialize(opts = {}) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/simplify_api/simplify_api.rb', line 33 def initialize(opts = {}) opts.symbolize_keys! opts.each_pair do |key, value| = process_value(key, value) create_and_set_instance_variable(key.to_s, ) end end |
#respond_to_missing?(method_name, *args) ⇒ Boolean
respond_to_mssing?
Called to check if an instance respond to a message.
It should respond to any defined attribute (getter & setter).
Every other type should be passed to super.
89 90 91 92 93 94 95 |
# File 'lib/simplify_api/simplify_api.rb', line 89 def respond_to_missing?(method_name, *args) return true if method_name.match(/(.*)\=$/) return true if klass_attr.key?(method_name.to_sym) super(method_name, args) end |
#to_h ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/simplify_api/simplify_api.rb', line 42 def to_h h = {} instance_variables.each do |i| k = /\@(.*)/.match(i.to_s)[1].to_sym v = instance_variable_get(i) if v.class == Array then r = [] v.each { |a| r << (a.respond_to?(:to_h) ? a.to_h : a) } if klass_attr[k][:params][:invisible] then h = r else h[k] = r end else h[k] = v.respond_to?(:to_h) ? v.to_h : v end end return h end |
#to_json ⇒ Object
62 63 64 |
# File 'lib/simplify_api/simplify_api.rb', line 62 def to_json self.to_h.to_json end |
#valid? ⇒ Boolean
97 98 99 100 101 102 103 |
# File 'lib/simplify_api/simplify_api.rb', line 97 def valid? klass_attr.each_pair do |key, value| puts "Key: #{key}" return false if value.dig(:params, :mandatory) & !instance_variable_defined?("@#{key.to_s}".to_sym) end true end |