Sha256: b307a806ca89e97d6d0d479dd5a91b5e40e02a7a63bbfa5a12c4aa76695706c7

Contents?: true

Size: 671 Bytes

Versions: 8

Compression:

Stored size: 671 Bytes

Contents

module DoesFlag
  as_trait do |field, options = {}|

    default = options[:default]
    virtual = options[:virtual]
    field_var = "@#{field}"
    set_field = "#{field}="
    field_query = "#{field}?"

    validates field.to_sym, inclusion: { in: [true, false] }, allow_nil: !!virtual

    unless default.nil?
      has_defaults field.to_sym => default
    end

    if virtual

      attr_reader field

      define_method field_query do
        send(field)
      end

      define_method set_field do |value|
        value = ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
        instance_variable_set(field_var, value)
      end

    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
katapult-0.5.0 lib/generators/katapult/model/templates/app/models/shared/does_flag.rb
katapult-0.4.1 lib/generators/katapult/model/templates/app/models/shared/does_flag.rb
katapult-0.4.0 lib/generators/katapult/model/templates/app/models/shared/does_flag.rb
katapult-0.3.0 lib/generators/katapult/model/templates/app/models/shared/does_flag.rb
katapult-0.2.0 lib/generators/katapult/model/templates/app/models/shared/does_flag.rb
katapult-0.1.2 lib/generators/katapult/model/templates/app/models/shared/does_flag.rb
katapult-0.1.1 lib/generators/katapult/model/templates/app/models/shared/does_flag.rb
katapult-0.1.0 lib/generators/katapult/model/templates/app/models/shared/does_flag.rb