Class: Attrify::OperationSet
- Inherits:
-
Object
- Object
- Attrify::OperationSet
- Includes:
- ActionView::Helpers::TagHelper, Helpers
- Defined in:
- lib/attrify/operation_set.rb
Instance Attribute Summary collapse
-
#has_procs ⇒ Object
readonly
Returns the value of attribute has_procs.
-
#operations ⇒ Object
readonly
Returns the value of attribute operations.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Retrieve value by key.
-
#[]=(key, value) ⇒ Object
Set value by key.
- #dig(*keys) ⇒ Object
-
#each ⇒ Object
Iterate like a hash.
-
#initialize(operations, has_procs = false) ⇒ OperationSet
constructor
A new instance of OperationSet.
-
#keys ⇒ Object
Return all keys.
- #to_hash ⇒ Object
- #to_html ⇒ Object
- #to_s ⇒ Object
- #to_tags ⇒ Object
-
#values ⇒ Object
Return all values.
- #values_for(instance:, keys:) ⇒ Object
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_procs ⇒ Object (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 |
#operations ⇒ Object (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 |
#each ⇒ Object
Iterate like a hash
68 69 70 |
# File 'lib/attrify/operation_set.rb', line 68 def each(&) @result.each(&) end |
#keys ⇒ Object
Return all keys
73 74 75 |
# File 'lib/attrify/operation_set.rb', line 73 def keys @result.keys end |
#to_hash ⇒ Object
53 54 55 |
# File 'lib/attrify/operation_set.rb', line 53 def to_hash @result end |
#to_html ⇒ Object
19 20 21 |
# File 'lib/attrify/operation_set.rb', line 19 def to_html tag.attributes(@result) end |
#to_s ⇒ Object
82 83 84 |
# File 'lib/attrify/operation_set.rb', line 82 def to_s to_html end |
#to_tags ⇒ Object
23 24 25 |
# File 'lib/attrify/operation_set.rb', line 23 def to_hash end |
#values ⇒ Object
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 |