Class: Susanin::Resource
- Inherits:
-
Object
- Object
- Susanin::Resource
- Defined in:
- lib/susanin/resource.rb
Instance Method Summary collapse
- #array_unwrap(a) ⇒ Object
-
#contains_subarray?(source, subarray) ⇒ Boolean
contains_subarray?(, [1,2,3]) => true contains_subarray?(, [3,4]) => true contains_subarray?(, [1,3,4]) => false contains_subarray?(, 5) => true.
-
#find_first_pattern(record, resources) ⇒ Object
find_first_pattern( [:a, :b, :c, :d], [ [[:A, :B], ->® :a] [[:C], ->® :w] [[:E], ->® :e] ] ).
-
#get(record, resources = @resources) ⇒ Object
get( [:a, :c, :d], [ [[:A, :B], ->® :a] [[:A], ->® :q] [[:C], ->® :w] [[:E], ->® :e] ] ).
- #get_key(record) ⇒ Object
-
#initialize(values = []) ⇒ Resource
constructor
A new instance of Resource.
-
#merged_options(params, options = {}) ⇒ Object
merged_options([], {}) #=> [] merged_options(, {}) #=> [a] merged_options([a, {}], {}) #=> [a] merged_options([a, 1], {}) #=> [a, 1] merged_options([a, {}], 1) #=> [a, 1] merged_options([a, 1], 2) #=> [a, 1].
-
#pattern_match?(record, pattern) ⇒ Boolean
pattern_match?([a, b], [A, B]) => true pattern_match?([a, b], [a, b]) => false.
- #patterns(arr) ⇒ Object
-
#replace_subrecord(record, pattern, resource) ⇒ Object
replace_subrecord( [:a, :b, :c, :a, :b, :b], [:a, :b], ->(){ ‘1’ } ).
-
#replace_with(record, resources) ⇒ Object
replace_with( [:a, :b, :c, :d], [ [[:A, :B], ->® :a] [[:C], ->® :w] [[:E], ->® :e] ] ).
-
#resources_except(resources, keys) ⇒ Object
resources_except( [ [[:A, :B], ->® :a] [[:A], ->® :q] [[:C], ->® :w] [[:E], ->® :e] ], [:A, :B] ).
- #url_parameters(record_or_hash_or_array, options = {}) ⇒ Object
Constructor Details
#initialize(values = []) ⇒ Resource
Returns a new instance of Resource.
8 9 10 |
# File 'lib/susanin/resource.rb', line 8 def initialize(values=[]) @resources = values.dup end |
Instance Method Details
#array_unwrap(a) ⇒ Object
205 206 207 |
# File 'lib/susanin/resource.rb', line 205 def array_unwrap(a) a.size == 1 ? a[0] : a end |
#contains_subarray?(source, subarray) ⇒ Boolean
contains_subarray?(, [1,2,3]) => true contains_subarray?(, [3,4]) => true contains_subarray?(, [1,3,4]) => false contains_subarray?(, 5) => true
149 150 151 152 153 154 155 156 |
# File 'lib/susanin/resource.rb', line 149 def (source, ) source = Array.wrap(source) = Array.wrap() iteration_count = source.length - .length 0.upto(iteration_count).any? do |i| source[i..(i+.length-1)] == end end |
#find_first_pattern(record, resources) ⇒ Object
find_first_pattern(
[:a, :b, :c, :d],
[
[[:A, :B], ->(r) {:a}]
[[:C], ->(r) {:w}]
[[:E], ->(r) {:e}]
]
)
- [:A, :B], ->® :a
77 78 79 80 81 82 83 |
# File 'lib/susanin/resource.rb', line 77 def find_first_pattern(record, resources) record_patterns = patterns(get_key(record)) resources.select do |r| record_patterns.include?(r[0]) end.first || [] end |
#get(record, resources = @resources) ⇒ Object
get(
[:a, :c, :d],
[
[[:A, :B], ->(r) {:a}]
[[:A], ->(r) {:q}]
[[:C], ->(r) {:w}]
[[:E], ->(r) {:e}]
]
)
- :qwe, :wer, :d
30 31 32 33 34 35 36 37 |
# File 'lib/susanin/resource.rb', line 30 def get(record, resources=@resources) new_record, new_resources = replace_with(Array.wrap(record), resources) if record == new_record new_record else get(new_record, new_resources) end end |
#get_key(record) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/susanin/resource.rb', line 119 def get_key(record) case record when Class then record when Array then record.map { |i| get_key(i) } when Symbol then record when String then record else record.class end end |
#merged_options(params, options = {}) ⇒ Object
merged_options([], {}) #=> [] merged_options(, {}) #=> [a] merged_options([a, {}], {}) #=> [a] merged_options([a, 1], {}) #=> [a, 1] merged_options([a, {}], 1) #=> [a, 1] merged_options([a, 1], 2) #=> [a, 1]
137 138 139 140 141 |
# File 'lib/susanin/resource.rb', line 137 def (params, ={}) params = params.dup = params. params + ((.any? || .any?) ? [.merge()] : []) end |
#pattern_match?(record, pattern) ⇒ Boolean
pattern_match?([a, b], [A, B]) => true pattern_match?([a, b], [a, b]) => false
199 200 201 202 203 |
# File 'lib/susanin/resource.rb', line 199 def pattern_match?(record, pattern) Array.wrap(get_key(record)).zip(Array.wrap(pattern)).all? do |a, b| b.is_a?(Class) && a.is_a?(Class) ? a <= b : b == a end end |
#patterns(arr) ⇒ Object
158 159 160 |
# File 'lib/susanin/resource.rb', line 158 def patterns(arr) Pattern.new(arr) end |
#replace_subrecord(record, pattern, resource) ⇒ Object
replace_subrecord(
[:a, :b, :c, :a, :b, :b],
[:a, :b],
->(){ '_1_' }
)
- ‘1’, :c, ‘1’, :b
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/susanin/resource.rb', line 171 def replace_subrecord(record, pattern, resource) record = record.dup pattern = Array.wrap(pattern) i = pattern.length return record if i == 0 while i <= record.length do set = (0+(i-pattern.length))..(i-1) subset = record[set] if pattern_match?(subset, pattern) value = resource.call(*subset) record[set] = value i += Array.wrap(value).size else i += 1 end end record end |
#replace_with(record, resources) ⇒ Object
replace_with(
[:a, :b, :c, :d],
[
[[:A, :B], ->(r) {:a}]
[[:C], ->(r) {:w}]
[[:E], ->(r) {:e}]
]
)
[
[:a, :c, :d],
[
[[:C], ->(r) {:w}]
[[:E], ->(r) {:e}]
]
]
57 58 59 60 61 62 63 |
# File 'lib/susanin/resource.rb', line 57 def replace_with(record, resources) record = record.dup resources = resources.dup pattern, converter = find_first_pattern(record, resources) [replace_subrecord(record, pattern, converter), resources_except(resources, pattern)] end |
#resources_except(resources, keys) ⇒ Object
resources_except(
[
[[:A, :B], ->(r) {:a}]
[[:A], ->(r) {:q}]
[[:C], ->(r) {:w}]
[[:E], ->(r) {:e}]
],
[:A, :B]
)
[
[[:C], ->(r) {:w}]
[[:E], ->(r) {:e}]
],
101 102 103 104 105 106 |
# File 'lib/susanin/resource.rb', line 101 def resources_except(resources, keys) keys = Array.wrap(keys) new_resources = resources.dup new_resources.reject! { |r| (keys, Array.wrap(r[0])) } new_resources end |
#url_parameters(record_or_hash_or_array, options = {}) ⇒ Object
12 13 14 15 |
# File 'lib/susanin/resource.rb', line 12 def url_parameters(record_or_hash_or_array, ={}) params = self.get(Array.wrap(record_or_hash_or_array)).flatten (params, ={}) end |