Class: Attrify::OperationSet

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, Helpers
Defined in:
lib/attrify/operation_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#compute_attributes, #deep_merge_hashes, #deep_merge_hashes!, #execute_operation

Constructor Details

#initialize(operations, has_procs = false) ⇒ OperationSet

Returns a new instance of OperationSet.



14
15
16
17
# File 'lib/attrify/operation_set.rb', line 14

def initialize(operations, has_procs = false)
  @has_procs = has_procs
  @operations = operations
end

Instance Attribute Details

#has_procsObject (readonly)

Returns the value of attribute has_procs.



12
13
14
# File 'lib/attrify/operation_set.rb', line 12

def has_procs
  @has_procs
end

#operationsObject (readonly)

Returns the value of attribute operations.



11
12
13
# File 'lib/attrify/operation_set.rb', line 11

def operations
  @operations
end

Instance Method Details

#[](key) ⇒ Object

Retrieve value by key



58
59
60
# File 'lib/attrify/operation_set.rb', line 58

def [](key)
  @result[key]
end

#[]=(key, value) ⇒ Object

Set value by key



63
64
65
# File 'lib/attrify/operation_set.rb', line 63

def []=(key, value)
  @result[key] = value
end

#dig(*keys) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/attrify/operation_set.rb', line 27

def dig(*keys)
  result = @result.dig(*keys)

  if result.present?
    if result.has_key?(:attributes)
      result[:attributes]
    end
  end
end

#eachObject

Iterate like a hash



68
69
70
# File 'lib/attrify/operation_set.rb', line 68

def each(&)
  @result.each(&)
end

#keysObject

Return all keys



73
74
75
# File 'lib/attrify/operation_set.rb', line 73

def keys
  @result.keys
end

#to_hashObject



53
54
55
# File 'lib/attrify/operation_set.rb', line 53

def to_hash
  @result
end

#to_htmlObject



19
20
21
# File 'lib/attrify/operation_set.rb', line 19

def to_html
  tag.attributes(@result)
end

#to_sObject



82
83
84
# File 'lib/attrify/operation_set.rb', line 82

def to_s
  to_html
end

#to_tagsObject



23
24
25
# File 'lib/attrify/operation_set.rb', line 23

def to_tags
  to_hash
end

#valuesObject

Return all values



78
79
80
# File 'lib/attrify/operation_set.rb', line 78

def values
  @result
end

#values_for(instance:, keys:) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/attrify/operation_set.rb', line 37

def values_for(instance:, keys:)
  # if @has_procs
  @operations = run_procs_on(@operations, instance)
  @result = cache_result(@operations)[:value]
  @result = @result.dig(*keys) || {}
  if @result.present?
    if @result.has_key?(:attributes)
      @result = @result[:attributes]
    end
  end
  self
  # else
  #  self
  # end
end