Sha256: 386b2fb3d7c7e28368e41b5321196c7e46fac67575edfa6a6156c07687015a43

Contents?: true

Size: 1.87 KB

Versions: 9

Compression:

Stored size: 1.87 KB

Contents

class Card
  class Format
    module Nest
      # Fetch card for a nest
      module Fetch
        def fetch_nested_card cardish, opts={}
          case cardish
          when Card            then cardish
          when Symbol, Integer then Card.fetch cardish
          when "_", "_self"    then card.context_card
          else
            opts[:nest_name] = Card::Name[cardish].to_s
            Card.fetch cardish, new: nest_new_args(opts)
          end
        end

        private

        def nest_new_args nest_opts
          nest_name = nest_opts[:nest_name].to_s
          new_args = { name: nest_name, type: nest_opts[:type] }

          new_args[:supercard] = card.context_card unless nest_name.strip.blank?
          # special case.  gets absolutized incorrectly. fix in name?

          nest_new_main_args new_args if nest_name =~ /^_main\+/
          nest_new_content_args new_args, nest_name
          new_args
        end

        def nest_new_main_args new_args
          # FIXME: this is a rather hacky way to get @superleft
          # to work on new cards named _main+whatever
          new_args[:name] = new_args[:name].gsub(/^_main\+/, "+")
          new_args[:supercard] = root.card
        end

        def nest_new_content_args new_args, nest_name
          content = nest_content_from_shorthand_param(nest_name) ||
                    nest_content_from_subcard_params(nest_name)
          new_args[:content] = content if content.present?
        end

        def nest_content_from_shorthand_param nest_name
          shorthand_param = nest_name.tr "+", "_"
          # FIXME: this is a lame shorthand; could be another card's key
          # should be more robust and managed by Card::Name
          params[shorthand_param]
        end

        def nest_content_from_subcard_params nest_name
          params.dig "subcards", nest_name, "content"
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
card-1.94.1 lib/card/format/nest/fetch.rb
card-1.94.0 lib/card/format/nest/fetch.rb
card-1.93.13 lib/card/format/nest/fetch.rb
card-1.93.12 lib/card/format/nest/fetch.rb
card-1.93.11 lib/card/format/nest/fetch.rb
card-1.93.10 lib/card/format/nest/fetch.rb
card-1.93.9 lib/card/format/nest/fetch.rb
card-1.93.8 lib/card/format/nest/fetch.rb
card-1.93.7 lib/card/format/nest/fetch.rb