Class: Spider::WidgetAttributes

Inherits:
Hash
  • Object
show all
Defined in:
lib/spiderfw/widget/widget_attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(widget) ⇒ WidgetAttributes

Returns a new instance of WidgetAttributes.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/spiderfw/widget/widget_attributes.rb', line 5

def initialize(widget)
    @widget = widget
    @attributes = widget.class.attributes
    @attributes.each do |k, params|
        if params[:default]
            if (params[:default].is_a?(Proc))
                self[k] = params[:default].call
            else
                self[k] = params[:default]
            end
        end
    end
end

Instance Method Details

#[](k) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/spiderfw/widget/widget_attributes.rb', line 40

def [](k)
    return nil unless @attributes[k]
    params = @attributes[k]
    v = super
    if (!v)
        return @widget.instance_variable_get("@#{k}") if params[:instance_attr]
        return nil
    end
    return v
end

#[]=(k, v) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/spiderfw/widget/widget_attributes.rb', line 19

def []=(k, v)
    params = @attributes[k]
    raise ArgumentError, "#{k} is not an allowed attribute for widget #{@widget}" unless params
    raise ArgumentError, "#{k} is not in the correct format" if params[:format] && v !=~ params[:format]
    if (params[:type])
        case params[:type].name
        when 'String'
            v = v.to_s
        when 'Symbol'
            v = v.to_sym if v
        when 'TrueClass', 'FalseClass', 'Spider::DataTypes::Bool'
            v = (v.to_s == 'false' || v.to_s.empty?) ? false : true
        when 'Fixnum'
            v = v.to_i
        end
    end
    v = params[:process].call(v) if params[:process] && v
    @widget.instance_variable_set("@#{k}", v) if params[:set_var]
    super(k, v)
end