Skip to content

Commit

Permalink
Merge pull request #453 from decko-commons/chunks
Browse files Browse the repository at this point in the history
chunk api touch up
  • Loading branch information
ethn authored Mar 16, 2020
2 parents 96f4ac3 + c5c28f6 commit a829ff3
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 86 deletions.
2 changes: 1 addition & 1 deletion card/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.4
0.9.5
4 changes: 0 additions & 4 deletions card/lib/card/view/cache/cache_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ def cache_setting
def clean_enough_to_cache?
# requested_view == ok_view && !card.unknown? && !card.db_content_changed?
requested_view == ok_view && !card.db_content_changed?
# unknown views may be safe if we add type_id to the cache key for unknown
# (or all new?) cards.
#
#
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions card/mod/basic_formats/set/all/all_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def default_item_view
end

def name_with_fields_row
nested_fields.each_with_object([card.name]) do |(field_name, _options), row|
nested_field_names.each_with_object([card.name]) do |field_name, row|
row << nest(field_name)
end
end
Expand All @@ -53,11 +53,7 @@ def row_card_names
end

def columns
columns = []
csv_structure_card.format.each_nested_field do |chunk|
columns << chunk.referee_name.tag
end
columns
csv_structure_card.format.nested_field_names.map(&:tag)
end

def csv_structure_card
Expand Down
2 changes: 1 addition & 1 deletion card/mod/basic_formats/set/all/json.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
format :json do
# because card.item_cards returns "[[#{self}]]"
def item_cards
uniq_nested_cards
nested_cards
end

def default_nest_view
Expand Down
139 changes: 70 additions & 69 deletions card/mod/core/set/all/chunk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,109 +33,110 @@ def nest_chunks content=nil
card.nest_chunks content
end

def nested_cards content=nil
nest_chunks(content).map(&:referee_card).uniq
end

def edit_fields
voo.edit_structure || []
end

def nested_fields
result = []
each_nested_card(nil, true) do |chunk|
result << [chunk.referee_name, chunk.options]
end
result
def nested_field_names content=nil
nest_chunks(content).map(&:referee_name).select { |n| field_name? n }
end

def nested_cards_for_edit fields_only=false
return normalized_edit_fields if edit_fields.present?
result = []
each_nested_card nil, fields_only do |chunk|
result << [chunk.options[:nest_name], chunk.options]
end
result
def nested_field_cards content=nil
nested_cards(content).select { |c| field_name? c.name }
end

def normalized_edit_fields
edit_fields.map do |cardish, options|
field_mark = normalized_edit_field_mark cardish, options
options = normalized_edit_field_options options, Card::Name[field_mark]
[field_mark, options]
end
def field_name? name
name.field_of? card.name
end

def normalized_edit_field_options options, cardname
options ||= cardname
options.is_a?(String) ? { title: options } : options
# @return [Array] of Arrays. each is [nest_name, nest_options_hash]
def edit_field_configs fields_only=false
if edit_fields.present?
explicit_edit_fields_config # explicitly configured in voo or code
else
implicit_edit_fields_config fields_only # inferred from nests
end
end

def normalized_edit_field_mark cardish, options
return cardish if cardish.is_a?(Card) ||
(options.is_a?(Hash) && options.delete(:absolute))
card.name.field cardish
def implicit_edit_fields_config fields_only
result = []
each_nested_chunk(fields: fields_only) do |chunk|
result << [chunk.options[:nest_name], chunk.options]
end
result
end

def process_field chunk, processed
return unless process_unique_field? chunk, processed
yield chunk if block_given?
def each_nested_field_chunk &block
each_nested_chunk fields: true, &block
end

def each_nested_field &block
each_nested_card nil, true, &block
def each_nested_chunk content: nil, fields: false, uniq: true, virtual: true, &block
return unless block_given?
chunks = prepare_nested_chunks content, fields, uniq
process_nested_chunks chunks, virtual, &block
end

