Class: Glia::UpdateBuilder
- Inherits:
-
Object
- Object
- Glia::UpdateBuilder
- Defined in:
- lib/glia/update_builder.rb
Instance Method Summary collapse
- #action(definition) ⇒ Object
- #cell(definition, &blk) ⇒ Object
- #handle(*keys, &blk) ⇒ Object
-
#initialize(data = nil) ⇒ UpdateBuilder
constructor
A new instance of UpdateBuilder.
- #merge(handles) ⇒ Object
- #reference(definition, &blk) ⇒ Object
- #remove(definition) ⇒ Object
- #to_h ⇒ Object
- #view_namespace(namespace) ⇒ Object
Constructor Details
#initialize(data = nil) ⇒ UpdateBuilder
Returns a new instance of UpdateBuilder.
5 6 7 |
# File 'lib/glia/update_builder.rb', line 5 def initialize(data = nil) @data = data||{} end |
Instance Method Details
#action(definition) ⇒ Object
45 46 47 48 49 |
# File 'lib/glia/update_builder.rb', line 45 def action(definition) raise Glia::Errors::SyntaxError, 'Action cannot be used outside of cell' if @current_cell.nil? @current_cell[:actions] ||= [] @current_cell[:actions] << definition end |
#cell(definition, &blk) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/glia/update_builder.rb', line 28 def cell(definition, &blk) raise Glia::Errors::SyntaxError, 'cell must have a class' if definition[:class].nil? # Store the namespace here, for use when building layout. # We delay resolving the class, in case a block is not used, or is defined later. definition[:view_namespace] = @view_namespace unless @view_namespace.nil? _cell(definition, &blk) end |
#handle(*keys, &blk) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/glia/update_builder.rb', line 14 def handle(*keys, &blk) keys.each do |key| begin @current_scope = @data[key] ||= {} @current_cell = nil instance_eval &blk ensure @current_scope = nil @current_cell = nil end end self end |
#merge(handles) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/glia/update_builder.rb', line 55 def merge(handles) _data = {} # Merge all of the handles together merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 } handles.each{|h| _data = _data.merge(@data[h].clone, &merger) unless @data[h].nil?} #returned cleaned merged data _clean_handle(_data) end |
#reference(definition, &blk) ⇒ Object
40 41 42 43 |
# File 'lib/glia/update_builder.rb', line 40 def reference(definition, &blk) raise Glia::Errors::SyntaxError, 'Reference cannot have a class' unless definition[:class].nil? _cell(definition, &blk) end |
#remove(definition) ⇒ Object
36 37 38 |
# File 'lib/glia/update_builder.rb', line 36 def remove(definition) _cell(definition.merge({_removed: true})) end |
#to_h ⇒ Object
51 52 53 |
# File 'lib/glia/update_builder.rb', line 51 def to_h @data end |
#view_namespace(namespace) ⇒ Object
9 10 11 12 |
# File 'lib/glia/update_builder.rb', line 9 def view_namespace(namespace) @view_namespace = namespace self end |