Class: BlockKit::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations, ActiveModel::Validations::Callbacks
Defined in:
lib/block_kit/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) {|_self| ... } ⇒ Base

Returns a new instance of Base.

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (NotImplementedError)


36
37
38
39
40
41
42
# File 'lib/block_kit/base.rb', line 36

def initialize(attributes = {})
  raise NotImplementedError, "#{self.class} is an abstract class and can't be instantiated." if instance_of?(Base)

  super

  yield(self) if block_given?
end

Class Method Details

.fix(method_name, dangerous: false) ⇒ Object



28
29
30
# File 'lib/block_kit/base.rb', line 28

def self.fix(method_name, dangerous: false)
  attribute_fixers[:base] << {name: method_name, dangerous: dangerous}
end

.fixes(attribute, fixers) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/block_kit/base.rb', line 18

def self.fixes(attribute, fixers)
  fixers.each do |name, options|
    options = {} if !options.is_a?(Hash)
    fixer_class = BlockKit::Fixers.const_get(name.to_s.camelize)
    fixer = fixer_class.new(attribute: attribute, **options)

    attribute_fixers[attribute] << fixer
  end
end

.inherited(subclass) ⇒ Object



32
33
34
# File 'lib/block_kit/base.rb', line 32

def self.inherited(subclass)
  subclass.attribute_fixers = attribute_fixers.deep_dup
end

.inspectObject



82
83
84
# File 'lib/block_kit/base.rb', line 82

def self.inspect
  "#{name}(#{attribute_types.map { |k, v| "#{k}: #{v.type}" }.join(", ")})"
end

Instance Method Details

#==(other) ⇒ Object



78
79
80
# File 'lib/block_kit/base.rb', line 78

def ==(other)
  other.instance_of?(self.class) && attributes == other.attributes
end

#as_jsonObject



68
69
70
71
72
# File 'lib/block_kit/base.rb', line 68

def as_json(*)
  fix_validation_errors if BlockKit.config.autofix_on_render

  {type: self.class.type.to_s}
end

#fix_validation_errors(dangerous: BlockKit.config.autofix_dangerously) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/block_kit/base.rb', line 44

def fix_validation_errors(dangerous: BlockKit.config.autofix_dangerously)
  fixing do
    return true if valid?

    Array(attribute_fixers[:base]).each do |method|
      method(method[:name]).call unless method[:dangerous] && !dangerous
    end

    attribute_fixers.except(:base).each do |attribute, fixers|
      fixers.each { |fixer| fixer.fix(self, fixing_dangerously: dangerous) }
    end

    validate
  end
end

#fix_validation_errors!(dangerous: BlockKit.config.autofix_dangerously) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/block_kit/base.rb', line 60

def fix_validation_errors!(dangerous: BlockKit.config.autofix_dangerously)
  fixing do
    fix_validation_errors(dangerous: dangerous)

    validate!
  end
end

#inspectObject



86
87
88
# File 'lib/block_kit/base.rb', line 86

def inspect
  "#<#{self.class} #{attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(", ")}>"
end

#pretty_print(pp) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/block_kit/base.rb', line 90

def pretty_print(pp)
  pp.object_group(self) do
    pp.breakable
    pp.seplist(attributes) do |k, v|
      pp.text "#{k}: "
      pp.pp v
    end
  end
end

#to_jsonObject



74
75
76
# File 'lib/block_kit/base.rb', line 74

def to_json(*)
  as_json.to_json
end