def each_nested_card content=nil, fields_only=false, &block
processed = process_tally
nest_chunks(content).each do |chunk|
next if fields_only && !field_chunk?(chunk)
process_nested_chunk chunk, processed, &block
def uniq_chunks chunks
processed = ::Set.new [card.key]
chunks.select do |chunk|
key = chunk.referee_name.key
ok = !processed.include?(key)
processed << key
ok
end
end

def uniq_nested_cards content: nil
with_unique_chunks do
nest_chunks(content).map do |chunk|
chunk.referee_card if unique_chunk? chunk
end
end
def field_chunks chunks
chunks.select { |chunk| field_name?(chunk.referee_name) }
end

def process_tally
::Set.new [card.key]
end
private

def field_chunk? chunk
chunk.referee_name.to_name.field_of? card.name
def prepare_nested_chunks content, fields, uniq
chunks = nest_chunks content
chunks = field_chunks chunks if fields
chunks = uniq_chunks chunks if uniq
chunks
end

def unique_chunk? chunk
key = chunk.referee_name.key
return false if @processed.include? key
@processed << key
true
def process_nested_chunks chunks, virtual, &block
chunks.each do |chunk|
process_nested_chunk chunk, virtual, &block
end
end

def with_unique_chunks
@processed = ::Set.new [card.key]
yield
def process_nested_chunk chunk, virtual, &block
if chunk.referee_card&.virtual?
process_nested_virtual_chunk chunk, &block unless virtual
else
yield chunk
end
end

def process_nested_chunk chunk, processed, &block
virtual = chunk.referee_card&.virtual?
# TODO: handle structures that are non-virtual
method = virtual ? :process_virtual_field : :process_field
send method, chunk, processed, &block
def process_virtual_chunk chunk
subformat(chunk.referee_card).each_nested_field_chunk { |sub_chunk| yield sub_chunk }
end

def process_virtual_field chunk, processed, &block
return unless process_unique_field? chunk, processed
subformat(chunk.referee_card).each_nested_field do |sub_chunk|
process_field sub_chunk, processed, &block
def explicit_edit_fields_config
edit_fields.map do |cardish, options|
field_mark = normalized_edit_field_mark cardish, options
options = normalized_edit_field_options options, Card::Name[field_mark]
[field_mark, options]
end
end

def process_unique_field? chunk, processed
key = chunk.referee_name.key
return false if processed.include? key
processed << key
true
def normalized_edit_field_options options, cardname
options ||= cardname
options.is_a?(String) ? { title: options } : options
end

def normalized_edit_field_mark cardish, options
return cardish if cardish.is_a?(Card) ||
(options.is_a?(Hash) && options.delete(:absolute))
card.name.field cardish
end
end
1 change: 1 addition & 0 deletions card/mod/core/spec/set/all/chunk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def format_with_edit_fields args

example "absolute option", as_bot: true do
format = format_with_edit_fields [[:self, { absolute: true }]]

expect(format.render_edit).to have_tag ".card-editor", with: { card_name: "*self" }
end

Expand Down
6 changes: 3 additions & 3 deletions card/mod/edit/set/all/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ def editor_in_multi_card
end

def multi_card_edit fields_only=false
nested_cards = nested_cards_for_edit(fields_only)
return structure_link if nested_cards.empty?
field_configs = edit_field_configs fields_only
return structure_link if field_configs.empty?

nested_cards.map do |name, options|
field_configs.map do |name, options|
nest name, options || {}
end.join "\n"
end
Expand Down
2 changes: 1 addition & 1 deletion card/mod/standard/set/all/rich_html/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def short_content_items

def short_content_fields
return unless voo.structure || card.structure
fields = nested_fields.size
fields = nested_field_names.size
return if fields.zero?
"#{fields} #{'field'.pluralize fields}"
end
Expand Down
2 changes: 1 addition & 1 deletion decko/lib/decko/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true
config.cache_classes = false

config.assets.enabled = true if Object.const_defined?(:JasmineRails)

Expand Down

0 comments on commit a829ff3

Please sign in to comment.