Class: Bundix::Nixer
- Inherits:
-
Object
- Object
- Bundix::Nixer
- Defined in:
- lib/bundix/nixer.rb
Constant Summary collapse
- SET_T =
ERB.new(File.read(File.("../../template/nixer/set.erb", __dir__)).chomp)
- LIST_T =
ERB.new(File.read(File.("../../template/nixer/list.erb", __dir__)).chomp)
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#obj ⇒ Object
readonly
Returns the value of attribute obj.
Class Method Summary collapse
Instance Method Summary collapse
- #indent ⇒ Object
-
#initialize(obj, level = 0) ⇒ Nixer
constructor
A new instance of Nixer.
- #outdent ⇒ Object
- #serialize ⇒ Object
- #serialize_key(k) ⇒ Object
- #sub(obj, indent = 0) ⇒ Object
Constructor Details
#initialize(obj, level = 0) ⇒ Nixer
Returns a new instance of Nixer.
55 56 57 58 |
# File 'lib/bundix/nixer.rb', line 55 def initialize(obj, level = 0) @obj = obj @level = level end |
Instance Attribute Details
#level ⇒ Object (readonly)
Returns the value of attribute level.
50 51 52 |
# File 'lib/bundix/nixer.rb', line 50 def level @level end |
#obj ⇒ Object (readonly)
Returns the value of attribute obj.
50 51 52 |
# File 'lib/bundix/nixer.rb', line 50 def obj @obj end |
Class Method Details
.class_order(left, right) ⇒ Object
45 46 47 |
# File 'lib/bundix/nixer.rb', line 45 def class_order(left, right) left.class.name <=> right.class.name # like Erlang end |
.order(left, right) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bundix/nixer.rb', line 23 def order(left, right) if right.is_a?(left.class) if right.respond_to?(:<=>) cmp = right <=> left return -1 * (cmp) unless cmp.nil? end end if left.is_a?(right.class) if left.respond_to?(:<=>) cmp = right <=> left if cmp.nil? return class_order(left, right) else return cmp end end end return class_order(left, right) end |
.serialize(obj) ⇒ Object
19 20 21 |
# File 'lib/bundix/nixer.rb', line 19 def serialize(obj) new(obj).serialize end |
Instance Method Details
#indent ⇒ Object
60 61 62 |
# File 'lib/bundix/nixer.rb', line 60 def indent ' ' * (level + 2) end |
#outdent ⇒ Object
64 65 66 |
# File 'lib/bundix/nixer.rb', line 64 def outdent ' ' * level end |
#serialize ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/bundix/nixer.rb', line 80 def serialize case obj when Hash return SET_T.result(binding) when Array return LIST_T.result(binding) when String obj.dump when Symbol obj.to_s.dump when Pathname str = obj.to_s if %r{/} !~ str "./"+ str else str end when true, false obj.to_s else fail "Cannot convert to nix: #{obj.inspect}" end end |
#serialize_key(k) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/bundix/nixer.rb', line 72 def serialize_key(k) if k.to_s =~ /^[a-zA-Z_-]+[a-zA-Z0-9_-]*$/ k.to_s else sub(k, 2) end end |
#sub(obj, indent = 0) ⇒ Object
68 69 70 |
# File 'lib/bundix/nixer.rb', line 68 def sub(obj, indent = 0) self.class.new(obj, level + indent).serialize end |