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
207 208 209 |
# File 'lib/susanin/resource.rb', line 207 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
150 151 152 153 154 155 156 157 |
# File 'lib/susanin/resource.rb', line 150 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
78 79 80 81 82 83 84 |
# File 'lib/susanin/resource.rb', line 78 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
31 32 33 34 35 36 37 38 |
# File 'lib/susanin/resource.rb', line 31 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
120 121 122 123 124 125 126 127 128 |
# File 'lib/susanin/resource.rb', line 120 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]
138 139 140 141 142 |
# File 'lib/susanin/resource.rb', line 138 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
201 202 203 204 205 |
# File 'lib/susanin/resource.rb', line 201 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
159 160 161 |
# File 'lib/susanin/resource.rb', line 159 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
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/susanin/resource.rb', line 172 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 opts, r = record.partition { |i| i.is_a?(Hash) } opts.any? ? [*r, opts.inject({}, :merge)] : r 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}]
]
]
58 59 60 61 62 63 64 |
# File 'lib/susanin/resource.rb', line 58 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}]
],
102 103 104 105 106 107 |
# File 'lib/susanin/resource.rb', line 102 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 16 |
# 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 opts, params = params.partition { |i| i.is_a?(Hash) } (params, opts.inject() { |acc, i| acc.merge(i) }) end |