giri 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce0c580bbf94b64b4aabe9b470cc631ea1ec0ce01bac6fe169b594022415a693
4
- data.tar.gz: e78841c26884ad0d47df63e938fb04058123ee39b4aeeadc353cc2f4aa5a05a1
3
+ metadata.gz: 7b4941af0601ffb00e81380ca1b6dfe9311f14ad878ab8581e0901e447c47178
4
+ data.tar.gz: 17effae76955d8b07a554b8e11f17126ee908c8e48b0ca264801150b68f68def
5
5
  SHA512:
6
- metadata.gz: 41e49781948e8d73d0c3b46a0a0bb645d5d23af55da76dcd4394b3b9202f6cc21ae044bbb6bbfa11be678027912acc347f3908f0d8a4df5ae4da36bfdaa0f1e8
7
- data.tar.gz: 4d43d8b9db7be0184061ded67a1c2a417309af2da06baee4a2c436a729908c1117ee7a002e4858ab39ae97107ec4b7c9636c4e20b8dc808f227238fc96ceb35a
6
+ metadata.gz: ef4328f59ca11c09302b8275fef6b32338b3043d0306df0a1f6b427fe28b19fde3fb80a3b35d488db6439d941063674762b5a10363b00edf73564d7e63f8cfb7
7
+ data.tar.gz: 14a27203b15bbdbc4bf043abf6e42960b0d07ab091d42bddf3f374dfdcaaf521bfc4813ef474615832f41bffb24f5e23133adf61364d8d9232216d3ef9ec0811
data/lib/giri/bud.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  module Giri::Bud
2
2
 
3
3
  def self.extended(base)
4
- base.singleton_class.attr_accessor :xml_nodes, :xml_attributes, :with_name_default_for_node, :with_name_default_for_attribute
4
+ base.singleton_class.attr_accessor :xml_nodes, :xml_attributes, :context_attributes, :with_name_default_for_node, :with_name_default_for_attribute
5
5
 
6
6
  base.xml_nodes = {}
7
7
  base.xml_attributes = []
8
+ base.context_attributes = []
8
9
  # TODO: Set nil for default
9
10
  base.with_name_default_for_node = :camelize
10
11
  base.with_name_default_for_attribute = :lower_camelcase
@@ -38,6 +39,10 @@ module Giri::Bud
38
39
  block.call(node)
39
40
  end
40
41
 
42
+ def context
43
+ @context ||= Giri::Context.new(self)
44
+ end
45
+
41
46
  def build_attributes
42
47
  self.class.xml_attributes.map do |col|
43
48
  name = col[:name]
@@ -68,6 +73,7 @@ module Giri::Bud
68
73
  def inherited(base)
69
74
  base.xml_nodes = xml_nodes.dup
70
75
  base.xml_attributes = xml_attributes.dup
76
+ base.context_attributes = context_attributes.dup
71
77
  base.with_name_default_for_node = with_name_default_for_node
72
78
  base.with_name_default_for_attribute = with_name_default_for_attribute
73
79
  super
@@ -182,4 +188,12 @@ module Giri::Bud
182
188
  nil
183
189
  end
184
190
  end
191
+
192
+ def context(context_name, &block)
193
+ self.context_attributes << context_name.to_sym
194
+
195
+ class_eval do
196
+ define_method(context_name, &block)
197
+ end
198
+ end
185
199
  end
@@ -0,0 +1,18 @@
1
+ class Giri::Context
2
+ def initialize(base_node)
3
+ @base_node = base_node
4
+ @context_attributes = base_node.class.context_attributes
5
+ end
6
+
7
+ def method_missing(method, *args, &block)
8
+ if @context_attributes.include?(method.to_sym)
9
+ @base_node.public_send(method, *args, &block)
10
+ else
11
+ if @base_node.parent
12
+ @base_node.parent.context.public_send(method, *args, &block)
13
+ else
14
+ super
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ # https://www.w3.org/TR/xmlschema-2/#duration
2
+ class Giri::Duration < DelegateClass(String)
3
+ def components
4
+ @components ||= begin
5
+ _, years, months, days, time_part, hours, minutes, seconds = self.match(/\AP(\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?\z/).to_a
6
+ # Example of values "15H" and "15H".to_i will get 15.
7
+ {
8
+ years: years.to_i,
9
+ months: months.to_i,
10
+ days: days.to_i,
11
+ hours: hours.to_i,
12
+ minutes: minutes.to_i,
13
+ seconds: seconds.to_i,
14
+ }
15
+ end
16
+ end
17
+
18
+ def duration_time
19
+ components.reduce(0) do |acc,(k,v)|
20
+ acc + v.public_send(k)
21
+ end
22
+ end
23
+ end
@@ -1,24 +1,13 @@
1
1
  # https://www.w3.org/TR/xmlschema-2/#duration
2
- class Giri::TextNodeDuration < Giri::TextNodeString
2
+ class Giri::TextNodeDuration < DelegateClass(Giri::Duration)
3
+ extend Giri::Bud
3
4
 
4
- def components
5
- @components ||= begin
6
- _, years, months, days, time_part, hours, minutes, seconds = self.match(/\AP(\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?\z/).to_a
7
- # Example of values "15H" and "15H".to_i will get 15.
8
- {
9
- years: years.to_i,
10
- months: months.to_i,
11
- days: days.to_i,
12
- hours: hours.to_i,
13
- minutes: minutes.to_i,
14
- seconds: seconds.to_i,
15
- }
16
- end
5
+ def self.build_value(str)
6
+ Giri::Duration.new(str)
17
7
  end
18
8
 
19
- def duration_time
20
- components.reduce(0) do |acc,(k,v)|
21
- acc + v.public_send(k)
22
- end
9
+ def initialize(*args)
10
+ initialize_setup(*args)
11
+ super(self.class.build_value(@node.text))
23
12
  end
24
13
  end
data/lib/giri/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Giri
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/giri.rb CHANGED
@@ -5,6 +5,8 @@ require "active_support/core_ext/integer"
5
5
 
6
6
  require "giri/version"
7
7
  require "giri/bud"
8
+ require "giri/context"
9
+ require "giri/duration"
8
10
  require "giri/base_node"
9
11
  require "giri/text_node_string"
10
12
  require "giri/text_node_integer"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: giri
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - metheglin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-05 00:00:00.000000000 Z
11
+ date: 2024-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -62,6 +62,8 @@ files:
62
62
  - lib/giri.rb
63
63
  - lib/giri/base_node.rb
64
64
  - lib/giri/bud.rb
65
+ - lib/giri/context.rb
66
+ - lib/giri/duration.rb
65
67
  - lib/giri/text_node_big_decimal.rb
66
68
  - lib/giri/text_node_date_time.rb
67
69
  - lib/giri/text_node_duration.rb