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.1.3'

Class Method Summary collapse

Instance Method Summary collapse

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.
Every other call will be passed to super.


85
86
87
88
89
90
91
92
# File 'lib/simplify_api/simplify_api.rb', line 85

def method_missing(method_name, *args, &block)
  case method_name
  when /(.*)\=$/
    create_and_set_instance_variable($1.to_s, args[0])
  else
    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
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/simplify_api/simplify_api.rb', line 33

def initialize(opts = {})
  opts.symbolize_keys!
  # opts = { klass_attr.first[0] => opts } if opts.class == Array

  klass_attr.each_pair do |name, spec|
    params = spec[:params]
    if opts.key?(name)
      value = process_value(name, opts[name])
      opts.delete(name)
    else
      value = params[:default]
    end
    raise ArgumentError, "Missing mandatory attribute => #{name}" if params[:mandatory] & value.nil?

    create_and_set_instance_variable(name.to_s, value)
  end

  opts.each_pair do |key, value|
    expanded_value = process_value(key, value)
    create_and_set_instance_variable(key.to_s, expanded_value)
  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 assignment call.
Every other type should be passed to super.

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
# File 'lib/simplify_api/simplify_api.rb', line 100

def respond_to_missing?(method_name, *args)
  case method_name
  when /(.*)\=$/
    true # always respond to assignment methods

  else
    super(method_name, args)
  end
end

#to_hObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simplify_api/simplify_api.rb', line 55

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_jsonObject



75
76
77
# File 'lib/simplify_api/simplify_api.rb', line 75

def to_json
  self.to_h.to_json
end