Class: Memorium::RequireAttr

Inherits:
Object
  • Object
show all
Defined in:
lib/memorium/require_attr.rb

Defined Under Namespace

Classes: Template

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_name, by:, value:) ⇒ RequireAttr

Returns a new instance of RequireAttr.



7
8
9
10
11
# File 'lib/memorium/require_attr.rb', line 7

def initialize(attr_name, by:, value:)
  @attr_name = attr_name.to_s
  @by = by.is_a?(Proc) ? by : by.to_s
  @value = value
end

Instance Attribute Details

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



2
3
4
# File 'lib/memorium/require_attr.rb', line 2

def attr_name
  @attr_name
end

#byObject (readonly)

Returns the value of attribute by.



2
3
4
# File 'lib/memorium/require_attr.rb', line 2

def by
  @by
end

#valueObject (readonly)

Returns the value of attribute value.



2
3
4
# File 'lib/memorium/require_attr.rb', line 2

def value
  @value
end

Class Method Details

.caller_locationObject



5
# File 'lib/memorium/require_attr.rb', line 5

def self.caller_location = caller_locations(4, 1).first

.create_method(klass) ⇒ Object



4
# File 'lib/memorium/require_attr.rb', line 4

def self.create_method(klass) = klass.class_eval Template.new.require_method, caller_location.path, caller_location.lineno + 1

Instance Method Details

#check_requirementsObject



13
14
15
16
17
# File 'lib/memorium/require_attr.rb', line 13

def check_requirements
  return by.call(value) if by.is_a?(Proc)

  inverse_type? ? inverse_check_by_type : check_by_type
end

#raise_textObject



19
20
21
22
23
24
25
# File 'lib/memorium/require_attr.rb', line 19

def raise_text
  @raise_text ||= begin
    return  "Attribute `#{attr_name}` not pass the condition: #{value.inspect}" if by.is_a?(Proc)

    "Attribute `#{attr_name}` #{raise_verb} be #{unpredicated_type}: #{value.inspect}"
  end
end