Sha256: 25eac35416684ddc9e74bdc4424dcf493a4147bcba914d5e525f73c4332e8d09

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

class Card
  module Model
    module SaveHelper
      # private helper methods for public SaveHelper api
      module SaveHelperHelper
        CARDTYPE_METHOD_REGEXP = /^(?<method_name>create|ensure)_(?<type>.+?)(?:_card)?$/

        private

        def codename_from_name name
          name.downcase.tr(" ", "_").tr(":*", "")
        end

        def delete_code_card? name
          return false if name.is_a?(Symbol) && !Codename.exist?(name)

          Card.exist? name
        end

        def method_missing method, *args
          method_name, cardtype_card = extract_cardtype_from_method_name method
          return super unless method_name

          sargs = standardize_args(*args)
          send "#{method_name}_card", sargs.merge(type_id: cardtype_card.id)
        end

        def respond_to_missing? method, _include_private=false
          extract_cardtype_from_method_name(method) || super
        end

        def extract_cardtype_from_method_name method
          return unless (match = method.match CARDTYPE_METHOD_REGEXP)

          cardtype_card = cardtype_card_from_string match[:type]

          return unless cardtype_card&.type_id == CardtypeID || cardtype_card&.id == SetID

          [match[:method_name], cardtype_card]
        end

        def cardtype_card_from_string type
          Card::Codename[type.to_sym] ? Card[type.to_sym] : Card[type]
        end

        def add_name name, content_or_args
          if content_or_args.is_a?(Hash)
            content_or_args.reverse_merge name: name
          else
            { content: content_or_args, name: name }
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
card-1.105.6 lib/card/model/save_helper/save_helper_helper.rb
card-1.105.5 lib/card/model/save_helper/save_helper_helper.rb
card-1.105.4 lib/card/model/save_helper/save_helper_helper.rb
card-1.105.3 lib/card/model/save_helper/save_helper_helper.rb
card-1.105.2.pre1 lib/card/model/save_helper/save_helper_helper.rb
card-1.105.2 lib/card/model/save_helper/save_helper_helper.rb
card-1.105.1 lib/card/model/save_helper/save_helper_helper.rb
card-1.105.0 lib/card/model/save_helper/save_helper_helper.rb