Class: BetterStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/better_struct.rb,
lib/better_struct/version.rb,
lib/better_struct/methodize.rb

Constant Summary collapse

EQUAL_SIGN =
"=".freeze
MAP_METHOD_NAMES =
%i(map map!).to_set.freeze
VERSION =
"0.2.2".freeze
EMPTY_STRING =
"".freeze
UNDERSCORE_SIGN =
"_".freeze
TRANSLITERATION_FROM =
"ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž".freeze
TRANSLITERATION_TO =
"AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz".freeze
UPCASE_REGEXP =
/[A-Z]/.freeze
NOT_UNDERSCORED_REGEXP =
/[^a-z0-9_]/.freeze
NON_ENGLISH_REGEXP =
/[#{ TRANSLITERATION_FROM }]/.freeze
UNDERSCORE_DUPLICATES_REGEXP =
/#{ UNDERSCORE_SIGN }{2,}/.freeze
UNDERSCORE_BEGIN_OR_END_REGEXP =
/^#{ UNDERSCORE_SIGN }|#{ UNDERSCORE_SIGN }$/.freeze
CAMELCASE_ABBREVIATION_REGEX =
/([A-Z\d]+)([A-Z][a-z])/.freeze
CAMELCASE_REGEX =
/([a-z\d])([A-Z])/.freeze
UNDERSCORE_MASK =
'\1_\2'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ BetterStruct

Returns a new instance of BetterStruct.



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

def initialize(value = nil)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



46
47
48
49
50
51
52
53
54
# File 'lib/better_struct.rb', line 46

def method_missing(method_name, *args, &block)
  if value.respond_to?(method_name)
    delegate_method(method_name, *args, &block)
  elsif assignment?(method_name) && defined_methods
    @defined_methods[methodize(method_name[0...-1])] = args.first
  else
    wrap(defined_methods[method_name.to_s])
  end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/better_struct.rb', line 9

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  other.is_a?(self.class) && value == other.value
end

#empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/better_struct.rb', line 27

def empty?
  value.respond_to?(:empty?) ? !!value.empty? : !value
end

#inspectObject



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

def inspect
  "#{ self.class }<#{ value.inspect }>"
end

#to_sObject



23
24
25
# File 'lib/better_struct.rb', line 23

def to_s
  value.to_s
end