Class: Qwe::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/qwe/attribute.rb

Constant Summary collapse

@@possible_params =
{}
@@extensions =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, *booleans, writer: nil, reader: nil, **params) ⇒ Attribute

Returns a new instance of Attribute.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/qwe/attribute.rb', line 14

def initialize(klass, name, *booleans, writer: nil, reader: nil, **params)
  @klass = klass
  @name = name
  @params = params
  raise "Parameter 'name' is reserved" if @params[:name]
  @params[:name] = name
  booleans.each { |b| @params[b] = true }

  @params.each_key do |p|
    raise "Unknown parameter #{p}" unless @@possible_params.include?(p)
  end

  @reader = if reader
    if reader.is_a?(Proc)
      reader
    else
      raise "Reader parameter should be a proc, #{reader.class} given"
    end
  else
    Qwe::Function.new(klass, name)
  end

  @writer = Qwe::Function.new(klass, :"#{name}=")
  if writer
    if writer.is_a?(Proc)
      klass.define_method(:"#{name}_writer", &writer)
      @writer.stage("value = #{name}_writer(value)")
    else
      raise "writer parameter should be a Proc, #{writer.class} given"
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/qwe/attribute.rb', line 81

def method_missing(symbol, *args)
  if @@possible_params.has_key?(symbol)
    @params[symbol]
  else
    super
  end
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



12
13
14
# File 'lib/qwe/attribute.rb', line 12

def klass
  @klass
end

#paramsObject

Returns the value of attribute params.



12
13
14
# File 'lib/qwe/attribute.rb', line 12

def params
  @params
end

Class Method Details

.add(p = nil, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/qwe/attribute.rb', line 48

def self.add(p = nil, &block)
  if p
    @@possible_params[p] = true
    if block
      @@extensions.push(proc { instance_exec(&block) if self[p] })
    end
  elsif block
    @@extensions.push(block)
  else
    raise ArgumentError.new("Attribute.add - param or block required")
  end
end

Instance Method Details

#[](key) ⇒ Object



93
94
95
# File 'lib/qwe/attribute.rb', line 93

def [](key)
  @params[key]
end

#compileObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/qwe/attribute.rb', line 61

def compile
  writer.arg(:value)

  @@extensions.each do |block|
    instance_exec(&block)
  end

  if reader.is_a?(Function)
    reader.stage "@#{name}"
    reader.compile
  else
    klass.define_method(name, &reader)
  end

  writer.stage("@#{name} = value")
  writer.stage("root&.record&.commit(\"\#{to_rb}.instance_variable_set(:@#{name}, \#{value.to_rb})\\n\")")
  writer.stage("value")
  writer.compile
end

#init_or_default_stage(p) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/qwe/attribute.rb', line 101

def init_or_default_stage(p)
  if self[p].is_a?(Class)
    "#{self[p]}.new"
  else
    klass.class_variable_set(:"@@#{name}_#{p}", self[p])
    if self[p].is_a?(Proc)
      "instance_exec(&@@#{name}_#{p})"
    else
      "@@#{name}_#{p}.clone"
    end
  end
end

#inspectObject



97
98
99
# File 'lib/qwe/attribute.rb', line 97

def inspect
  "Attribute #{klass}.#{params}"
end

#respond_to_missing?(symbol, *args) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/qwe/attribute.rb', line 89

def respond_to_missing?(symbol, *args)
  @@possible_params[symbol] || super
end