Module: Wardrobe::ClassMethods

Extended by:
Forwardable
Defined in:
lib/wardrobe/class_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



10
11
12
13
14
# File 'lib/wardrobe/class_methods.rb', line 10

def self.extended(base)
  wardrobe_methods = base.instance_variable_set(:@wardrobe_methods, Module.new)
  base.include(wardrobe_methods)
  base.instance_variable_set(:@wardrobe_stores, Stores.new)
end

Instance Method Details

#attribute(name, klass, *args, &blk) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/wardrobe/class_methods.rb', line 83

def attribute(name, klass, *args, &blk)
  merged_args = option_store.defaults.merge(args.inject({}) { |input, val| input.merge! val })
  @wardrobe_stores = wardrobe_stores.add_attribute(
    name, klass, self, **merged_args, &blk
  )
  define_getter(attribute_store[name])
  define_setter(attribute_store[name])
end

#attributes(**kargs, &blk) ⇒ Object



92
93
94
# File 'lib/wardrobe/class_methods.rb', line 92

def attributes(**kargs, &blk)
  BlockSetup.new(self).run(**kargs, &blk)
end

#default_gettersObject



46
47
48
49
50
# File 'lib/wardrobe/class_methods.rb', line 46

def default_getters
  [
    Wardrobe.getters[:getter]
  ]
end

#default_settersObject



52
53
54
55
56
# File 'lib/wardrobe/class_methods.rb', line 52

def default_setters
  [
    Wardrobe.setters[:setter]
  ]
end

#define_getter(atr) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/wardrobe/class_methods.rb', line 62

def define_getter(atr)
  @wardrobe_methods.instance_exec do
    define_method(atr.name) do
      atr.getters.inject(nil) do |val, getter|
        getter.block.call(val, atr, self)
      end
    end
  end
end

#define_setter(atr) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/wardrobe/class_methods.rb', line 72

def define_setter(atr)
  @wardrobe_methods.instance_exec do
    define_method(atr.setter_name) do |input|
      atr.setters.inject(input) do |val, setter|
        setter.block.call(val, atr, self)
      end
    end
  end
end

#included(base) ⇒ Object

This is called when included in another module/class



17
18
19
20
# File 'lib/wardrobe/class_methods.rb', line 17

def included(base)
  base.include(Wardrobe) unless base.respond_to? :wardrobe_stores
  base.merge(wardrobe_stores)
end

#inherited(child) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/wardrobe/class_methods.rb', line 22

def inherited(child)
  wardrobe_methods = child.instance_variable_set(:@wardrobe_methods, Module.new)
  child.include(wardrobe_methods)
  child.instance_variable_set(:@wardrobe_stores, Stores.new)
  child.merge(wardrobe_stores)
  child.root_config = root_config
end

#merge(config) ⇒ Object



58
59
60
# File 'lib/wardrobe/class_methods.rb', line 58

def merge(config)
  @wardrobe_stores = wardrobe_stores.merge(config, self)
end

#plugin(name, **args) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/wardrobe/class_methods.rb', line 104

def plugin(name, **args)
  @wardrobe_stores = wardrobe_stores.enable_plugin(name, **args)
  plugin = plugin_store[name][:klass]

  if plugin.const_defined?(:ClassMethods)
    extend(plugin.const_get(:ClassMethods))
  end

  if plugin.const_defined?(:InstanceMethods)
    include(plugin.const_get(:InstanceMethods))
  end

  # Currently these are not needed
  #
  # if plugin.const_defined?(:AttributeClassMethods)
  #   Attribute.extend(plugin.const_get(:AttributeClassMethods))
  # end
  #
  # if plugin.const_defined?(:AttributeInstanceMethods)
  #   Attribute.include(plugin.const_get(:AttributeInstanceMethods))
  # end
end

#remove_attributes(*wardrobe) ⇒ Object Also known as: remove_attribute



96
97
98
99
100
# File 'lib/wardrobe/class_methods.rb', line 96

def remove_attributes(*wardrobe)
  wardrobe.each do |name|
    @wardrobe_stores = wardrobe_stores.remove_attribute(name)
  end
end

#root_configObject



42
43
44
# File 'lib/wardrobe/class_methods.rb', line 42

def root_config
  @root_config if instance_variable_defined?(:@root_config)
end

#root_config=(input) ⇒ Object



38
39
40
# File 'lib/wardrobe/class_methods.rb', line 38

def root_config=(input)
  @root_config = input
end

#wardrobe_stores(&blk) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/wardrobe/class_methods.rb', line 30

def wardrobe_stores(&blk)
  if block_given?
    @wardrobe_stores = wardrobe_stores.update(&blk)
  else
    @wardrobe_stores
  end
end