Class: Wedge::Config

Inherits:
Object show all
Includes:
Methods
Defined in:
lib/wedge/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Methods

#client?, included, #server?

Constructor Details

#initialize(opts = {}) ⇒ Config

Setup initial opts values

Parameters:

  • opts (Hash) (defaults to: {})

    The initial params for #opts.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wedge/config.rb', line 16

def initialize(opts = {})
  opts = {
    cache_assets: false,
    assets_key: false,
    tmpl: IndifferentHash.new,
    scope: false,
    loaded: false,
    requires: [],
    on: [],
    on_server_methods: [],
    object_events: {},
    is_plugin: false,
    assets_url: '/assets/wedge',
    plugins: []
  }.merge opts

  @opts = OpenStruct.new(opts)
end

Instance Attribute Details

#optsOpenStruct

Stores the options for the config

Returns:

  • (OpenStruct)


11
12
13
# File 'lib/wedge/config.rb', line 11

def opts
  @opts
end

Instance Method Details

#domObject

Used to set and update the dom



58
59
60
61
62
# File 'lib/wedge/config.rb', line 58

def dom
  if server?
    yield
  end
end

#get_requires(requires = false, previous_requires = []) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wedge/config.rb', line 100

def get_requires(requires = false, previous_requires = [])
  list = []

  unless requires
    requires ||= opts.requires.dup
    previous_requires << opts.name.to_sym
  end

  previous_requires.each { |p| requires.delete(p) }

  requires.each do |r|
    klass = Wedge.components[r.to_sym].klass
    o = klass.client_wedge_opts.select do |k, v|
      %w(path_name name requires).include? k.to_s
    end

    # We don't want to get a stack limit error so we stop something
    # requiring itself
    pr = previous_requires.dup << o[:name].to_sym

    o[:requires] = get_requires o[:requires].dup, pr if o[:requires].present?

    list << o
  end

  list
end

#html(html) ⇒ Object

Set the raw html

Parameters:



66
67
68
69
70
71
72
73
74
# File 'lib/wedge/config.rb', line 66

def html(html)
  unless RUBY_ENGINE == 'opal'
    opts.html = begin
      File.read html
    rescue
      html
    end.strip
  end
end

#is_plugin?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/wedge/config.rb', line 47

def is_plugin?
  opts.is_plugin
end

#name(*names) ⇒ Object

Set the unique name of the component

Parameters:

  • name (<String, Symbol>, #to_sym)


38
39
40
41
42
43
44
45
# File 'lib/wedge/config.rb', line 38

def name(*names)
  names.each do |name|
    opts.name = name.to_sym
    opts.is_plugin = true if name.to_s =~ /_plugin$/
    Wedge.components ||= {}
    Wedge.components[opts.name] = opts
  end
end

#opts_dupObject



87
88
89
# File 'lib/wedge/config.rb', line 87

def opts_dup
  opts.to_h.inject({}) {|copy, (key, value)| copy[key] = value.dup rescue value; copy}
end

#plugin(name) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/wedge/config.rb', line 91

def plugin(name)
  unless RUBY_ENGINE == 'opal'
    require "wedge/plugins/#{name}"
    klass = Wedge.components[:"#{name}_plugin"].klass
    Wedge::Component.include(klass::InstanceMethods) if defined?(klass::InstanceMethods)
    Wedge::Component.extend(klass::ClassMethods) if defined?(klass::ClassMethods)
  end
end

#requires(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/wedge/config.rb', line 76

def requires(*args)
  unless RUBY_ENGINE == 'opal'
    args.each do |a|
      if a.to_s[/_plugin$/]
        require "wedge/plugins/#{a.to_s.gsub(/_plugin$/, '')}"
      end
      opts.requires << a
    end
  end
end