Class: Susanin::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/susanin/resource.rb

Instance Method Summary collapse

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

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
# File 'lib/susanin/resource.rb', line 150

def contains_subarray?(source, subarray)
  source = Array.wrap(source)
  subarray = Array.wrap(subarray)
  iteration_count = source.length - subarray.length
  0.upto(iteration_count).any? do |i|
    source[i..(i+subarray.length-1)] == subarray
  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

get_key(a) => A get_key(A) => A get_key() => [A] get_key([a, B]) => [A, B] get_key() => [A] get_key() => [A] get_key(:qwe) => :qwe get_key() => [:qwe] get_key(‘qwe’) => ‘qwe’



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 merged_options(params, options={})
  params = params.dup
  default_options = params.extract_options!
  params + ((default_options.any? || options.any?) ? [default_options.merge(options)] : [])
end

#pattern_match?(record, pattern) ⇒ Boolean

pattern_match?([a, b], [A, B]) => true pattern_match?([a, b], [a, b]) => false

Returns:

  • (Boolean)


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| contains_subarray?(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, options={})
  params = self.get(Array.wrap(record_or_hash_or_array)).flatten
  opts, params = params.partition { |i| i.is_a?(Hash) }
  merged_options(params, opts.inject(options) { |acc, i| acc.merge(i) })
end