Class: ActionBlocks::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/action_blocks/store.rb

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



6
7
8
9
10
# File 'lib/action_blocks/store.rb', line 6

def initialize
  super
  @block_store = {};
  @validation_store = [];
end

Instance Method Details

#add_to_validator(builder) ⇒ Object



115
116
117
# File 'lib/action_blocks/store.rb', line 115

def add_to_validator(builder)
  @validation_store << builder
end

#after_loadObject



144
145
146
147
148
# File 'lib/action_blocks/store.rb', line 144

def after_load
  @validation_store.each do |v|
    v.after_load
  end
end

#authorization(*p, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/action_blocks/store.rb', line 33

def authorization(*p, &block)
  rs = ActionBlocks::AuthorizationBuilder.new()
  rs.active_model = p.first
  rs.id = rs.active_model.to_s.underscore
  rs.before_build(nil, *p)
  store(rs)
  add_to_validator(rs)
  rs.evaluate(&block) if block
  rs.after_build(nil, *p)
end

#errorsObject

def errors

results = []
@validation_store.each do |b|
  results << b.errors if b.invalid?
end
results

end



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/action_blocks/store.rb', line 70

def errors
  results = []
  @validation_store.each do |b|
    results << {
      builder: b.class.to_s.sub("ActionBlocks::",""),
      key: b.key,
      fields: b.errors.keys,
      messages: b.errors.map {|k,v| [k,v]}
    } if b.invalid?
  end
  results
end

#find(block_key) ⇒ Object



119
120
121
# File 'lib/action_blocks/store.rb', line 119

def find(block_key)
  @block_store[block_key]
end

#form(*p, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/action_blocks/store.rb', line 23

def form(*p, &block)
  rs = ActionBlocks::FormBuilder.new()
  rs.id = p.first
  rs.before_build(nil, *p)
  store(rs)
  add_to_validator(rs)
  rs.evaluate(&block) if block
  rs.after_build(nil, *p)
end

#freeze_buildersObject



137
138
139
140
141
142
# File 'lib/action_blocks/store.rb', line 137

def freeze_builders
  @block_store.values.each do |v|
    v.freeze
    puts "#Freezing #{v.key}"
  end
end

#get(params) ⇒ Object



123
124
125
# File 'lib/action_blocks/store.rb', line 123

def get(params)
  @block_store[params[:block_key]].get(params)
end

#has_error(substring) ⇒ Object



95
96
97
98
# File 'lib/action_blocks/store.rb', line 95

def has_error(substring)
  all_errors = errors.map {|e| e.messages}.map {|h| h.values}.flatten.join("\n")
  all_errors[substring]
end

#has_error_for(block_type, field_name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/action_blocks/store.rb', line 83

def has_error_for(block_type, field_name)
  type_errors = errors.select { |e|
    e[:builder] == block_type &&
    e[:fields].include?(field_name)
  }
  if type_errors == []
    return false
  else
    return type_errors
  end
end

#hashify(user) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/action_blocks/store.rb', line 127

def hashify(user)
  result = {}
  @block_store.each do |key, block|
    result[block.key] = block.hashify(user)
    result[block.key][:key] = block.key
  end
  result[:errors] = errors
  result
end

#invalid?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/action_blocks/store.rb', line 58

def invalid?
  !valid?
end

#keysObject



107
108
109
# File 'lib/action_blocks/store.rb', line 107

def keys()
  @block_store.keys
end

#layout(*p, &block) ⇒ Object

ActionBlocks.layout



13
14
15
16
17
18
19
20
21
# File 'lib/action_blocks/store.rb', line 13

def layout(*p, &block)
  l = ActionBlocks::LayoutBuilder.new()
  l.id = 'main'
  l.before_build(nil, *p)
  store(l)
  add_to_validator(l)
  l.evaluate(&block) if block
  l.after_build(nil, *p)
end

#model(*p, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/action_blocks/store.rb', line 44

def model(*p, &block)
  m = ActionBlocks::ModelBuilder.new()
  m.id = p.first
  m.before_build(nil, *p)
  store(m)
  add_to_validator(m)
  m.evaluate(&block) if block
  m.after_build(nil, *p)
end

#store(block) ⇒ Object



111
112
113
# File 'lib/action_blocks/store.rb', line 111

def store(block)
  @block_store[block.key] = block
end

#unload!Object



100
101
102
103
104
105
# File 'lib/action_blocks/store.rb', line 100

def unload!
  # If classes were dynamically created.
  # We would remove const here (JRH)
  @block_store = {};
  @validation_store = [];
end

#valid?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/action_blocks/store.rb', line 54

def valid?
  @validation_store.all? { |builder| builder.valid? }
end