Class: RecursiveCaseIndifferentOstruct

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

Constant Summary collapse

DEFAULT_CASE =
:snake

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, casing = nil) ⇒ RecursiveCaseIndifferentOstruct



6
7
8
9
# File 'lib/recursive_case_indifferent_ostruct.rb', line 6

def initialize(hash={}, casing=nil)
  @default_case = casing || DEFAULT_CASE
  @hash         = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/recursive_case_indifferent_ostruct.rb', line 23

def method_missing(method_sym, *args, &block)
  method_name   = method_sym.to_s
  is_assignment = method_name.slice(-1) == '='
  is_get        = !is_assignment && args.length == 0 && !@hash.respond_to?(method_name)
  
  # remove = if an assignment
  if is_assignment
    method_name = method_name.slice(0, method_name.length - 1)
  end
  
  if is_assignment
    return handle_assignment(method_name, args.first)
  elsif is_get
    return handle_get(method_name)
  else
    @hash.send(method_name, *args, &block)
  end
  
  # if they called a function and passed some args, then
  # raise no method error
  #super
end

Instance Attribute Details

#default_caseObject

Returns the value of attribute default_case.



4
5
6
# File 'lib/recursive_case_indifferent_ostruct.rb', line 4

def default_case
  @default_case
end

Class Method Details

.clean_key(key) ⇒ Object

string for comparison



80
81
82
# File 'lib/recursive_case_indifferent_ostruct.rb', line 80

def self.clean_key(key)
  key.to_s.downcase.gsub(/[\W_]/, '')
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/recursive_case_indifferent_ostruct.rb', line 15

def [](key)
  handle_get(key)
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  handle_assignment(key, value, true)
end

#case_being_usedObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/recursive_case_indifferent_ostruct.rb', line 46

def case_being_used
  # TODO: If all nil then scan for keys in nested hashes
  cases_found = @hash.keys.map do |key|
    if !key.to_s.match(/^[a-z]+\-[a-z]/).nil? # kabab
      :kabab
    elsif key.to_s.include?('_')# snake
      :snake
    elsif !key.to_s.match(/^[a-z]+[A-Z]/).nil? # lower camel
      :lower_camel
    elsif !key.to_s.match(/^[A-Z][a-z]+[A-Z]/).nil? # upper camel
      :upper_camel
    elsif !key.to_s.match(/^[A-Z][a-z]+\-[A-Z]/).nil? # Train-Case
      :train
    else
      # Could just be one word in that case who knows what case it is
      nil
    end
  end
  
  if cases_found.compact.uniq.size == 1
    cases_found.compact.first
  else
    return @default_case
  end
  
end

#find_matching_keys(to_find) ⇒ Object



73
74
75
76
77
# File 'lib/recursive_case_indifferent_ostruct.rb', line 73

def find_matching_keys(to_find)
  @hash.keys.select do |key|
    self.class.clean_key(to_find) == self.class.clean_key(key)
  end
end

#to_hObject



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

def to_h
  @hash
end