Class: Reflex::Model

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Xot::Invoker, Xot::Setter
Defined in:
lib/reflex/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, &block) ⇒ Model

Returns a new instance of Model.



18
19
20
21
# File 'lib/reflex/model.rb', line 18

def initialize(data = nil, &block)
  self.data = data if data
  Xot::BlockUtil.instance_eval_or_block_call self, &block if block
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



16
17
18
# File 'lib/reflex/model.rb', line 16

def data
  @data
end

Instance Method Details

#[](*args) ⇒ Object



89
90
91
# File 'lib/reflex/model.rb', line 89

def [](*args)
  call_symbols ARRAY_GET_SYMS, *args
end

#[]=(*args) ⇒ Object



93
94
95
96
97
# File 'lib/reflex/model.rb', line 93

def []=(*args)
  ret = call_symbols ARRAY_SET_SYMS, *args
  invoke :update
  ret
end

#add_filter(&block) ⇒ Object Also known as: filter



50
51
52
# File 'lib/reflex/model.rb', line 50

def add_filter(&block)
  (@filters ||= []).push block
end

#add_handler(handler) ⇒ Object



35
36
37
38
# File 'lib/reflex/model.rb', line 35

def add_handler(handler)
  super
  invoke :attach, {}, only: handler
end

#each(&block) ⇒ Object



103
104
105
# File 'lib/reflex/model.rb', line 103

def each(&block)
  data.each(&block) if block && data.respond_to?(:each)
end

#float(defvar = nil) ⇒ Object



81
82
83
# File 'lib/reflex/model.rb', line 81

def float(defvar = nil)
  call_symbols FLOAT_SYMS
end

#inspectObject



107
108
109
# File 'lib/reflex/model.rb', line 107

def inspect()
  "#<Model data:#{@data.inspect}>"
end

#int(defvar = nil) ⇒ Object



77
78
79
# File 'lib/reflex/model.rb', line 77

def int(defvar = nil)
  call_symbols INT_SYMS
end

#invoke(name, attributes = {}, options = {}) ⇒ Object



45
46
47
48
# File 'lib/reflex/model.rb', line 45

def invoke(name, attributes = {}, options = {})
  attributes[:data] ||= @data
  super "on_data_#{name}".intern, attributes, options
end

#max=(max) ⇒ Object



68
69
70
# File 'lib/reflex/model.rb', line 68

def max=(max)
  filter {|data| data > max ? max : data}
end

#min=(min) ⇒ Object



64
65
66
# File 'lib/reflex/model.rb', line 64

def min=(min)
  filter {|data| data < min ? min : data}
end

#minmax=(range) ⇒ Object



72
73
74
75
# File 'lib/reflex/model.rb', line 72

def minmax=(range)
  min, max = range.min, range.max
  filter {|data| data < min ? min : data > max ? max : data}
end

#remove_filter(&block) ⇒ Object



54
55
56
# File 'lib/reflex/model.rb', line 54

def remove_filter(&block)
  @filters.delete block if @filters
end

#remove_handler(handler) ⇒ Object



40
41
42
43
# File 'lib/reflex/model.rb', line 40

def remove_handler(handler)
  invoke :detach, {}, only: handler
  super
end

#sizeObject



99
100
101
# File 'lib/reflex/model.rb', line 99

def size()
  call_symbols(ARRAY_SIZE_SYMS) || 0
end

#string(defvar = nil) ⇒ Object



85
86
87
# File 'lib/reflex/model.rb', line 85

def string(defvar = nil)
  call_symbols STRING_SYMS
end

#validate(&block) ⇒ Object



60
61
62
# File 'lib/reflex/model.rb', line 60

def validate(&block)
  filter {|data| block.call(data) ? data : nil}
end