Module: WeChat::ActsAsWeChatEntity::ClassMethods

Defined in:
lib/we_chat/acts_as_we_chat_entity.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_we_chat_entity(entity_name, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/we_chat/acts_as_we_chat_entity.rb', line 21

def acts_as_we_chat_entity(entity_name, options = {})
  children_options = options[:children] || {}
  children_definition = {}

  module_including = "WeChat::#{entity_name.to_s.camelize}".constantize
  include module_including

  if module_including.const_defined?(:CHILDREN)
    children_definition = generate_children_definition(module_including.const_get(:CHILDREN), children_options)
  end

  define_children_defintion_method(children_definition)
  define_entity_name_method(entity_name)
end

#default_child_class(klass_option, default) ⇒ Object



47
48
49
50
# File 'lib/we_chat/acts_as_we_chat_entity.rb', line 47

def default_child_class(klass_option, default)
  return default.to_s.camelize.constantize if klass_option.nil? || klass_option[:klass].nil?
  klass_option[:klass]
end

#default_child_method(method_option, default) ⇒ Object



52
53
54
55
# File 'lib/we_chat/acts_as_we_chat_entity.rb', line 52

def default_child_method(method_option, default)
  return default.to_s.pluralize if method_option.nil? || method_option[:method].nil?
  method_option[:method]
end

#define_children_defintion_method(children_definition) ⇒ Object



63
64
65
66
67
# File 'lib/we_chat/acts_as_we_chat_entity.rb', line 63

def define_children_defintion_method(children_definition)
  define_singleton_method(:children_definition) do
    return children_definition
  end
end

#define_entity_name_method(entity_name) ⇒ Object



57
58
59
60
61
# File 'lib/we_chat/acts_as_we_chat_entity.rb', line 57

def define_entity_name_method(entity_name)
  define_singleton_method(:we_chat_entity_name) do
    return entity_name
  end
end

#generate_children_definition(module_children_definition, children_options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/we_chat/acts_as_we_chat_entity.rb', line 36

def generate_children_definition(module_children_definition, children_options)
  children_definition = {}
  module_children_definition.map do |child_key, child_data_key|
    option_item = children_options[child_key]
    child_klass = default_child_class(option_item, child_key)
    child_method = default_child_method(option_item, child_key)
    children_definition[child_key] = { klass: child_klass, method: child_method, data_key: child_data_key }
  end
  children_definition
end